如何防止硬重载绕过 Service Worker?
How to prevent hard reloads from bypassing the service worker?
硬重新加载或硬刷新(例如 Chrome 中的 shift-reload)似乎绕过了 service worker。
例如,加载一个 service-worker 控制的页面,例如 https://airhorner.com/ or https://wiki-offline.jakearchibald.com/,在 devtools 中将网络设置为 "offline",然后硬性重新加载页面会导致损坏 "there is no internet connection"页。 (如预期的那样,定期重新加载会显示缓存页面。)
有没有办法防止这种情况发生,或者在设备离线时使用 service worker 作为后备?
这是作为 service worker specification 的一部分明确调用的行为:
navigator.serviceWorker.controller returns null if the request is a
force refresh (shift+refresh). The ServiceWorker objects returned from
this attribute getter that represent the same service worker are the
same objects.
所以这不仅仅是一个浏览器实现细节。
如果您觉得有充分的理由说明服务人员不应该那样做,最好的方法是在规范 issue tracker.
中提出您的顾虑
硬重新加载或硬刷新(例如 Chrome 中的 shift-reload)似乎绕过了 service worker。
例如,加载一个 service-worker 控制的页面,例如 https://airhorner.com/ or https://wiki-offline.jakearchibald.com/,在 devtools 中将网络设置为 "offline",然后硬性重新加载页面会导致损坏 "there is no internet connection"页。 (如预期的那样,定期重新加载会显示缓存页面。)
有没有办法防止这种情况发生,或者在设备离线时使用 service worker 作为后备?
这是作为 service worker specification 的一部分明确调用的行为:
navigator.serviceWorker.controller returns null if the request is a force refresh (shift+refresh). The ServiceWorker objects returned from this attribute getter that represent the same service worker are the same objects.
所以这不仅仅是一个浏览器实现细节。
如果您觉得有充分的理由说明服务人员不应该那样做,最好的方法是在规范 issue tracker.
中提出您的顾虑