service-worker - 当您将版本/哈希添加到 Service Worker 文件时会发生什么?

标签 service-worker browser-cache

我认为通过在服务工作线程的文件名中添加版本字符串来使用缓存清除是一个坏主意。任何教程中都从未提到过这一点,而且我也从未在野外见过这种方法。

您应该使用 no-cache directivemax-age field在服务工作线程文件的响应 header 中。

但是由于我没有找到关于此方法的专门声明,所以我尝试了一下,似乎如果您更改服务工作人员文件,就很难摆脱旧的文件。所以我可以在开发工具的选项卡中看到这两个文件。

Sources: two SW files

但是您不会立即在应用程序选项卡中看到新的服务工作人员,所以我不确定是什么阻碍了新的软件负责以及它实际上在等待什么。

New SW (renamed!) awaits installation

有人知道浏览器通常如何处理这种情况吗?

最佳答案

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:

  1. index.html registers sw-v1.js as a service worker.
  2. sw-v1.js caches and serves index.html so it works offline-first.
  3. 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/

相关文章:

java - 为什么 Chrome DevTools 说我的静态资源在使用 Spring 的 mvc :resources? 映射时显式不可缓存

service-worker - pwa - Service Worker 未成功提供 list 的 start_url

javascript - 如何从 Service Worker 对象调用 React 类的函数?

javascript - SW-Toolbox 防止加载跨源视频

javascript - 为什么 workbox registerRoute 得到一个不透明的响应,而预缓存却没有

caching - Safari 及其贪婪的缓存

javascript - 如果我重新启动应用程序池,客户端浏览器是否会引用我更新的 javascript 或 css 文件?

javascript - 服务人员显示通知不起作用

javascript - 启用 JSP 文件中包含的 Javascript、CSS 文件的缓存

asp.net-mvc-3 - 新鲜加载页面而不是从缓存中加载