feat(cache): add `onCacheNotAvailable` option (#4876)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent f23e97b · Browse files · 2 files·+40 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+40 added−1 removed
- tests:+31 added−0 removed
- other:+9 added−1 removed
Provenance
- Commit
faa6c46- Tree
3b2cb70- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
f23e97b
2 files changed+40 added−1 removed
modified:src/middleware/cache/index.test.ts+31 added
414 unmodified lines415 expect(res).not.toBeNull()416 expect(res.status).toBe(200)417 expect(res.headers.get('cache-control')).toBe(null)418 })419+420+ it('Should call onCacheNotAvailable when caches is not defined', async () => {421+ vi.stubGlobal('caches', undefined)422+ const spy = vi.fn()423+ const app = new Hono()424+ app.use(cache({ cacheName: 'my-app-v1', onCacheNotAvailable: spy }))425+ app.get('/', (c) => {426+ return c.text('cached')427+ })428+ const res = await app.request('/')429+ expect(res.status).toBe(200)430+ expect(spy).toHaveBeenCalledOnce()431+ vi.unstubAllGlobals()432+ })433+434+ it('Should suppress default log when onCacheNotAvailable is false', async () => {435+ vi.stubGlobal('caches', undefined)436+ const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})437+ const app = new Hono()438+ app.use(cache({ cacheName: 'my-app-v1', onCacheNotAvailable: false }))439+ app.get('/', (c) => {440+ return c.text('cached')441+ })442+ const res = await app.request('/')443+ expect(res.status).toBe(200)444+ expect(logSpy).not.toHaveBeenCalledWith(445+ 'Cache Middleware is not enabled because caches is not defined.'446+ )447+ logSpy.mockRestore()448+ vi.unstubAllGlobals()449+ })450 })451 452 describe('Cache Skipping Logic', () => {453 let putSpy: ReturnType<typeof vi.fn>80 unmodified linesmodified:src/middleware/cache/index.ts+9 added-1 removed
43 unmodified lines44454647484950515213 unmodified lines6667686970717273747576777178798081828390 unmodified lines43 unmodified lines * @param {string} [options.cacheControl] - A string of directives for the `Cache-Control` header. * @param {string | string[]} [options.vary] - Sets the `Vary` header in the response. If the original response header already contains a `Vary` header, the values are merged, removing any duplicates. * @param {Function} [options.keyGenerator] - Generates keys for every request in the `cacheName` store. This can be used to cache data based on request parameters or context parameters. * @param {number[]} [options.cacheableStatusCodes=[200]] - An array of status codes that can be cached. * @param {Function | false} [options.onCacheNotAvailable] - A callback invoked when `globalThis.caches` is not available. By default, a message is logged to the console. Set to `false` to suppress the log, or provide a custom function. * @returns {MiddlewareHandler} The middleware handler function. * @throws {Error} If the `vary` option includes "*". * * @example13 unmodified lines cacheControl?: string vary?: string | string[] keyGenerator?: (c: Context) => Promise<string> | string cacheableStatusCodes?: StatusCode[] onCacheNotAvailable?: (() => void) | false}): MiddlewareHandler => { if (!globalThis.caches) { if (options.onCacheNotAvailable === false) { // suppress log } else if (options.onCacheNotAvailable) { options.onCacheNotAvailable() } else { console.log('Cache Middleware is not enabled because caches is not defined.') console.log('Cache Middleware is not enabled because caches is not defined.') } return async (_c, next) => await next() }
if (options.wait === undefined) {90 unmodified lines