Skip to content

Commit

Permalink
feat: support custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed May 27, 2024
1 parent ed8530d commit 187df41
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-mirrors-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-widget/shared-cache": minor
---

Support custom logger.
36 changes: 27 additions & 9 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
CacheItem,
PolicyResponse,
SharedCacheStatus,
Logger,
} from './types';
import { createCacheKeyGenerator, vary as getVary } from './cache-key';
import type { SharedCacheKeyRules, FilterOptions } from './cache-key';
Expand All @@ -20,14 +21,15 @@ import {
const ORIGINAL_FETCH = globalThis.fetch;

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

constructor(storage: KVStorage, options?: SharedCacheOptions) {
if (!storage) {
Expand All @@ -41,14 +43,15 @@ export class SharedCache implements Cache {
...options,
};

this.#storage = storage;
this.#waitUntil = resolveOptions.waitUntil;
this.#fetch = resolveOptions.fetch ?? ORIGINAL_FETCH;
this.#cacheKeyRules = resolveOptions.cacheKeyRules;
this.#cacheKeyGenerator = createCacheKeyGenerator(
resolveOptions._cacheName,
resolveOptions.cacheKeyPartDefiners
);
this.#cacheKeyRules = resolveOptions.cacheKeyRules;
this.#fetch = resolveOptions.fetch ?? ORIGINAL_FETCH;
this.#logger = resolveOptions.logger;
this.#storage = storage;
this.#waitUntil = resolveOptions.waitUntil;
}

/** @private */
Expand Down Expand Up @@ -221,7 +224,15 @@ export class SharedCache implements Cache {
response: Response,
options?: SharedCacheQueryOptions
): Promise<void> {
return this.#putWithCustomCacheKey(request, response, options);
return this.#putWithCustomCacheKey(request, response, options).catch(
(error) => {
this.#logger?.error('Failed to cache response.', {
url: request instanceof Request ? request.url : request,
error,
});
throw error;
}
);
}

async #putWithCustomCacheKey(
Expand Down Expand Up @@ -338,6 +349,13 @@ export class SharedCache implements Cache {
);
}

if (revalidationResponse.status >= 500) {
this.#logger?.error(`Revalidation failed.`, {
url: request.url,
status: revalidationResponse.status,
});
}

const { modified, policy: revalidatedPolicy } =
resolveCacheItem.policy.revalidatedPolicy(
revalidationRequest,
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export type SharedCacheOptions = {
* @default globalThis.fetch
*/
fetch?: typeof fetch;

/**
* Custom logger.
*/
logger?: Logger;
};

export type Logger = {
info(message?: any, ...optionalParams: any[]): void;
error(message?: any, ...optionalParams: any[]): void;
};

export type KVStorage = {
Expand Down

0 comments on commit 187df41

Please sign in to comment.