Skip to content

Commit 98de4dd

Browse files
authored
updated caching
1 parent 8410cc2 commit 98de4dd

1 file changed

Lines changed: 17 additions & 29 deletions

File tree

src/sw.js

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,30 @@ const otherManifest = [
2525
url: "/app-shell/index.html",
2626
},
2727
];
28-
const manifestURLs = [...manifest, ...otherManifest].map((entry) => {
29-
const url = new URL(entry.url, self.location);
30-
return url.href;
31-
});
28+
const EXCLUDED_PRECACHE_URLS = new Set([
29+
new URL("/icon_512x512.png", self.location).href,
30+
]);
31+
const manifestURLs = [...manifest, ...otherManifest]
32+
.map((entry) => new URL(entry.url, self.location).href)
33+
.filter((href) => !EXCLUDED_PRECACHE_URLS.has(href));
34+
3235
self.addEventListener("install", (event) => {
3336
event.waitUntil(
34-
(async () => {
35-
const cache = await caches.open(cacheName);
36-
37-
await Promise.all(
38-
manifestURLs.map(async (url) => {
39-
try {
40-
const response = await fetch(url, { cache: "no-store" });
41-
if (response && response.ok) {
42-
await cache.put(url, response);
43-
}
44-
} catch (err) {
45-
console.warn("[sw] Failed to precache:", url, err);
46-
}
47-
}),
48-
);
49-
})(),
37+
caches.open(cacheName).then((cache) => cache.addAll(manifestURLs))
5038
);
5139
});
5240
self.addEventListener("activate", (event) => {
5341
event.waitUntil(
54-
caches.open(cacheName).then((cache) =>
42+
(async () => {
43+
const cache = await caches.open(cacheName);
5544
// clean up those who are not listed in manifestURLs
56-
cache.keys().then((keys) => {
57-
for (const request of keys) {
58-
if (!manifestURLs.includes(request.url)) {
59-
cache.delete(request);
60-
}
61-
}
62-
}),
63-
),
45+
const keys = await cache.keys();
46+
await Promise.all(
47+
keys.map((request) =>
48+
manifestURLs.includes(request.url) ? Promise.resolve() : cache.delete(request),
49+
),
50+
);
51+
}) (),
6452
);
6553
});
6654
registerRoute(

0 commit comments

Comments
 (0)