gpt4 book ai didi

ios - Service Worker FechtEvent.respondWith 响应在 iOS 12.1 Safari 上为空

转载 作者:行者123 更新时间:2023-12-01 15:36:41 26 4
gpt4 key购买 nike

我可以在 Android Chrome、macOS Chrome 以及 Safari 和 Windows Chrome 上使用 service Worker 下载网站以供离线使用。当我尝试将网站下载到 时iOS 12.1 Safari 它首先起作用。但是当我关闭 Safari,离线并重新打开 Safari 时,我得到以下信息 错误信息 :

Safari can't open the Site.

Error: "FetchEvent.respondWith received an error: TypeError: Thereseems to be no connection to the Internet."

==== AND in the console ====

FetchEvent.respondWith received an error: Returned response is null


您可以在下面看到文本形式的脚本。不幸的是,我几乎无法报告有关该问题的任何信息,因为我不了解它并希望有一些知识渊博的人:)
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Offline App</h1>
</body>
<script>
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js').then(function (registration) {
console.log('Service Worker Registered');
});
}
</script>
</html>
sw.js
/*
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

importScripts('cache-polyfill.js');

var CACHE_VERSION = 1;
var CURRENT_CACHES = {
prefetch: 'prefetch-cache-v' + CACHE_VERSION
};

self.addEventListener('install', function(event) {
var now = Date.now();

var urlsToPrefetch = [
'/pwa/index.html'
];

console.log('Handling install event. Resources to prefetch:', urlsToPrefetch);

event.waitUntil(
caches.open(CURRENT_CACHES.prefetch).then(function(cache) {
var cachePromises = urlsToPrefetch.map(function(urlToPrefetch) {
var url = new URL(urlToPrefetch, location.href);
url.search += (url.search ? '&' : '?') + 'cache-bust=' + now;

var request = new Request(url, {mode: 'no-cors'});
return fetch(request).then(function(response) {
if (response.status >= 400) {
throw new Error('request for ' + urlToPrefetch +
' failed with status ' + response.statusText);
}

return cache.put(urlToPrefetch, response);
}).catch(function(error) {
console.error('Not caching ' + urlToPrefetch + ' due to ' + error);
});
});

return Promise.all(cachePromises).then(function() {
console.log('Pre-fetching complete.');
});
}).catch(function(error) {
console.error('Pre-fetching failed:', error);
})
);
});

self.addEventListener('activate', function(event) {
var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) {
return CURRENT_CACHES[key];
});

event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (expectedCacheNames.indexOf(cacheName) === -1) {
console.log('Deleting out of date cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
});

self.addEventListener('fetch', function(event) {
if (!navigator.onLine) {

event.respondWith(
caches.match(event.request).then(function (response) {
if (response) {

return response;
}

console.log('No response found in cache. About to fetch from network...');

return fetch(event.request).then(function (response) {
console.log('Response from network is:', response);

return response;
}).catch(function (error) {
console.error('Fetching failed:', error);
throw error;
});
})
);
}
});

最佳答案

清除 Web 缓存后,它对我来说工作正常......

关于ios - Service Worker FechtEvent.respondWith 响应在 iOS 12.1 Safari 上为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53431195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com