From 573d05474833bd057d4d34bf9dfbfc84f447da40 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Thu, 28 Nov 2024 09:03:35 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(SW)=20change=20strategy=20ht?= =?UTF-8?q?ml=20caching?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We will use the network first strategy for the html files. This will allow us to always have the latest version of the html files. --- CHANGELOG.md | 4 ++++ .../features/service-worker/service-worker.ts | 22 ++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f1d3d78..80dac384 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to ## [Unreleased] +## Changed + +♻️(SW) change strategy html caching #460 + ## [1.8.1] - 2024-11-27 diff --git a/src/frontend/apps/impress/src/features/service-worker/service-worker.ts b/src/frontend/apps/impress/src/features/service-worker/service-worker.ts index 94b9c37c..3beaada7 100644 --- a/src/frontend/apps/impress/src/features/service-worker/service-worker.ts +++ b/src/frontend/apps/impress/src/features/service-worker/service-worker.ts @@ -47,9 +47,13 @@ setCacheNameDetails({ const getStrategy = ( options?: NetworkFirstOptions | StrategyOptions, ): NetworkFirst | CacheFirst => { - return SW_DEV_URL.some((devDomain) => + const isDev = SW_DEV_URL.some((devDomain) => self.location.origin.includes(devDomain), - ) || isApiUrl(self.location.href) + ); + const isApi = isApiUrl(self.location.href); + const isHTMLRequest = options?.cacheName?.includes('html'); + + return isDev || isApi || isHTMLRequest ? new NetworkFirst(options) : new CacheFirst(options); }; @@ -77,7 +81,7 @@ self.addEventListener('activate', function (event) { }), ); }) - .then(void self.clients.claim()), + .then(() => self.clients.claim()), ); }); @@ -139,6 +143,18 @@ setCatchHandler(async ({ request, url, event }) => { } }); +// HTML documents +registerRoute( + ({ request }) => request.destination === 'document', + new NetworkFirst({ + cacheName: getCacheNameVersion('html'), + plugins: [ + new CacheableResponsePlugin({ statuses: [0, 200] }), + new ExpirationPlugin({ maxAgeSeconds: 24 * 60 * 60 * DAYS_EXP }), + ], + }), +); + /** * External urls cache strategy */