当你将 version/hash 添加到你的 service worker 文件时会发生什么?
What happens when you add a version/hash to your service worker file?
我认为通过在其文件名中添加版本字符串来为您的服务工作者使用缓存破坏是个坏主意。这在任何教程中都从未提及,我也从未在野外见过这种方法。
您应该在服务工作者文件的响应 header 中使用 no-cache
directive and the 。
但是由于我没有找到关于此方法的专门声明,所以我尝试了一下,如果您更改 service worker 文件,似乎很难摆脱旧的。所以我可以在 dev-tools 的 sources 选项卡中看到这两个文件。
但是您不会立即在 application 选项卡中看到新的 service worker,所以我不确定,是什么阻碍了新的 SW 负责以及它是什么其实在等
有人知道浏览器通常如何处理这种情况吗?
Here's some guidance 建议避免这种情况的原因:
Avoid changing the URL of your service worker script
If you've read my
post on
caching best practices, you may consider giving each version of your
service worker a unique URL. Don't do this! This is usually bad
practice for service workers, just update the script at its current
location.
It can land you with a problem like this:
- index.html registers sw-v1.js as a service worker.
- sw-v1.js caches and serves index.html so it works offline-first.
- You update index.html so it registers your new and shiny sw-v2.js.
If you do the above, the user never gets sw-v2.js, because sw-v1.js is
serving the old version of index.html from its cache. You've put
yourself in a position where you need to update your service worker in
order to update your service worker. Ew.
我认为通过在其文件名中添加版本字符串来为您的服务工作者使用缓存破坏是个坏主意。这在任何教程中都从未提及,我也从未在野外见过这种方法。
您应该在服务工作者文件的响应 header 中使用 no-cache
directive and the
但是由于我没有找到关于此方法的专门声明,所以我尝试了一下,如果您更改 service worker 文件,似乎很难摆脱旧的。所以我可以在 dev-tools 的 sources 选项卡中看到这两个文件。
但是您不会立即在 application 选项卡中看到新的 service worker,所以我不确定,是什么阻碍了新的 SW 负责以及它是什么其实在等
有人知道浏览器通常如何处理这种情况吗?
Here's some guidance 建议避免这种情况的原因:
Avoid changing the URL of your service worker script
If you've read my post on caching best practices, you may consider giving each version of your service worker a unique URL. Don't do this! This is usually bad practice for service workers, just update the script at its current location.
It can land you with a problem like this:
- index.html registers sw-v1.js as a service worker.
- sw-v1.js caches and serves index.html so it works offline-first.
- You update index.html so it registers your new and shiny sw-v2.js.
If you do the above, the user never gets sw-v2.js, because sw-v1.js is serving the old version of index.html from its cache. You've put yourself in a position where you need to update your service worker in order to update your service worker. Ew.