Skip to content

Commit

Permalink
feat: extending the lifecycle of background tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed Oct 19, 2024
1 parent 9cd10bd commit c214f54
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-ravens-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-widget/shared-cache": minor
---

The `waitUntil` option can be used to extend the life of background tasks.
16 changes: 7 additions & 9 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ import {

export class SharedCache implements Cache {
#cacheKeyGenerator: (request: Request) => Promise<string>;
#fetch?: typeof fetch;
#logger?: Logger;
#storage: KVStorage;
#waitUntil: (promise: Promise<unknown>) => void;

constructor(storage: KVStorage, options?: SharedCacheOptions) {
if (!storage) {
throw TypeError('Missing storage.');
}

const resolveOptions = {
async waitUntil(promise: Promise<unknown>) {
await promise.catch(console.error);
},
...options,
};

Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand All @@ -182,9 +175,14 @@ export class SharedCache implements Cache {
if (!fetch) {
return;
} else if (stale && policy.useStaleWhileRevalidate()) {
const waitUntil =
options?._waitUntil ??
((promise: Promise<unknown>) => {
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,
{
Expand Down
5 changes: 3 additions & 2 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 3 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ export interface SharedCacheOptions {
*/
cacheKeyPartDefiners?: SharedCacheKeyPartDefiners;

waitUntil?: (promise: Promise<unknown>) => void;

/**
* Method to initiate a request after cache expiration.
*/
fetch?: typeof fetch;

/**
* Custom logger.
*/
Expand Down Expand Up @@ -79,6 +72,7 @@ export interface SharedCacheRequestInitProperties {
ignoreRequestCacheControl?: boolean;
ignoreVary?: boolean;
varyOverride?: string;
waitUntil?: (promise: Promise<unknown>) => void;
}

declare global {
Expand All @@ -93,5 +87,7 @@ declare global {
_ignoreRequestCacheControl?: boolean;
/** @private */
_fetch?: typeof fetch;
/** @private */
_waitUntil?: (promise: Promise<unknown>) => void;
}
}

0 comments on commit c214f54

Please sign in to comment.