Add cache-control stale-while-revalidate config

This commit is contained in:
David Evans 2021-10-25 22:18:40 +01:00
parent 5b99ba2937
commit 2e425b5223
2 changed files with 45 additions and 20 deletions

View File

@ -32,18 +32,26 @@ function devMapper(file, type, data) {
} }
} }
const MINUTE = 60;
const HOUR = MINUTE * 60;
const DAY = HOUR * 24;
const YEAR = DAY * 365;
const STATIC_CACHE = { const STATIC_CACHE = {
maxAgeSeconds: 10 * 60, // 10 minutes maxAgeSeconds: 10 * MINUTE,
staleSeconds: 60 * 60 * 24 * 365, // 1 year staleIfErrorSeconds: YEAR,
staleWhileRevalidateSeconds: YEAR,
}; };
const RENDER_CACHE = { const RENDER_CACHE = {
immutable: true, immutable: true,
maxAgeSeconds: 60 * 60 * 24 * 30, // 1 month maxAgeSeconds: 30 * DAY,
staleSeconds: 60 * 60 * 24 * 365, // 1 year staleIfErrorSeconds: YEAR,
staleWhileRevalidateSeconds: YEAR,
}; };
const PREVIEW_CACHE = { const PREVIEW_CACHE = {
maxAgeSeconds: 60 * 60, // 1 hour maxAgeSeconds: HOUR,
staleSeconds: 60 * 60 * 24, // 1 day staleIfErrorSeconds: DAY,
staleWhileRevalidateSeconds: DAY,
}; };
const SKETCH_CSS_SHA = 'sha256-s7UPtBgvov5WNF9C1DlTZDpqwLgEmfiWha5a5p/Zn7E='; const SKETCH_CSS_SHA = 'sha256-s7UPtBgvov5WNF9C1DlTZDpqwLgEmfiWha5a5p/Zn7E=';

View File

@ -1,20 +1,42 @@
function makeCacheControlHeader({
mode = 'public',
maxAgeSeconds = -1,
staleIfErrorSeconds = -1,
staleWhileRevalidateSeconds = -1,
immutable = false,
}) {
const parts = [];
if(mode) {
parts.push(mode);
}
if(maxAgeSeconds >= 0) {
parts.push(`max-age=${maxAgeSeconds}`);
}
if(staleIfErrorSeconds >= 0) {
parts.push(`stale-if-error=${staleIfErrorSeconds}`);
}
if(staleWhileRevalidateSeconds >= 0) {
parts.push(`stale-while-revalidate=${staleWhileRevalidateSeconds}`);
}
if(immutable) {
parts.push('immutable');
}
return parts.join(', ');
}
class RequestHandler { class RequestHandler {
constructor(method, matcher, handleFn) { constructor(method, matcher, handleFn) {
this.method = method; this.method = method;
this.matcher = matcher; this.matcher = matcher;
this.handleFn = handleFn; this.handleFn = handleFn;
this.cacheMaxAge = 0; this.cacheControl = '';
this.cacheStale = 0;
this.immutable = false;
this.allowAllOrigins = false; this.allowAllOrigins = false;
this.staticHeaders = []; this.staticHeaders = [];
this.info = `Custom handler at ${this.method} ${this.matcher}`; this.info = `Custom handler at ${this.method} ${this.matcher}`;
} }
setCache({ maxAgeSeconds = 0, staleSeconds = 0, immutable = false }) { setCache(options) {
this.cacheMaxAge = maxAgeSeconds; this.cacheControl = options ? makeCacheControlHeader(options) : '';
this.cacheStale = staleSeconds;
this.immutable = immutable;
return this; return this;
} }
@ -32,13 +54,8 @@ class RequestHandler {
if(this.allowAllOrigins) { if(this.allowAllOrigins) {
res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Origin', '*');
} }
if(this.cacheMaxAge > 0) { if(this.cacheControl) {
res.setHeader( res.setHeader('Cache-Control', this.cacheControl);
'Cache-Control',
`public, max-age=${this.cacheMaxAge}` +
(this.cacheStale ? `, stale-if-error=${this.cacheStale}` : '') +
(this.immutable ? ', immutable' : '')
);
} }
for(const header of this.staticHeaders) { for(const header of this.staticHeaders) {
res.setHeader(header.name, header.value); res.setHeader(header.name, header.value);