Closed Bug 1804622 Opened 2 years ago Closed 2 years ago

Serviceworker fetch requests are not visible in DevTools

Categories

(DevTools :: Netmonitor, defect, P2)

defect

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 1432311

People

(Reporter: jdescottes, Unassigned)

References

(Blocks 1 open bug)

Details

Serviceworkers can initiate fetch requests, but those don't show up in the regular devtools because we filter them out in https://searchfox.org/mozilla-central/rev/670e2e0999f04dc7734c8c12b2c3d420a1e31f12/devtools/shared/network-observer/NetworkUtils.sys.mjs#363-366.

There are at least two scenarios impacted, with different symptoms.

Bug 1781258 comment #15 gives an example of a service worker intercepting a fetch request to add an Authorization header:

self.addEventListener("fetch", (event) => {
    // ignore non MM requests
    if (!authorization
        || event.request.method !== "GET"
        || event.request.url.indexOf(targetPrefix) === -1) {
        return;
    }

    var url = event.request.url;

    log('fetch event returning: ' + url + ' authorization: ' + authorization);

    event.respondWith(
        fetch(event.request.url, {
            method: "GET",
            cache: 'no-cache',
            headers: {
                "Authorization": "Bearer " + authorization,
            },
            redirect: "follow"
        })
    );
});

And over at https://serviceworkerapp.azurewebsites.net/basic2/ we have an example using fetch to feed a cache.

self.oninstall = function(event) {
    event.waitUntil(
        caches.open('assets-v1').then(function(cache) {
            return cache.addAll([
                './',
                './index.html',
                './styles.css',
                './bg.jpg',
                './common.js',
                './util.js',
                './main.js',
                './fallback.html'
            ]);
        })
    );
}

self.onactivate = function(event) {
    var keepList = ['assets-v1'];
 
    event.waitUntil(
        caches.keys().then(function(cacheNameList) {
            return Promise.all(cacheNameList.map(function(cacheName) { 
                if (keepList.indexOf(cacheName) === -1) {
                    return caches.delete(cacheName);
                }
            }));
        })
    );
}

self.onfetch = function(event) {
    event.respondWith(
        caches.match(event.request).then(function(response) {
            return response || fetch(event.request).then(function(response) {
                caches.open('dynamic').then(function(cache) {
                    cache.put(event.request, response);
                });
                return response.clone();
            });
        })
    );
}
Severity: -- → S3
Priority: -- → P2

Already tracked in Bug 1432311

Status: NEW → RESOLVED
Closed: 2 years ago
Duplicate of bug: 1432311
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.