Javascript 为什么Safari会向服务人员发送两次图像请求?
就我的测试而言,这似乎是Safari唯一的问题。我已经创建了一个服务工作者,它缓存一组页面以供脱机/回退使用。在具有清除缓存的全新导航上,所有功能都将按预期工作。服务人员将安装、缓存必要的页面、激活并开始侦听获取事件。这就是事情变得奇怪的时候。如果我随后刷新页面或导航到站点上的任何其他页面,服务人员将按预期截获请求,但图像文件(在我的情况下是GIF)将被请求两次。那就更奇怪了。关闭站点,然后导航回站点,重复图像请求的问题将得到解决。然而,仍然出乎意料的是,服务人员将拦截文档(该部分是预期的),但其他所有内容(图像、JS、CSS)都将从内存缓存中提供。我没有体验过Chrome或Mozilla的那种行为 swCache.jsJavascript 为什么Safari会向服务人员发送两次图像请求?,javascript,php,safari,service-worker,Javascript,Php,Safari,Service Worker,就我的测试而言,这似乎是Safari唯一的问题。我已经创建了一个服务工作者,它缓存一组页面以供脱机/回退使用。在具有清除缓存的全新导航上,所有功能都将按预期工作。服务人员将安装、缓存必要的页面、激活并开始侦听获取事件。这就是事情变得奇怪的时候。如果我随后刷新页面或导航到站点上的任何其他页面,服务人员将按预期截获请求,但图像文件(在我的情况下是GIF)将被请求两次。那就更奇怪了。关闭站点,然后导航回站点,重复图像请求的问题将得到解决。然而,仍然出乎意料的是,服务人员将拦截文档(该部分是预期的),但
var cacheName = 'v1';
var fallback = {'/':'/index_OFFLINE.php', '/index.php':'/index_OFFLINE.php', '/reportgenerator':'/reportgenerator/index_OFFLINE.php', '/reportgenerator/':'/reportgenerator/index_OFFLINE.php', '/reportgenerator/1DayDataEntry.php':'/reportgenerator/1DayDataEntry_OFFLINE.php'};
var cacheAssets = ['/login.php', '/index_OFFLINE.php', '/reportgenerator/index_OFFLINE.php', '/reportgenerator/1DayDataEntry_OFFLINE.php', '/the_pump_new.gif', '/The-Pump.gif', '/thePumpCloud.gif', '/the_pump_new.ico', '/h2osoftwarestyle.css', '/h2osoftwareJS.js'];
self.addEventListener('install', e => {
console.log('Service Worker Installed');
e.waitUntil(
caches.open(cacheName)
.then(cache => {
console.log('Service Worker Caching Files');
cache.addAll(cacheAssets);
})
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', e => {
console.log('Service Worker Activated');
e.waitUntil(
caches.keys()
.then(cacheNames =>{
return Promise.all(
cacheNames.map(cache => {
if(cache != cacheName){
console.log('Service Worker Clearing Old Cache');
return caches.delete(cache);
}
})
);
})
);
});
self.addEventListener('fetch', e => {
console.log('Service Worker Fetching');
e.respondWith(
fetch(e.request)
.catch(() => {
var matchReq;
var swReqURL = new URL(e.request.url);
var swReqURLPath = swReqURL.pathname;
if(cacheAssets.includes(swReqURLPath)){
matchReq = e.request;
}else if(fallback[swReqURLPath]){
matchReq = fallback[swReqURLPath];
}else{
matchReq = '/index_OFFLINE.php';
}
return caches.match(matchReq);
})
);
});
任何帮助都将不胜感激