简体   繁体   中英

Service worker does not cache index.html served by golang server on '/'

I'm playing around with a service worker trying to cache my index.html. I'm serving it via

http.HandleFunc("/sw", handle.SW)
http.HandleFunc("/", handle.Index)

My service worker cache looks like this

self.addEventListener('install', (event) => {
    self.skipWaiting();
    event.waitUntil(async function () {
        const cache = await caches.open(CACHE_VERSION);
        await cache.addAll([
            '/',
            '/static/merged.css',
            '/static/merged.js',
            '/favicon.ico'
        ]);
    }());
});

Without the service worker my network tab in the developer tools shows the size of the index.html with 4.5 KB and a time of 46ms.

在此处输入图片说明

Now with the service worker merged.css, merged.js and favicon.ico are cached, but my index has two entries. One from service worker, and one of size 4.5 KB and a time of 48ms.

在此处输入图片说明

So I'm guessing that index.html is not cached and I can not find out how to do it.

From the screenshot you uploaded, it says that the request for fetching the index.html is initiated by a script named sw.js, and the exact code is placed at the second line. If this is truly the service-worker in your code, please recheck it carefully and find out why this request is made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM