From c214f54e316e46b900f86aa645246d99b7027d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E9=A5=BC?= Date: Sat, 19 Oct 2024 18:45:51 +0800 Subject: [PATCH] feat: extending the lifecycle of background tasks --- .changeset/empty-ravens-jump.md | 5 +++++ src/cache.ts | 16 +++++++--------- src/fetch.ts | 5 +++-- src/types.ts | 10 +++------- 4 files changed, 18 insertions(+), 18 deletions(-) create mode 100644 .changeset/empty-ravens-jump.md diff --git a/.changeset/empty-ravens-jump.md b/.changeset/empty-ravens-jump.md new file mode 100644 index 0000000..21ee393 --- /dev/null +++ b/.changeset/empty-ravens-jump.md @@ -0,0 +1,5 @@ +--- +"@web-widget/shared-cache": minor +--- + +The `waitUntil` option can be used to extend the life of background tasks. diff --git a/src/cache.ts b/src/cache.ts index b526f8b..ea27d17 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -24,10 +24,8 @@ import { export class SharedCache implements Cache { #cacheKeyGenerator: (request: Request) => Promise; - #fetch?: typeof fetch; #logger?: Logger; #storage: KVStorage; - #waitUntil: (promise: Promise) => void; constructor(storage: KVStorage, options?: SharedCacheOptions) { if (!storage) { @@ -35,9 +33,6 @@ export class SharedCache implements Cache { } const resolveOptions = { - async waitUntil(promise: Promise) { - await promise.catch(console.error); - }, ...options, }; @@ -51,10 +46,8 @@ export class SharedCache implements Cache { ...resolveOptions.cacheKeyRules, ...request.sharedCache?.cacheKeyRules, }); - this.#fetch = resolveOptions.fetch; this.#logger = resolveOptions.logger; this.#storage = storage; - this.#waitUntil = resolveOptions.waitUntil; } /** @private */ @@ -158,7 +151,7 @@ export class SharedCache implements Cache { return; } - const fetch = options?._fetch ?? this.#fetch; + const fetch = options?._fetch; const policy = CachePolicy.fromObject(cacheItem.policy); const { body, status, statusText } = cacheItem.response; @@ -182,9 +175,14 @@ export class SharedCache implements Cache { if (!fetch) { return; } else if (stale && policy.useStaleWhileRevalidate()) { + const waitUntil = + options?._waitUntil ?? + ((promise: Promise) => { + promise.catch(this.#logger?.error); + }); // Well actually, in this case it's fine to return the stale response. // But we'll update the cache in the background. - this.#waitUntil( + waitUntil( this.#revalidate( r, { diff --git a/src/fetch.ts b/src/fetch.ts index 64f111f..6521af3 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -52,9 +52,10 @@ export function createSharedCacheFetch( } const cachedResponse = await cache.match(request, { - ignoreMethod: request.method === 'HEAD', - _ignoreRequestCacheControl: sharedCacheOptions.ignoreRequestCacheControl, _fetch: interceptor, + _ignoreRequestCacheControl: sharedCacheOptions.ignoreRequestCacheControl, + _waitUntil: sharedCacheOptions.waitUntil, + ignoreMethod: request.method === 'HEAD', }); if (cachedResponse) { diff --git a/src/types.ts b/src/types.ts index d2a4ca3..005dcd1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,13 +22,6 @@ export interface SharedCacheOptions { */ cacheKeyPartDefiners?: SharedCacheKeyPartDefiners; - waitUntil?: (promise: Promise) => void; - - /** - * Method to initiate a request after cache expiration. - */ - fetch?: typeof fetch; - /** * Custom logger. */ @@ -79,6 +72,7 @@ export interface SharedCacheRequestInitProperties { ignoreRequestCacheControl?: boolean; ignoreVary?: boolean; varyOverride?: string; + waitUntil?: (promise: Promise) => void; } declare global { @@ -93,5 +87,7 @@ declare global { _ignoreRequestCacheControl?: boolean; /** @private */ _fetch?: typeof fetch; + /** @private */ + _waitUntil?: (promise: Promise) => void; } }