service-worker: use Stale-While-Revalidate strategy

This commit is contained in:
vchikalkin 2024-04-23 22:19:05 +03:00
parent 678b55ecdf
commit e3d691bac3
4 changed files with 18 additions and 9 deletions

View File

@ -66,6 +66,7 @@
"ts-jest": "^29.0.5", "ts-jest": "^29.0.5",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"typescript": "^5.3.3", "typescript": "^5.3.3",
"workbox-cacheable-response": "^7.0.0",
"workbox-core": "^7.0.0", "workbox-core": "^7.0.0",
"workbox-expiration": "^7.0.0", "workbox-expiration": "^7.0.0",
"workbox-routing": "^7.0.0", "workbox-routing": "^7.0.0",

File diff suppressed because one or more lines are too long

View File

@ -1,11 +1,11 @@
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
import { clientsClaim } from 'workbox-core'; import { clientsClaim } from 'workbox-core';
import { ExpirationPlugin } from 'workbox-expiration';
import { registerRoute } from 'workbox-routing'; import { registerRoute } from 'workbox-routing';
import { CacheFirst } from 'workbox-strategies'; import { StaleWhileRevalidate } from 'workbox-strategies';
declare let self: ServiceWorkerGlobalScope; declare let self: ServiceWorkerGlobalScope;
const cacheName = 'my-cache'; const cacheName = 'pages-cache';
self.addEventListener('install', () => { self.addEventListener('install', () => {
self.skipWaiting(); self.skipWaiting();
@ -14,13 +14,12 @@ self.addEventListener('install', () => {
clientsClaim(); clientsClaim();
registerRoute( registerRoute(
/\/(unlimited)?$/u, ({ request }) => request.destination === 'document',
new CacheFirst({ new StaleWhileRevalidate({
cacheName, cacheName,
plugins: [ plugins: [
new ExpirationPlugin({ new CacheableResponsePlugin({
maxAgeSeconds: 1 * 60, statuses: [200],
maxEntries: 8,
}), }),
], ],
}) })

9
pnpm-lock.yaml generated
View File

@ -268,6 +268,9 @@ importers:
typescript: typescript:
specifier: ^5.3.3 specifier: ^5.3.3
version: 5.3.3 version: 5.3.3
workbox-cacheable-response:
specifier: ^7.0.0
version: 7.0.0
workbox-core: workbox-core:
specifier: ^7.0.0 specifier: ^7.0.0
version: 7.0.0 version: 7.0.0
@ -14273,6 +14276,12 @@ packages:
dependencies: dependencies:
isexe: 2.0.0 isexe: 2.0.0
/workbox-cacheable-response@7.0.0:
resolution: {integrity: sha512-0lrtyGHn/LH8kKAJVOQfSu3/80WDc9Ma8ng0p2i/5HuUndGttH+mGMSvOskjOdFImLs2XZIimErp7tSOPmu/6g==}
dependencies:
workbox-core: 7.0.0
dev: true
/workbox-core@7.0.0: /workbox-core@7.0.0:
resolution: {integrity: sha512-81JkAAZtfVP8darBpfRTovHg8DGAVrKFgHpOArZbdFd78VqHr5Iw65f2guwjE2NlCFbPFDoez3D3/6ZvhI/rwQ==} resolution: {integrity: sha512-81JkAAZtfVP8darBpfRTovHg8DGAVrKFgHpOArZbdFd78VqHr5Iw65f2guwjE2NlCFbPFDoez3D3/6ZvhI/rwQ==}
dev: true dev: true