NetworkFirst strategy for cookie presence in a service worker

I’d like to use the NetworkFirtst strategy for the WordPress logged-in users. I tried but can’t get the Cookie header, it returns with the error “Uncaught TypeError: Cannot read property ‘headers’ of undefined” for the code:

(() => {
    importScripts("https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js");

    const {
        registerRoute: e,
        NavigationRoute: a
    } = workbox.routing, {
        StaleWhileRevalidate: n,
        NetworkOnly: s,
        CacheFirst: t,
        NetworkFirst: c
    } = workbox.strategies;

    const user = e.request.headers.get("Cookie").includes(`wordpress_logged`),
    h = new c({
        cacheName: "user-cache"
    }), l = new n({
        cacheName: "default-cache"
    });

    e(new a((async e => {
        try {
            return user ? await h.handle(e) : await l.handle(e)
        } catch (a) {
            return new Promise((a => {
                caches.match(e.url.pathname).then((e => a(e || caches.match("/offline/", {
                    cacheName: "offline-cache"
                }))))
            }))
        }
    })))
})();

I tried many different ways to get the logged-in cookie header but failed! Can there be a solution?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.