| | import type { Response } from 'express' |
| |
|
| | interface CacheControlOptions { |
| | key?: string |
| | public_?: boolean |
| | immutable?: boolean |
| | maxAgeZero?: boolean |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function cacheControlFactory( |
| | maxAge: number = 60 * 60, |
| | { |
| | key = 'cache-control', |
| | public_ = true, |
| | immutable = false, |
| | maxAgeZero = false, |
| | }: CacheControlOptions = {}, |
| | ): (res: Response) => void { |
| | const directives = [ |
| | maxAge && public_ && 'public', |
| | maxAge && `max-age=${maxAge}`, |
| | maxAge && immutable && 'immutable', |
| | !maxAge && 'private', |
| | !maxAge && 'no-store', |
| | maxAge >= 60 * 60 && `stale-while-revalidate=${60 * 60}`, |
| | maxAge >= 60 * 60 && `stale-if-error=${24 * 60 * 60}`, |
| | (maxAgeZero || maxAge === 0) && 'max-age=0', |
| | ] |
| | .filter(Boolean) |
| | .join(', ') |
| | return (res: Response) => { |
| | if (process.env.NODE_ENV !== 'production' && res.hasHeader('set-cookie') && maxAge) { |
| | console.warn( |
| | "You can't set a >0 cache-control header AND set-cookie or else the CDN will never respect the cache-control.", |
| | ) |
| | } |
| | res.set(key, directives) |
| | } |
| | } |
| |
|
| | |
| |
|
| | |
| | export const noCacheControl = cacheControlFactory(0) |
| |
|
| | |
| | export const errorCacheControl = cacheControlFactory(60) |
| |
|
| | |
| | const searchBrowserCacheControl = cacheControlFactory(60 * 60) |
| | |
| | const searchCdnCacheControl = cacheControlFactory(60 * 60 * 24, { |
| | key: 'surrogate-control', |
| | }) |
| | export function searchCacheControl(res: Response): void { |
| | searchBrowserCacheControl(res) |
| | searchCdnCacheControl(res) |
| | } |
| |
|
| | |
| | const defaultCDNCacheControl = cacheControlFactory(60 * 60 * 24, { |
| | key: 'surrogate-control', |
| | }) |
| | |
| | |
| | |
| | const defaultBrowserCacheControl = cacheControlFactory(60) |
| | |
| | |
| | export function defaultCacheControl(res: Response): void { |
| | defaultCDNCacheControl(res) |
| | defaultBrowserCacheControl(res) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function languageCacheControl(res: Response): void { |
| | defaultCacheControl(res) |
| | res.set('vary', 'accept-language, x-user-language') |
| | } |
| |
|
| | |
| | export const assetCacheControl = cacheControlFactory(60 * 60 * 24 * 7, { immutable: true }) |
| |
|
| | |
| | export const archivedCacheControl = cacheControlFactory(60 * 60 * 24 * 365) |
| |
|