I'm using workbox with workbox-webpack-plugin to cache some assets. My current config is supposed to cache two files: a .js and a .css. Both files are being cached properly, but the problem is that the browser still downloads them from the network, and I have no idea why.
Here's the workbox plugin in my webpack config that generates the service worker:
new GenerateSW({
swDest: 'service-worker.js',
importWorkboxFrom: 'local',
chunks: ['myChunk'],
skipWaiting: true,
clientsClaim: true,
ignoreUrlParametersMatching: [/.*/],
cacheId: 'myCacheId',
}),
Here's the generated service worker:
/**
* Welcome to your Workbox-powered service worker!
*
* You'll need to register this file in your web app and you should
* disable HTTP caching for this file too.
* See https://xxx
*
* The rest of the code is auto-generated. Please don't update this file
* directly; instead, make changes to your Workbox build configuration
* and re-run your build process.
* See https://xxx
*/
importScripts("workbox-v3.6.3/workbox-sw.js");
workbox.setConfig({modulePathPrefix: "workbox-v3.6.3"});
importScripts(
"precache-manifest.14645da973669ef1d2247d1863e806bd.js"
);
workbox.core.setCacheNameDetails({prefix: "myCacheId"});
workbox.skipWaiting();
workbox.clientsClaim();
/**
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
* requests for URLs in the manifest.
* See https://xxx
*/
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {
"ignoreUrlParametersMatching": [/.*/]
});
And precache manifest:
self.__precacheManifest = [
{
"revision": "9b22d66a17276ac21d45",
"url": "myChunk.js"
},
{
"revision": "9b22d66a17276ac21d45",
"url": "myChunk.css"
}
];
The precaching actually works, but it looks like the fetch event is not intercepted by the service worker. If I try to download the files directly from the address bar in Chrome, the file is loaded correctly from the service worker. But when it's loaded from the script tag of my page, it's still being downloaded from the network.
Here are the request header when loaded from the script tag:
GET /assets/myChunk.js?1546600720154 HTTP/1.1
Host: localhost:5000
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Accept: */*
Referer: http://localhost:5000/xxx
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,fr-BE;q=0.8,fr;q=0.7,te-IN;q=0.6,te;q=0.5
Cookie: visitor_id=f86c312d-76e2-468d-a5c5-45c47fa3bbdc
Any help would be great!