Skip to content
21 changes: 13 additions & 8 deletions src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
NetworkOnly,
StaleWhileRevalidate,
} from "workbox-strategies";
/* eslint-enable import/no-extraneous-dependencies */

const cacheName = cacheNames.runtime;

Expand All @@ -25,16 +24,23 @@ const otherManifest = [
url: "/app-shell/index.html",
},
];
const manifestURLs = [...manifest, ...otherManifest].map((entry) => {
const url = new URL(entry.url, self.location);
return url.href;
});
const manifestURLs = [
...new Set(
[...manifest, ...otherManifest].map((entry) => {
const url = new URL(entry.url, self.location);
return url.href;
}),
),
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(cacheName).then((cache) => cache.addAll(manifestURLs)),
(async () => {
const cache = await caches.open(cacheName);

await Promise.allSettled(manifestURLs.map((url) => cache.add(url)));
})(),
);
});

self.addEventListener("activate", (event) => {
// - [x] clean up outdated runtime cache
event.waitUntil(
Expand All @@ -52,7 +58,6 @@ self.addEventListener("activate", (event) => {
),
);
});

registerRoute(
({ url }) => manifestURLs.includes(url.href),
new NetworkFirst({
Expand Down
Loading