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
Provenance
- Commit
df85184- Tree
85c4370- Author
Komiya <102408484+pHo9UBenaA@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
46ec300
2 files changed+84 added−0 removed
modified:src/middleware/cache/index.test.ts+71 added
168 unmodified lines169 c.header('Vary', '*')170 return c.text('cached')171 })172 173+ 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+ })181+182+ 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+ })189+190+ 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+ })197+198+ 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+ })205+206+ 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+ })213+214 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 lines332 expect(() => cache({ cacheName: 'my-app-v1', wait: true, vary: ['*'] })).toThrow()333 expect(() => cache({ cacheName: 'my-app-v1', wait: true, vary: '*' })).toThrow()334 })335 336+ 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+ })341+342+ 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+ })347+348+ 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+ })353+354+ 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+ })359+360+ 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+ })365+366 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 linesmodified:src/middleware/cache/index.ts+13 added
10 unmodified lines11121314151617181920212223242526109 unmodified linesExpand all1361371381391401411421431441451461471484 unmodified lines10 unmodified lines * status codes that can be cached by default. */const defaultCacheableStatusCodes: ReadonlyArray<StatusCode> = [200]
const shouldSkipCache = (res: Response) => { const vary = res.headers.get('Vary') // Don't cache for Vary: * // https://www.rfc-editor.org/rfc/rfc9111#section-4.1 // Also note that some runtimes throw a TypeError for it. return vary && vary.includes('*')}
/** * Cache Middleware for Hono. * * @see {@link https://hono.dev/docs/middleware/builtin/cache}109 unmodified linesExpand all if (!cacheableStatusCodes.has(c.res.status)) { return } addHeader(c)
if (shouldSkipCache(c.res)) { return }
const res = c.res.clone() if (options.wait) { await cache.put(key, res) } else {4 unmodified lines