♻️(SW) change strategy html caching

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.
This commit is contained in:
Anthony LC
2024-11-28 09:03:35 +01:00
committed by Anthony LC
parent 2035a256f5
commit 573d054748
2 changed files with 23 additions and 3 deletions

View File

@@ -9,6 +9,10 @@ and this project adheres to
## [Unreleased]
## Changed
♻️(SW) change strategy html caching #460
## [1.8.1] - 2024-11-27

View File

@@ -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
*/