Skip to content
Myr

feat(cache): key cache entries by configured vary headers (#4915)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Taku Amano · committed by GitHub · parent 48a7ccb · Browse files · 2 files+227 added−41 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+227 added−41 removed

  • tests:+184 added−2 removed
  • other:+43 added−39 removed

2 files+227 added−41 removed

modified:src/middleware/cache/index.test.ts+184 added-2 removed
377 unmodified lines
378 })379})380381describe('Cache Skipping Logic', () => {382 const stubStoreBackedCache = () => {383 const store = new Map<string | Request, Response>()384 const mockCache = {385 match: vi.fn((key: string | Request) => Promise.resolve(store.get(key)?.clone())),386 put: vi.fn((key: string | Request, response: Response) => {387 store.set(key, response.clone())388 return Promise.resolve()389 }),390 keys: vi.fn(() => Promise.resolve(Array.from(store.keys()))),391 }392393 vi.stubGlobal('caches', {394 open: vi.fn().mockResolvedValue(mockCache),395 })396397 return { mockCache }398 }399400 let putSpy: ReturnType<typeof vi.fn>401 let matchSpy: ReturnType<typeof vi.fn>402403 beforeEach(() => {
90 unmodified lines
494 expect(putSpy).not.toHaveBeenCalled()495 }496 )497480 it('Should set configured Vary header but not cache the response', async () => {498 it('Should set configured Vary header and cache by the configured request header value', async () => {499 const { mockCache } = stubStoreBackedCache()500 const app = new Hono()501 let count = 0502 app.use('*', cache({ cacheName: 'skip-test', wait: true, vary: 'Accept' }))503 app.get('/', (c) => {
4 unmodified lines
508509 await app.request('/')510 const res = await app.request('/')511 expect(res.headers.get('Vary')).toBe('accept')493 expect(res.headers.get('X-Count')).toBe('2')512 expect(res.headers.get('X-Count')).toBe('1')513 expect(mockCache.put).toHaveBeenCalledOnce()514 })515516 it('Should store separate variants for different Accept-Language request values', async () => {517 const { mockCache } = stubStoreBackedCache()518 const app = new Hono()519 let count = 0520 app.use('*', cache({ cacheName: 'vary-language-test', wait: true, vary: 'Accept-Language' }))521 app.get('/', (c) => {522 count++523 c.header('X-Count', `${count}`)524 return c.text(c.req.header('Accept-Language') ?? 'missing')525 })526527 await app.request('/', {528 headers: {529 'Accept-Language': 'en',530 },531 })532 const en = await app.request('/', {533 headers: {534 'Accept-Language': 'en',535 },536 })537 await app.request('/', {538 headers: {539 'Accept-Language': 'ja',540 },541 })542 const ja = await app.request('/', {543 headers: {544 'Accept-Language': 'ja',545 },546 })547548 expect(await en.text()).toBe('en')549 expect(en.headers.get('X-Count')).toBe('1')550 expect(await ja.text()).toBe('ja')551 expect(ja.headers.get('X-Count')).toBe('2')552 expect(mockCache.put).toHaveBeenCalledTimes(2)553 })554555 it('Should keep missing and present Vary request header values in separate variants', async () => {556 stubStoreBackedCache()557 const app = new Hono()558 let count = 0559 app.use('*', cache({ cacheName: 'vary-missing-test', wait: true, vary: 'Accept-Language' }))560 app.get('/', (c) => {561 count++562 c.header('X-Count', `${count}`)563 return c.text(c.req.header('Accept-Language') ?? 'missing')564 })565566 await app.request('/')567 await app.request('/', {568 headers: {569 'Accept-Language': 'en',570 },571 })572 const missing = await app.request('/')573 const present = await app.request('/', {574 headers: {575 'Accept-Language': 'en',576 },577 })578579 expect(await missing.text()).toBe('missing')580 expect(missing.headers.get('X-Count')).toBe('1')581 expect(await present.text()).toBe('en')582 expect(present.headers.get('X-Count')).toBe('2')583 })584585 it('Should store separate variants using multiple configured request headers', async () => {586 const { mockCache } = stubStoreBackedCache()587 const app = new Hono()588 let count = 0589 app.use(590 '*',591 cache({ cacheName: 'vary-multiple-test', wait: true, vary: ['Accept', 'Accept-Encoding'] })592 )593 app.get('/', (c) => {594 count++595 c.header('X-Count', `${count}`)596 return c.text(`${c.req.header('Accept')}:${c.req.header('Accept-Encoding')}`)597 })598599 await app.request('/', {600 headers: {601 Accept: 'text/plain',602 'Accept-Encoding': 'gzip',603 },604 })605 const same = await app.request('/', {606 headers: {607 Accept: 'text/plain',608 'Accept-Encoding': 'gzip',609 },610 })611 await app.request('/', {612 headers: {613 Accept: 'application/json',614 'Accept-Encoding': 'gzip',615 },616 })617 const differentAccept = await app.request('/', {618 headers: {619 Accept: 'application/json',620 'Accept-Encoding': 'gzip',621 },622 })623 await app.request('/', {624 headers: {625 Accept: 'text/plain',626 'Accept-Encoding': 'br',627 },628 })629 const differentEncoding = await app.request('/', {630 headers: {631 Accept: 'text/plain',632 'Accept-Encoding': 'br',633 },634 })635636 expect(same.headers.get('X-Count')).toBe('1')637 expect(differentAccept.headers.get('X-Count')).toBe('2')638 expect(differentEncoding.headers.get('X-Count')).toBe('3')639 expect(mockCache.put).toHaveBeenCalledTimes(3)640 })641642 it('Should store when the handler Vary header is covered by configured Vary headers', async () => {643 const { mockCache } = stubStoreBackedCache()644 const app = new Hono()645 app.use('*', cache({ cacheName: 'vary-covered-test', wait: true, vary: 'Accept-Language' }))646 app.get('/', (c) => {647 c.header('Vary', 'Accept-Language')648 return c.text('response')649 })650651 await app.request('/')652 expect(mockCache.put).toHaveBeenCalledOnce()653 })654655 it('Should not store when the handler adds Vary headers not covered by configured Vary headers', async () => {656 const app = new Hono()657 app.use('*', cache({ cacheName: 'skip-test', wait: true, vary: 'Accept-Language' }))658 app.get('/', (c) => {659 c.header('Vary', 'Accept-Language, Cookie')660 return c.text('response')661 })662663 await app.request('/')664 expect(putSpy).not.toHaveBeenCalled()665 })666667 it('Should not store Vary: * responses even when Vary headers are configured', async () => {668 const app = new Hono()669 app.use('*', cache({ cacheName: 'skip-test', wait: true, vary: 'Accept-Language' }))670 app.get('/', (c) => {671 c.header('Vary', '*')672 return c.text('response')673 })674675 await app.request('/')676 expect(putSpy).not.toHaveBeenCalled()677 })678679 it('Should bypass cache for Authorization requests', async () => {680 const app = new Hono()681 app.use('*', cache({ cacheName: 'skip-test', wait: true, cacheControl: 'max-age=10' }))682 app.get('/', (c) => {
38 unmodified lines
modified:src/middleware/cache/index.ts+43 added-39 removed