Skip to content
23 changes: 14 additions & 9 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(
Comment thread
ovflowd marked this conversation as resolved.
caches.open(cacheName).then((cache) => cache.addAll(manifestURLs)),
(async () => {
const cache = await caches.open(cacheName);

await Promise.allSettled(manifestURLs.map((url) => cache.add(url)));
})(),
Comment thread
ovflowd marked this conversation as resolved.
);
});

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 Expand Up @@ -87,6 +92,6 @@ setCatchHandler(({ event }) => {
case "document":
return caches.match("/app-shell/index.html");
default:
return Response.error();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont remove the error handling please

return fetch(event.request);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this is smart. Lets say something is wrong with your computer and it doesnt have a good network. You will always re-fetch until it suceeds. will be very expensive an circular-loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the fetch(event.request) fallback.
I also removed the non-essential 512x512 favicon that was causing the Firefox error, tested locally and the service worker now installs and runs cleanly.
Please review

}
});
Loading