web-servicesfirefoxservice-workerweb-manifest

Desktop Firefox replaces user-agent in cache usage with service-worker.js


The user-agent of my normal desktop Firefox:

Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0

The user agent that Firefox sends when I cache some files with service-worker.js:

Mozilla/5.0 (Android 4.4; Mobile; rv:109.0) Gecko/109.0 Firefox/109.0

Note 1: I get this result in normal desktop website browsing.

Note 2: When I inspect the network traffic in the developer console, the user-agent is sent normally. However, when I test it on the server side, it gives results as if it is a mobile browser.

Note 3: When I refresh the page with ctrl+f5, it shows the correct user-agent for once.

My service-worker.js file:

let version;
version = 4;


// This is the service worker with the combined offline experience (Offline page + Offline copy of pages)

const CACHE = "system-v"+version;

importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');

// TODO: replace the following with the correct offline fallback page i.e.: const offlineFallbackPage = "offline.html";
const offlineFallbackPage = "pwa-offline/?v="+version;

self.addEventListener("message", (event) => {
    if (event.data && event.data.type === "SKIP_WAITING") {
        self.skipWaiting();
    }
});

self.addEventListener('install', async (event) => {
    event.waitUntil(
        caches.open(CACHE)
            .then((cache) => cache.add(offlineFallbackPage))
    );
});

if (workbox.navigationPreload.isSupported()) {
    workbox.navigationPreload.enable();
}

workbox.routing.registerRoute(
    new RegExp('/*\.(js|css|jpe?g|png|gif|webp|svg|ico)'),
    new workbox.strategies.StaleWhileRevalidate({
        cacheName: CACHE
    })
);

self.addEventListener('fetch', (event) => {
    if (event.request.mode === 'navigate') {
        event.respondWith((async () => {
            try {
                const preloadResp = await event.preloadResponse;
                
                if (preloadResp) {
                    return preloadResp;
                }
                
                const networkResp = await fetch(event.request);
                return networkResp;
            } catch (error) {
                
                const cache = await caches.open(CACHE);
                const cachedResp = await cache.match(offlineFallbackPage);
                return cachedResp;
            }
        })());
    }
});

What is causing this problem and how can I solve it?

Solved:

Problem has been detected. It's a add-on problem. Official side view add-on injects user agent when using service worker.


Solution

  • Problem has been detected. It's a add-on problem. Official side view add-on injects user agent when using service worker.