简体   繁体   English

如何缓存签名文件(服务工作者)

[英]How to cache signed files (Service worker)

Several front-end applications implement a signature template in the files to control the cache, and each change in the file that signature changes. 几个前端应用程序在文件中实现签名模板以控制缓存,并且签名中文件的每个更改都会更改。 How do I get the Service Worker to handle these signatures and cache? 我如何让Service Worker处理这些签名和缓存?

Signature Examples: 签名示例:

  • sw-d58e3582afa99040e27b92b13c8f2280.js sw-d58e3582afa99040e27b92b13c8f2280.js
  • sw.js?_gc=20180101 sw.js?_gc = 20180101

I'm working on an application that is already ready, trying to implement service worker for certain features to become available offline. 我正在开发一个已经准备就绪的应用程序,试图为特定功能实现服务工作者以使其离线可用。

Example, in this section I need to say what I will cache, however, the application changes the signatures of the file to control the cache. 例如,在本节中,我需要说一下将要缓存的内容,但是,应用程序更改了文件的签名以控制缓存。 (today it's like this) (今天就是这样)

 caches.open('my-cache').then(function(cache) { return cache.addAll([ '/index.html', '/styles.css', '/main.js' ]);}) 

The application is always changing "styles.css" to "styles.css? V = 1527624807103_1" (timestamp) As far as I understand, "styles.css" is not the same as "styles.css? V = 1527624807103_1". 应用程序始终将“ styles.css”更改为“ styles.css?V = 1527624807103_1”(时间戳)据我了解,“ styles.css”与“ styles.css?V = 1527624807103_1”不同。

You should not version or add a signture parameter to the URL used for your actual, top-level service worker file. 您不应该对用于实际顶级服务工作者文件的URL进行版本控制或添加签名参数。 As explained in " The Service Worker Lifecycle ": 如“ 服务工作者生命周期 ”中所述:

It can land you with a problem like this: 它可能使您遇到这样的问题:

  1. index.html registers sw-v1.js as a service worker. index.html将sw-v1.js注册为服务工作者。
  2. sw-v1.js caches and serves index.html so it works offline-first. sw-v1.js会缓存并提供index.html,因此它会先离线运行。
  3. You update index.html so it registers your new and shiny sw-v2.js. 您更新index.html,以便它注册新的闪亮的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. 如果执行上述操作,则用户永远不会获得sw-v2.js ,因为sw-v1.js正在从其缓存中提供旧版本的index.html。 You've put yourself in a position where you need to update your service worker in order to update your service worker. 您已将自己置于需要更新服务人员以更新服务人员的位置。

As for the actual URLs that are included inside of your service worker file, which may legitimately contain versioning/hashes/signatures, your best bet is to use a tool that integrates with your build process and will generate the list of URLs based on the latest versions of each file that should be precached. 至于服务工作者文件中包含的实际URL(可能合法包含版本控制/哈希/签名),最好的选择是使用与构建过程集成的工具,并将基于最新的URL生成URL列表。应该预先缓存的每个文件的版本。

Workbox is one such tool, and there are others, like sw-precache (a precursor to Workbox), and offline-plugin . Workbox就是这样一种工具,还有其他工具,例如sw-precache (Workbox的前身)和offline-plugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM