How to make ServiceWorker survive cache reset/Shift+F5?

北城以北 提交于 2019-12-23 15:09:22

问题


I want to create a website that can work even when it's server is offline — I found that that's what ServiceWorkers are for.

When I reload the page with service worker and no connectivity, it works just fine. However, shift+reload (e.g. bypassing cache) disarms service worker and I get "could not connect to server" error.

My question is — can I somehow prevent shift+reload (shift+f5, ctrl+f5 etc) from ruining service worker, or, at least, make it recover afterwards without restoring connectivity?


回答1:


QUICK Answer

How to make ServiceWorker survive cache reset/Shift+F5?

Theorically (*), you can do it with js plugins which detect the key hit of Ctrl or Shift... then prevent the "force refresh" to happen

...but there is a story behind this Ctrl/Shift reload.

(*) disclaimer: I've not tried this yet


LONG story... (kind of)

This is actually a spec of Service Worker. And only present in recent change of Chrome. For the earlier version of Chrome , Service Worker has no any issue surviving a "force refresh".

Along with the spec from W3C

navigator.serviceWorker.controller returns null if the request is a force refresh (shift+refresh).

Also, there are many people (**) has suggested that a "force refresh" should always clear out all kind of caches. Which is matched with the purpose of its existent and its spec.

...Furthermore, we even got it on the wikipedia...

Wikipedia: Bypassing your cache means forcing your web browser to re-download a web page from scratch

(**) us, web developers in the early stage of service worker.

In my opinion I think it is OK to let a force refresh doing its job. Since pretty much all of us always expecting the browser to not use any cache when we doing this.



来源:https://stackoverflow.com/questions/37559415/how-to-make-serviceworker-survive-cache-reset-shiftf5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!