Skip to content

Commit 5c065e2

Browse files
committed
feat: Add cache for translations
1 parent 3d1434a commit 5c065e2

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

public/service-worker.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* eslint-env serviceworker */
22
/* eslint-disable no-restricted-globals */
33

4-
// "editor-app-v1" is replaced with the package version at build time (see webpack.config.js)
4+
// "editor-app-v1" and "editor-translations-v1" are replaced with the package version at build time (see webpack.config.js)
55
const APP_CACHE = "editor-app-v1";
6+
const TRANSLATIONS_CACHE = "editor-translations-v1";
67
const PYODIDE_CACHE = "pyodide-v0.26.2";
78

89
// Minimal set of assets to pre-cache on install
@@ -37,7 +38,7 @@ self.addEventListener("activate", (event) => {
3738
caches.keys().then((keys) =>
3839
Promise.all(
3940
keys
40-
.filter((key) => key !== APP_CACHE && key !== PYODIDE_CACHE)
41+
.filter((key) => key !== APP_CACHE && key !== TRANSLATIONS_CACHE && key !== PYODIDE_CACHE)
4142
.map((key) => {
4243
console.log("[SW] Deleting old cache:", key);
4344
return caches.delete(key);
@@ -123,6 +124,12 @@ self.addEventListener("fetch", (event) => {
123124
return;
124125
}
125126

127+
// Translation files get their own cache so they can be evicted independently of the app shell
128+
if (url.origin === self.location.origin && url.pathname.includes("/translations/")) {
129+
event.respondWith(networkFirst(event.request, TRANSLATIONS_CACHE));
130+
return;
131+
}
132+
126133
// Same-origin app assets are network-first so users get fresh content online
127134
if (url.origin === self.location.origin) {
128135
event.respondWith(networkFirst(event.request, APP_CACHE));

webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ const mainConfig = {
243243
const version = process.env.npm_package_version;
244244
return content
245245
.toString()
246-
.replace("editor-app-v1", `editor-app-v${version}`);
246+
.replace("editor-app-v1", `editor-app-v${version}`)
247+
.replace("editor-translations-v1", `editor-translations-v${version}`);
247248
},
248249
},
249250
{ from: "src/projects", to: "projects" },

0 commit comments

Comments
 (0)