1
0
mirror of synced 2025-12-26 14:02:45 -05:00
Files
docs/middleware/cache-control.js
Kevin Heis a16aeac936 Prepare render-page for re-enabling HTML caching (#29473)
* Prepare render-page for re-enabling HTML caching

* Prepare render-page for re-enabling HTML caching

* Prepare render-page for re-enabling HTML caching

* Update healthz.js

* Pre calculate cache control directives

* Update render-page.js
2022-07-29 19:49:36 +00:00

33 lines
861 B
JavaScript

// Return a function you can pass a Response object to and it will
// set the `Cache-Control` header.
//
// For example:
//
// const cacheControlYear = getCacheControl(60 * 60 * 24 * 365)
// ...
// cacheControlYear(res)
// res.send(body)
//
// Or, if you want to make it definitely not cache:
//
// const noCacheControl = getCacheControl(0) // you can use `false` too
// ...
// noControlYear(res)
// res.send(body)
//
// Max age is in seconds
export function cacheControlFactory(maxAge = 60 * 60, { public_ = true, immutable = false } = {}) {
const directives = [
maxAge && public_ && 'public',
maxAge && `max-age=${maxAge}`,
maxAge && immutable && 'immutable',
!maxAge && 'private',
!maxAge && 'no-store',
]
.filter(Boolean)
.join(', ')
return (res) => {
res.set('cache-control', directives)
}
}