Skip to content
Myr

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

2 files+40 added−1 removed

modified:src/middleware/cache/index.test.ts+31 added
414 unmodified lines
415 expect(res).not.toBeNull()416 expect(res.status).toBe(200)417 expect(res.headers.get('cache-control')).toBe(null)418 })419420 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 })433434 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})451452describe('Cache Skipping Logic', () => {453 let putSpy: ReturnType<typeof vi.fn>
80 unmodified lines
modified:src/middleware/cache/index.ts+9 added-1 removed