简体   繁体   English

Chrome甚至使用Cache-Control来缓存service-worker.js:不存储,不缓存最大年龄:0

[英]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,因为如果我:

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.

相关问题 谷歌浏览器不尊重缓存控制中的 max-age - Google chrome not respecting max-age in Cache-Control 浏览器是否继续使用“ Cache-Control:max-age”发送资源请求? - Does browser continue to send requests for resource even with `Cache-Control: max-age` Axios GET 请求忽略响应 Cache-Control max-age - Axios GET requests ignoring response Cache-Control max-age Chrome 正在发送 Cache-control:no-cache 标头 - Chrome is sending Cache-control:no-cache header 如何清除Firebase上的service-worker.js缓存 - How to clear service-worker.js cache on firebase 服务工作者缓存是否支持缓存控制标头? - Does the service worker cache support cache-control headers? 强制缓存控制:在 F5 重新加载时通过 XMLHttpRequest 在 Chrome 中无缓存 - Force Cache-Control: no-cache in Chrome via XMLHttpRequest on F5 reload Rails PWA 中的 CacheStorage 始终为空。 在 service-worker.js cache.addAll 似乎没有缓存任何东西 - CacheStorage in Rails PWA always empty. In service-worker.js cache.addAll doesn't seem to cache anything Firefox实时HTTP标头,在Cache-Control之后使用大括号:no-cache - Firefox live http headers, curley braces after Cache-Control: no-cache Chrome推送通知正在更新Service-Worker.js - Chrome Push Notification Updating Service-Worker.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM