diff --git a/package.json b/package.json index 7593c70..2b6bd5c 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,8 @@ "websocket-as-promised": "^2.0.1", "workbox-core": "^6.1.1", "workbox-precaching": "^6.1.1", - "workbox-routing": "^6.1.1" + "workbox-routing": "^6.1.1", + "workbox-strategies": "^6.1.1" }, "scripts": { "start": "craco start", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c40c5dc..e97392d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,6 +41,7 @@ workbox-core: 6.1.1 workbox-precaching: 6.1.1 workbox-routing: 6.1.1 + workbox-strategies: 6.1.1 devDependencies: '@craco/craco': 6.1.1_react-scripts@4.0.3 '@types/classnames': 2.2.11 @@ -13909,3 +13910,4 @@ workbox-core: ^6.1.1 workbox-precaching: ^6.1.1 workbox-routing: ^6.1.1 + workbox-strategies: ^6.1.1 diff --git a/src/service-worker.ts b/src/service-worker.ts index cb7896d..1e209ba 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -14,6 +14,7 @@ import { clientsClaim } from "workbox-core"; import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching"; import { registerRoute } from "workbox-routing"; +import { StaleWhileRevalidate } from "workbox-strategies"; declare const self: ServiceWorkerGlobalScope; @@ -25,6 +26,12 @@ // even if you decide not to use precaching. See https://cra.link/PWA precacheAndRoute(self.__WB_MANIFEST); +// Cache the locale files (currently the only preloading step) +registerRoute( + ({ url }) => url.pathname.startsWith("/locale/"), + new StaleWhileRevalidate({}) +); + // Set up App Shell-style routing, so that all navigation requests // are fulfilled with your index.html shell. Learn more at // https://developers.google.com/web/fundamentals/architecture/app-shell diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts index 83b3770..7f48db2 100644 --- a/src/utils/i18n.ts +++ b/src/utils/i18n.ts @@ -12,7 +12,10 @@ import JSON5 from "json5"; import languagesJson from "../__data__/languages.json"; -import packageJson from "../../package.json"; + +// Replaced by webpack DefinePlugin and git-revision-webpack-plugin +declare const __GIT_VERSION__: string; +const gitVersion: string = __GIT_VERSION__; export interface Language { name: string; @@ -75,7 +78,7 @@ }, backend: { - queryStringParams: { v: packageJson.version }, + queryStringParams: { v: encodeURIComponent(gitVersion) }, loadPath: "/locales/{{lng}}.json", // Translations now use JSON5 to allow for comments, newlines, and basic