我有一个可离线运行的渐进式Web应用程序,但是当将它添加到iOS上的“主屏幕”时,当您进入飞行模式(离线)时,它会显示一个屏幕,显示:
Safari cannot open the page
The error was:
"FetchEvent.respondWith received an error: TypeError: The internet connection appears to be offline."
我知道这来自服务工作者的fetch事件,因为它试图传递“ POST”请求,应该避免这种情况,因为它们会导致错误,并且我不想缓存它们。我尝试捕获错误,或者只是记录错误,但我一直看到该屏幕,有一种方法可以忽略“ POST”请求以防止出现此屏幕
这是我在服务工作者上获取事件
self.addEventListener("fetch", function(event) {
if (event.request.method == "POST") {
return;
}
event.respondWith(
caches
.match(event.request)
.then(function(res) {
if (!navigator.onLine) {
if (res) {
return res;
}
return fetch(event.request);
} else {
return fetch(request).then(function(res) {
if (res) {
return caches.open(version).then(function(cache) {
return cache.put(request, res.clone()).then(function() {
return res;
});
});
}
});
}
})
.catch(function(err) {
console.log("error", error);
})
);
});