服务人员JavaScript更新频率(每24小时一次) [英] Service worker JavaScript update frequency (every 24 hours?)

查看:74
本文介绍了服务人员JavaScript更新频率(每24小时一次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此MDN文档:

此后,大约每24小时下载一次.可能是下载频率更高,但必须每24小时下载一次防止不良脚本长时间烦人.

After that it is downloaded every 24 hours or so. It may be downloaded more frequently, but it must be downloaded every 24h to prevent bad scripts from being annoying for too long.

Firefox和Chrome浏览器也一样吗?还是仅在用户导航到网站时才更新对Service Worker的JavaScript?

Is the same true for Firefox and Chrome? OR does update to service worker javascript only happens when user navigates to site?

推荐答案

注意:从Firefox 57起 Chrome 68 ,以及Safari的版本和支持服务人员的Edge,默认行为已更改为符合更新的服务人员规范.在那些浏览器中,默认情况下,在检查服务工作者脚本中的更新时将忽略HTTP缓存指令.以下说明仍适用于早期版本的Chrome和Firefox.

Note: As of Firefox 57, and Chrome 68, as well as the versions of Safari and Edge that support service workers, the default behavior has changed to account for the updated service worker specification. In those browsers, HTTP cache directives will, by default, be ignored when checking the service worker script for updates. The description below still applies to earlier versions of Chrome and Firefox.

每次,每次导航到服务工作者范围内的新页面时,Chrome都会对传递到 navigator.serviceWorker.register的JavaScript资源进行标准的HTTP请求.()调用.假设它的名称为 service-worker.js .此请求仅与导航结合使用,或通过例如 push 事件唤醒服务工作者时发出.没有的后台进程每24小时重新获取每个服务工作者脚本,或类似的自动化操作.

Every time you navigate to a new page that's under a service worker's scope, Chrome will make a standard HTTP request for the JavaScript resource that was passed in to the navigator.serviceWorker.register() call. Let's assume it's named service-worker.js. This request is only made in conjunction with a navigation or when a service worker is woken up via, e.g., a push event. There is not a background process that refetches each service worker script every 24 hours, or anything automated like that.

此HTTP请求将遵循标准 HTTP缓存指令,但有一个例外(将在下一段中介绍).例如,如果您的服务器设置了适当的HTTP响应标头,该标头指示应使用缓存的响应1个小时,则在接下来的1个小时内,浏览器对 service-worker.js 的请求将通过浏览器的缓存.请注意,我们不是谈论缓存存储API ,在这种情况下不相关,而是标准浏览器HTTP缓存.

This HTTP request will obey standard HTTP cache directives, with one exception (which is covered in the next paragraph). For instance, if your server set appropriate HTTP response headers that indicated the cached response should be used for 1 hour, then within the next hour, the browser's request for service-worker.js will be fulfilled by the browser's cache. Note that we're not talking about the Cache Storage API, which isn't relevant in this situation, but rather standard browser HTTP caching.

标准HTTP缓存规则的一个例外(这就是24小时的问题所在)是,如果 service-worker.js 条目的使用期限,浏览器将始终连接到网络.HTTP缓存中的时间大于24小时.因此,从功能上讲,使用1天或1周或1年的 max-age 并没有区别-它们都将被视为 max-age 1天.

The one exception to standard HTTP caching rules, and this is where the 24 hours thing comes in, is that browsers will always go to the network if the age of the service-worker.js entry in the HTTP cache is greater than 24 hours. So, functionally, there's no difference in using a max-age of 1 day or 1 week or 1 year—they'll all be treated as if the max-age was 1 day.

浏览器供应商希望确保开发人员不会意外推出破碎的"或有故障的 service-worker.js ,该代码以 max-age 1年,使用户长时间无法体验持久的网络体验.(您不能依靠您的用户知道清除其站点数据或转移-重新加载该站点.)

Browser vendors want to ensure that developers don't accidentally roll out a "broken" or buggy service-worker.js that gets served with a max-age of 1 year, leaving users with what might be a persistent, broken web experience for a long period of time. (You can't rely on your users knowing to clear out their site data or to shift-reload the site.)

某些开发人员更喜欢使用响应标头显式地为其 service-worker.js 提供服务,从而导致所有HTTP缓存均被禁用,这意味着对 service-worker.js 用于每个导航.另一种方法可能是使用非常短的 age (例如一分钟)来提供某种程度的限制,以防单个用户进行大量快速导航.如果您确实希望最小化请求,并且确信不会很快更新您的 service-worker.js ,则可以随意设置一个 max-age 24小时,但我建议您减少一些您意外需要重新部署的机会.

Some developers prefer to explicitly serve their service-worker.js with response headers causing all HTTP caching to be disabled, meaning that a network request for service-worker.js is made for each and every navigation. Another approach might be to use a very, very short max-age—say a minute—to provide some degree of throttling in case there is a very large number of rapid navigations from a single user. If you really want to minimize requests and are confident you won't be updating your service-worker.js anytime soon, you're free to set a max-age of 24 hours, but I'd recommend going with something shorter on the off chance you unexpectedly need to redeploy.

这篇关于服务人员JavaScript更新频率(每24小时一次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆