Skip to content

Commit

Permalink
♻️(SW) change strategy html caching
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AntoLC committed Nov 28, 2024
1 parent 2035a25 commit 573d054
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to

## [Unreleased]

## Changed

♻️(SW) change strategy html caching #460


## [1.8.1] - 2024-11-27

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -77,7 +81,7 @@ self.addEventListener('activate', function (event) {
}),
);
})
.then(void self.clients.claim()),
.then(() => self.clients.claim()),
);
});

Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 573d054

Please sign in to comment.