45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
const CACHE_NAME = "polyweather-v1";
|
|||
|
|
const STATIC_ASSETS = ["/_next/", "/favicon"];
|
||
|
|
|
||
|
|
self.addEventListener("install", (event) => {
|
||
|
|
event.waitUntil(self.skipWaiting());
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener("activate", (event) => {
|
||
|
|
event.waitUntil(
|
||
|
|
caches.keys().then((keys) =>
|
||
|
|
Promise.all(
|
||
|
|
keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key)),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener("fetch", (event) => {
|
||
|
|
const url = new URL(event.request.url);
|
||
|
|
|
||
|
|
if (STATIC_ASSETS.some((prefix) => url.pathname.startsWith(prefix))) {
|
||
|
|
event.respondWith(
|
||
|
|
caches.open(CACHE_NAME).then((cache) =>
|
||
|
|
cache.match(event.request).then(
|
||
|
|
(cached) =>
|
||
|
|
cached ||
|
||
|
|
fetch(event.request).then((response) => {
|
||
|
|
if (response.ok) {
|
||
|
|
cache.put(event.request, response.clone());
|
||
|
|
}
|
||
|
|
return response;
|
||
|
|
}),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
event.respondWith(
|
||
|
|
fetch(event.request).catch(() =>
|
||
|
|
caches.match(event.request).then((cached) => cached || Response.error()),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
});
|