最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to update service workers? - Stack Overflow

programmeradmin3浏览0评论

Situation:

On mywebsite/game, I registered a service-worker with

navigator.serviceWorker.register('/service-worker.js', {scope: "/"});

On my server, '/service-worker.js' has a maxAge of 1d.


Problem:

service-worker.js has a major bug. It always displays an empty page and can't fetch anything. service-worker.js must be changed.

The problem is whenever a user goes to mywebsite/game, it displays the empty page and does nothing more. I am unable to make the client fetch the new service-worker.js.

How can I make the client fetch the new service-worker.js?

Situation:

On mywebsite.com/game, I registered a service-worker with

navigator.serviceWorker.register('/service-worker.js', {scope: "/"});

On my server, '/service-worker.js' has a maxAge of 1d.


Problem:

service-worker.js has a major bug. It always displays an empty page and can't fetch anything. service-worker.js must be changed.

The problem is whenever a user goes to mywebsite.com/game, it displays the empty page and does nothing more. I am unable to make the client fetch the new service-worker.js.

How can I make the client fetch the new service-worker.js?

Share Improve this question edited Sep 13, 2019 at 3:24 RainingChain asked Sep 13, 2019 at 3:01 RainingChainRainingChain 7,73610 gold badges38 silver badges69 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 17

What you're describing—a check for updates to /service-worker.js—happens by default, automatically, under the circumstances laid out in this article:

An update is triggered if any of the following happens:

  • A navigation to an in-scope page.
  • A functional events such as push and sync, unless there's been an update check within the previous 24 hours.
  • Calling .register() only if the service worker URL has changed. However, you should avoid changing the worker URL.

All modern web browsers will ignore any Cache-Control headers you set on /service-worker.js by default and go directly against the web server to obtain the latest copy.

This Stack Overflow answer has some best practices for what the revised service-worker.js file should contain if you want it to behave like a "kill switch."

Close your page and open it again. Source

The accepted answer does not provides any working solution.

Just add ?v=1 to your script like this.

navigator.serviceWorker.register('/service-worker.js?v=1', {scope: "/"});

And increment the number of script version when you make changes of service worker's script

发布评论

评论列表(0)

  1. 暂无评论