【发布时间】:2024-04-25 00:30:01
【问题描述】:
我有一个无法脱机工作的 PWA。在线它工作得很好,但是当我切换到离线时它什么也没做。我怀疑这与我的 service-worker.js 文件有关 -
var dataCacheName = 'sigmaApp';
var cacheName = 'sigmaApp';
var filesToCache = [
"/",
"./images/icons/icon-128x128.png",
"./images/icons/icon-144x144.png",
"./images/icons/icon-152x152.png",
"./images/icons/icon-192x192.png",
"./images/icons/icon-256x256.png",
"./images/create-how-boxes.png",
"./images/create-how-lines.png",
"./images/create-what-logo-1.png",
"./images/create-what-logo-2.png",
"./images/create-who-telkomsel.png",
"./images/create-who-volvo.png",
"./images/create-who-windstream.png",
"./images/create-why-hand.png",
"./images/create-why-rock.png",
"./images/deliver-what-logo-1.png",
"./images/deliver-what-logo-2.png",
"./images/deliver-why-road-orange.png",
"./images/deliver-why-roads.png",
"./images/sell-how-phone.png",
"./images/sell-how-sphere.png",
"./images/sell-what-logo-1.png",
"./images/sell-what-logo-1.png",
"./images/sell-what-logo-2.png",
"./images/sell-why-block.png",
"./images/sell-why-hand.png",
"./images/telkomsel-logo.png",
"./scripts",
"./index.html",
"./manifest.json",
"./scripts/jquery-3.3.1.js",
"./scripts/swiper.min.js",
"./scripts/swiper.min.js.map",
"./scripts/tube-animation.min.js",
"./scripts/typeit.min.js",
"./scripts/scripts.js",
"./service-worker.js",
"./styles",
"./styles/style.min.css",
"./videos/create-what-infographic-1.mp4",
"./scripts/swiper.min.js.map"
];
self.addEventListener('install', function(e) {
console.log('[ServiceWorker] Install');
e.waitUntil(
caches.open(cacheName).then(function(cache) {
console.log('[ServiceWorker] Caching app shell');
return cache.addAll(filesToCache);
})
);
});
self.addEventListener('activate', function(e) {
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== cacheName && key !== dataCacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
self.addEventListener('fetch', function(e) {
console.log('[Service Worker] Fetch', e.request.url);
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request);
})
);
});
我的页面似乎出错了 - Uncaught (in promise) TypeError: Request failed in my service-worker.js file in my install event listener。我觉得它正在通过 filesToCache 数组并在那里出错。
有人知道我做错了什么吗?
【问题讨论】:
-
如果您使用的是 Chrome,请转到 DevTools 的网络选项卡并重新加载您的页面。您将看到失败的请求。
-
这是在远程服务器上吗?可以分享网址吗?您是否使用 Chrom Lighthouse 工具检查过错误?
-
@Mathias 不,这是在实时服务器上 - silly-kilby-fbefb5.netlify.com。 Lighthouse 只是一直说服务工作者没有被注册,但在控制台中它说它已经注册。
-
@abadalyan - 在我的网络选项卡中,它说文件夹样式和脚本是 404 的。但它们都存在 - silly-kilby-fbefb5.netlify.com
标签: javascript jquery html service-worker progressive-web-apps