fix(cache): Allow composing cache control directives (#1922)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Eduard · committed by GitHub · parent ff34b1b · Browse files · 3 files·+69 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+69 added−8 removed
- tests:+31 added−2 removed
- other:+38 added−6 removed
Provenance
- Commit
6fe828c- Tree
ad9ba31- Author
Eduard <40474768+eduardvercaemer@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
ff34b1b
3 files changed+69 added−8 removed
modified:deno_dist/middleware/cache/index.ts+19 added-3 removed
123453 unmodified lines910111212131314151617181920212223242526272829303132333 unmodified lines373839402541424344457 unmodified linesimport type { Context } from '../../context.ts'import type { MiddlewareHandler } from '../../types.ts'
export const cache = (options: { cacheName: string3 unmodified lines if (options.wait === undefined) { options.wait = false }
const addHeader = (response: Response) => { if (options.cacheControl) response.headers.set('Cache-Control', options.cacheControl) const directives = options.cacheControl?.split(',').map((directive) => directive.toLowerCase())
const addHeader = (c: Context) => { if (directives) { const existingDirectives = c.res.headers .get('Cache-Control') ?.split(',') .map((d) => d.trim().split('=', 1)[0]) ?? [] for (const directive of directives) { let [name, value] = directive.trim().split('=', 2) name = name.toLowerCase() if (!existingDirectives.includes(name)) { c.header('Cache-Control', `${name}${value ? `=${value}` : ''}`, { append: true }) } } } }
return async function cache(c, next) { const key = c.req.url3 unmodified lines await next() if (!c.res.ok) { return } addHeader(c.res) addHeader(c) const response = c.res.clone() if (options.wait) { await cache.put(key, response) } else {7 unmodified linesmodified:src/middleware/cache/index.test.ts+31 added-2 removed
29 unmodified lines303132333434353637383940414243444546474849505152535455565717 unmodified lines75767778647980818283848586878889909192939495969798993 unmodified lines29 unmodified lines return c.text('not cached') })
app.use('/wait2/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' })) app.use('/wait2/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' })) app.use('/wait2/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'Max-Age=20' })) app.get('/wait2/', (c) => { return c.text('cached') })
app.use('/wait3/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' })) app.use( '/wait3/private/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'private' }) ) app.get('/wait3/private/', (c) => { return c.text('cached') })
app.use('/wait4/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' })) app.get('/wait4/', (c) => { c.header('Cache-Control', 'private') return c.text('cached') })
app.use('/not-found/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))
const ctx = new Context()
17 unmodified lines it('Should not return duplicate header values', async () => { const res = await app.request('/wait2/') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(res.headers.get('cache-control')).toBe('max-age=10') expect(res.headers.get('cache-control')).toBe('max-age=20') })
it('Should return composed middleware header values', async () => { const res = await app.request('/wait3/private/') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(res.headers.get('cache-control')).toBe('private, max-age=10') })
it('Should return composed handler header values', async () => { const res = await app.request('/wait4/') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(res.headers.get('cache-control')).toBe('private, max-age=10') })
it('Should not cache if it is not found', async () => { const res = await app.request('/not-found/') expect(res).not.toBeNull() expect(res.status).toBe(404)3 unmodified linesmodified:src/middleware/cache/index.ts+19 added-3 removed
123453 unmodified lines910111212131314151617181920212223242526272829303132333 unmodified lines373839402541424344457 unmodified linesimport type { Context } from '../../context'import type { MiddlewareHandler } from '../../types'
export const cache = (options: { cacheName: string3 unmodified lines if (options.wait === undefined) { options.wait = false }
const addHeader = (response: Response) => { if (options.cacheControl) response.headers.set('Cache-Control', options.cacheControl) const directives = options.cacheControl?.split(',').map((directive) => directive.toLowerCase())
const addHeader = (c: Context) => { if (directives) { const existingDirectives = c.res.headers .get('Cache-Control') ?.split(',') .map((d) => d.trim().split('=', 1)[0]) ?? [] for (const directive of directives) { let [name, value] = directive.trim().split('=', 2) name = name.toLowerCase() if (!existingDirectives.includes(name)) { c.header('Cache-Control', `${name}${value ? `=${value}` : ''}`, { append: true }) } } } }
return async function cache(c, next) { const key = c.req.url3 unmodified lines await next() if (!c.res.ok) { return } addHeader(c.res) addHeader(c) const response = c.res.clone() if (options.wait) { await cache.put(key, response) } else {7 unmodified lines