Skip to content
Myr

fix(middleware/cache): skip caching when `Vary: *` is present (#4504)

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

* fix(middleware/cache): skip caching when Vary: * is present * refactor

authored by Komiya · committed by GitHub · parent 46ec300 · Browse files · 2 files+84 added−0 removed
This commit

State

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

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

2 files+84 added−0 removed

  • tests:+71 added−0 removed
  • other:+13 added−0 removed

2 files+84 added−0 removed

modified:src/middleware/cache/index.test.ts+71 added
168 unmodified lines
169 c.header('Vary', '*')170 return c.text('cached')171 })172173 let varyWildcardOnlyCount = 0174 app.use('/vary-wildcard/*', cache({ cacheName: 'vary-wildcard-test', wait: true }))175 app.get('/vary-wildcard/only', (c) => {176 varyWildcardOnlyCount++177 c.header('X-Count', `${varyWildcardOnlyCount}`)178 c.header('Vary', '*')179 return c.text('response')180 })181182 let varyWildcardFirstCount = 0183 app.get('/vary-wildcard/first', (c) => {184 varyWildcardFirstCount++185 c.header('X-Count', `${varyWildcardFirstCount}`)186 c.header('Vary', '*, Accept')187 return c.text('response')188 })189190 let varyWildcardMiddleCount = 0191 app.get('/vary-wildcard/middle', (c) => {192 varyWildcardMiddleCount++193 c.header('X-Count', `${varyWildcardMiddleCount}`)194 c.header('Vary', 'Accept, *')195 return c.text('response')196 })197198 let varyWildcardComplexCount = 0199 app.get('/vary-wildcard/complex', (c) => {200 varyWildcardComplexCount++201 c.header('X-Count', `${varyWildcardComplexCount}`)202 c.header('Vary', 'Accept, *, Accept-Encoding')203 return c.text('response')204 })205206 let varyWildcardSpacesCount = 0207 app.get('/vary-wildcard/spaces', (c) => {208 varyWildcardSpacesCount++209 c.header('X-Count', `${varyWildcardSpacesCount}`)210 c.header('Vary', ' * ')211 return c.text('response')212 })213214 app.use('/default/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))215 app.all('/default/:code/', (c) => {216 const code = parseInt(c.req.param('code'))217 // Intended to avoid the following error: `RangeError: init[“status”] must be in the range of 200 to 599, inclusive.`
114 unmodified lines
332 expect(() => cache({ cacheName: 'my-app-v1', wait: true, vary: ['*'] })).toThrow()333 expect(() => cache({ cacheName: 'my-app-v1', wait: true, vary: '*' })).toThrow()334 })335336 it('Should not cache response when Vary: * is set', async () => {337 await app.request('http://localhost/vary-wildcard/only')338 const res = await app.request('http://localhost/vary-wildcard/only')339 expect(res.headers.get('x-count')).toBe('2')340 })341342 it('Should not cache response when Vary: *, Accept is set', async () => {343 await app.request('http://localhost/vary-wildcard/first')344 const res = await app.request('http://localhost/vary-wildcard/first')345 expect(res.headers.get('x-count')).toBe('2')346 })347348 it('Should not cache response when Vary: Accept, * is set', async () => {349 await app.request('http://localhost/vary-wildcard/middle')350 const res = await app.request('http://localhost/vary-wildcard/middle')351 expect(res.headers.get('x-count')).toBe('2')352 })353354 it('Should not cache response when Vary: Accept, *, Accept-Encoding is set', async () => {355 await app.request('http://localhost/vary-wildcard/complex')356 const res = await app.request('http://localhost/vary-wildcard/complex')357 expect(res.headers.get('x-count')).toBe('2')358 })359360 it('Should not cache response when Vary contains * with extra spaces', async () => {361 await app.request('http://localhost/vary-wildcard/spaces')362 const res = await app.request('http://localhost/vary-wildcard/spaces')363 expect(res.headers.get('x-count')).toBe('2')364 })365366 it.each([200])('Should cache %i in default cacheable status codes', async (code) => {367 await app.request(`http://localhost/default/${code}/`)368 const res = await app.request(`http://localhost/default/${code}/`)369 expect(res).not.toBeNull()
50 unmodified lines
modified:src/middleware/cache/index.ts+13 added