[英]Chrome is caching service-worker.js even with Cache-Control: no-store, no-cache Max-Age: 0
Chrome v72 Seems to be ignoring Cache-Control headers and caching service-worker.js To test, I've set all resources to use: Chrome v72似乎忽略了Cache-Control标头并缓存service-worker.js要测试,我将所有资源都设置为使用:
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private');
res.set('Max-Age', '0');
When navigating to localhost:4000/dist/service-worker.js I'm able to verify this is set correctly: 导航到localhost:4000 / dist / service-worker.js时,我可以验证此设置是否正确:
HTTP/1.1 200 OK
X-Powered-By: Express
Cache-Control: no-store, no-cache, must-revalidate, private
Max-Age: 0
Access-Control-Allow-Origin: *
Accept-Ranges: bytes
Last-Modified: Thu, 14 Feb 2019 23:17:55 GMT
ETag: W/"3c1-168ee4d6297"
Content-Type: application/javascript; charset=UTF-8
Content-Length: 961
Vary: Accept-Encoding
Date: Thu, 14 Feb 2019 23:27:24 GMT
Connection: keep-alive
Even with this, as of Chrome 68, it "should" bypass the HTTP Cache by default: https://developers.google.com/web/updates/2018/06/fresher-sw 即使这样,从Chrome 68开始,它仍应默认情况下``应''绕过HTTP缓存: https : //developers.google.com/web/updates/2018/06/fresher-sw
It's clearly not. 显然不是。
I have update on reload checked in the Applications Tab. 我在“应用程序”选项卡中检查了有关重新加载的更新 。 I have closed Chrome and reopened and it still is using the old service-worker.js from the cache.
我已经关闭了Chrome,然后重新打开,它仍在使用缓存中的旧service-worker.js。
My workbox config looks like: 我的工作箱配置如下:
new GenerateSW({
include: [/\.css$/, /\.js$/],
clientsClaim: true,
skipWaiting: true
}),
I'm also able to verify, Chrome is caching the service-worker.js because if I: 我还可以验证,Chrome正在缓存service-worker.js,因为如果我:
Open a new tab and navigate to: http://localhost:4000/dist/service-worker.js 打开一个新标签,然后浏览至: http:// localhost:4000 / dist / service-worker.js
Chrome actually gets the LATEST, and now the other tab fires the "install" event. Chrome实际上获得了最新的信息,现在另一个标签触发了“安装”事件。
The second to last test: 倒数第二次测试:
The final test: 最终测试:
navigator.serviceWorker.register(`/dist/service-worker.js?hack=${Math.random()}`)
Of course, now it picks up all the new service-worker.js updates. 当然,现在它接收了所有新的service-worker.js更新。
Is this a Chrome Bug? 这是Chrome Bug吗? Am I not configuring something correctly?
我配置不正确吗? This same behavior happens in Chrome Canary.
Chrome Canary中也会发生相同的行为。
Alternatively, I can do the following to bypass the cache: 另外,我可以执行以下操作绕过缓存:
navigator.serviceWorker
.register(`/dist/sw.js`)
.then(registration => {
registration.update();
})
But this seems like a hack.... 但这似乎是一个hack ....
It ended up being a scoping issue due to the file being in /dist/service-worker.js and not in the root. 由于文件位于/dist/service-worker.js而不是根目录中,因此最终成为范围问题。
To fix the scoping issue I needed to add the following header to the service worker response: 要解决范围问题,我需要在服务工作者响应中添加以下标头:
res.set('Service-Worker-Allowed', '/');
Then when registering: 然后在注册时:
navigator.serviceWorker.register(`/dist/sw.js`, { scope: '/' });
Huge thanks to this article: https://developers.google.com/web/tools/workbox/guides/troubleshoot-and-debug 非常感谢本文: https : //developers.google.com/web/tools/workbox/guides/troubleshoot-and-debug
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.