Skip to content

Commit 258809a

Browse files
committed
fix: cache more in the service worker
1 parent b790d53 commit 258809a

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

src/sw.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CacheableResponsePlugin } from "workbox-cacheable-response";
22
import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching";
33
import { Route, registerRoute } from "workbox-routing";
4-
import { CacheFirst } from "workbox-strategies";
4+
import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from "workbox-strategies";
55

66
declare let self: ServiceWorkerGlobalScope;
77

@@ -18,6 +18,48 @@ self.addEventListener("message", (event) => {
1818
}
1919
});
2020

21+
// Cache /config.json with NetworkFirst so the app works offline with the last
22+
// known config, but always gets a fresh copy when online.
23+
registerRoute(
24+
new Route(
25+
({ url }) => url.pathname === "/config.json",
26+
new NetworkFirst({
27+
cacheName: "runtime-config",
28+
plugins: [
29+
new CacheableResponsePlugin({ statuses: [200] }),
30+
],
31+
}),
32+
),
33+
);
34+
35+
// Cache sub-app config files with NetworkFirst — they are as dynamic as
36+
// /config.json and must be fresh when online.
37+
registerRoute(
38+
new Route(
39+
({ url }) => /^\/_services\/[^/]+\/app-config\.json$/.test(url.pathname),
40+
new NetworkFirst({
41+
cacheName: "app-configs",
42+
plugins: [
43+
new CacheableResponsePlugin({ statuses: [200] }),
44+
],
45+
}),
46+
),
47+
);
48+
49+
// Cache translations with StaleWhileRevalidate: serve from cache immediately
50+
// for fast loads, refresh in the background so the next visit gets fresh strings.
51+
registerRoute(
52+
new Route(
53+
({ url }) => url.hostname === "j26-translations.jamboree.se",
54+
new StaleWhileRevalidate({
55+
cacheName: "translations",
56+
plugins: [
57+
new CacheableResponsePlugin({ statuses: [200] }),
58+
],
59+
}),
60+
),
61+
);
62+
2163
// Aggressively cache all Tabler icons from jsDelivr, since they are versioned
2264
// and thus safe to cache for a long time. This is needed to make the icons work
2365
// offline, since they are imported dynamically in the app.

0 commit comments

Comments
 (0)