Fixes for SW caching

This commit is contained in:
Lim Chee Aun 2024-09-17 17:35:01 +08:00
parent efb42ca911
commit f3345ad9f8

View file

@ -16,13 +16,17 @@ const assetsRoute = new Route(
({ request, sameOrigin }) => { ({ request, sameOrigin }) => {
const isAsset = const isAsset =
request.destination === 'style' || request.destination === 'script'; request.destination === 'style' || request.destination === 'script';
const hasHash = /-[0-9a-f]{4,}\./i.test(request.url); const hasHash = /-[0-9a-z-]{4,}\./i.test(request.url);
return sameOrigin && isAsset && hasHash; return sameOrigin && isAsset && hasHash;
}, },
new NetworkFirst({ new NetworkFirst({
cacheName: 'assets', cacheName: 'assets',
networkTimeoutSeconds: 5, networkTimeoutSeconds: 5,
plugins: [ plugins: [
new ExpirationPlugin({
maxEntries: 30,
purgeOnQuotaError: true,
}),
new CacheableResponsePlugin({ new CacheableResponsePlugin({
statuses: [0, 200], statuses: [0, 200],
}), }),
@ -44,7 +48,7 @@ const imageRoute = new Route(
cacheName: 'remote-images', cacheName: 'remote-images',
plugins: [ plugins: [
new ExpirationPlugin({ new ExpirationPlugin({
maxEntries: 50, maxEntries: 30,
purgeOnQuotaError: true, purgeOnQuotaError: true,
}), }),
new CacheableResponsePlugin({ new CacheableResponsePlugin({
@ -86,7 +90,7 @@ const apiExtendedRoute = new RegExpRoute(
cacheName: 'api-extended', cacheName: 'api-extended',
plugins: [ plugins: [
new ExpirationPlugin({ new ExpirationPlugin({
maxAgeSeconds: 24 * 60 * 60, // 1 day maxAgeSeconds: 12 * 60 * 60, // 12 hours
purgeOnQuotaError: true, purgeOnQuotaError: true,
}), }),
new CacheableResponsePlugin({ new CacheableResponsePlugin({
@ -128,6 +132,7 @@ const apiRoute = new RegExpRoute(
networkTimeoutSeconds: 5, networkTimeoutSeconds: 5,
plugins: [ plugins: [
new ExpirationPlugin({ new ExpirationPlugin({
maxEntries: 30,
maxAgeSeconds: 5 * 60, // 5 minutes maxAgeSeconds: 5 * 60, // 5 minutes
purgeOnQuotaError: true, purgeOnQuotaError: true,
}), }),