我认为通过在服务工作线程的文件名中添加版本字符串来使用缓存清除是一个坏主意。任何教程中都从未提到过这一点,而且我也从未在野外见过这种方法。
您应该使用 no-cache
directive和 max-age
field在服务工作线程文件的响应 header 中。
但是由于我没有找到关于此方法的专门声明,所以我尝试了一下,似乎如果您更改服务工作人员文件,就很难摆脱旧的文件。所以我可以在开发工具的源选项卡中看到这两个文件。
但是您不会立即在应用程序选项卡中看到新的服务工作人员,所以我不确定是什么阻碍了新的软件负责以及它实际上在等待什么。
有人知道浏览器通常如何处理这种情况吗?
最佳答案
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.
关于service-worker - 当您将版本/哈希添加到 Service Worker 文件时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54145027/