Firefox, service workers and the Back button(Firefox、服务人员和后退按钮)
本文介绍了Firefox、服务人员和后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到报告说,在最新版本的Firefox中,按Back会导致My ServiceWorker提供的"You Are Offline"页面出现。
以下是ServiceWorker的功能部分:
self.addEventListener('fetch',function(event) {
// We only want to call event.respondWith() if this is a navigation request
// for an HTML page.
// request.mode of 'navigate' is unfortunately not supported in Chrome
// versions older than 49, so we need to include a less precise fallback,
// which checks for a GET request with an Accept: text/html header.
if (event.request.mode === 'navigate' ||
(event.request.method === 'GET' &&
event.request.headers.get('accept').includes('text/html'))) {
event.respondWith(
fetch(event.request).catch(function(error) {
// The catch is only triggered if fetch() throws an exception, which will most likely
// happen due to the server being unreachable.
// If fetch() returns a valid HTTP response with an response code in the 4xx or 5xx
// range, the catch() will NOT be called. If you need custom handling for 4xx or 5xx
// errors, see https://github.com/GoogleChrome/samples/tree/gh- pages/service-worker/fallback-response
return caches.match(OFFLINE_URL);
})
);
}
// If our if() condition is false, then this fetch handler won't intercept the request.
// If there are any other fetch handlers registered, they will get a chance to call
// event.respondWith(). If no fetch handlers call event.respondWith(), the request will be
// handled by the browser as if there were no service worker involvement.
});
因此,由于某种原因,在Firefox中,按Back返回的是OFFLINE_URL而不是目标页面。
这可能是什么原因造成的,我该如何进行调试?
推荐答案
火狐在使用"后退"按钮时,显然有一个Chrome没有的额外步骤。
它执行一个"只有在缓存的情况下"的请求,这当然会失败,因为页面没有缓存(它们都是动态的)。由于失败,它会抛出一个错误,并调用catch。
添加此检查已修复:
&& event.request.cache !== 'only-if-cached'
这使Firefox能够意识到资源未缓存,并照常继续。
这篇关于Firefox、服务人员和后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:Firefox、服务人员和后退按钮
基础教程推荐
猜你喜欢
- 带角度的选项卡:仅使用 $http 在单击时加载选项卡 2022-01-01
- 即使每次插入第一个输入的值不同,第二个输入仍显示相同的输入值 2022-01-01
- 最佳动态 JavaScript/JQuery 网格 2022-01-01
- 在 Javascript 中使用 Fetch API 上传文件并显示进度 2022-01-01
- 从快速中间件中排除路由 2022-01-01
- CORS:当凭据标志为真时,无法在 Access-Control-Allow-Origin 中使用通配符 2022-01-01
- HTML5 画布调整为父级 2022-01-01
- 使用 jQuery 在悬停时交换 DIV 类 2022-01-01
- 当木偶师打开Chrome时,不能使用Chrome扩展 2022-01-01
- 逻辑运算符 ||在 javascript 中,0 代表 Boolean false? 2022-01-01
