From 2e425b52238cf7b13ec1689492cbffb0c5e5951a Mon Sep 17 00:00:00 2001 From: David Evans Date: Mon, 25 Oct 2021 22:18:40 +0100 Subject: [PATCH] Add cache-control stale-while-revalidate config --- bin/server.js | 20 +++++++++++----- bin/server/RequestHandler.js | 45 +++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/bin/server.js b/bin/server.js index 7bde87b..8445ed3 100755 --- a/bin/server.js +++ b/bin/server.js @@ -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 = { - maxAgeSeconds: 10 * 60, // 10 minutes - staleSeconds: 60 * 60 * 24 * 365, // 1 year + maxAgeSeconds: 10 * MINUTE, + staleIfErrorSeconds: YEAR, + staleWhileRevalidateSeconds: YEAR, }; const RENDER_CACHE = { immutable: true, - maxAgeSeconds: 60 * 60 * 24 * 30, // 1 month - staleSeconds: 60 * 60 * 24 * 365, // 1 year + maxAgeSeconds: 30 * DAY, + staleIfErrorSeconds: YEAR, + staleWhileRevalidateSeconds: YEAR, }; const PREVIEW_CACHE = { - maxAgeSeconds: 60 * 60, // 1 hour - staleSeconds: 60 * 60 * 24, // 1 day + maxAgeSeconds: HOUR, + staleIfErrorSeconds: DAY, + staleWhileRevalidateSeconds: DAY, }; const SKETCH_CSS_SHA = 'sha256-s7UPtBgvov5WNF9C1DlTZDpqwLgEmfiWha5a5p/Zn7E='; diff --git a/bin/server/RequestHandler.js b/bin/server/RequestHandler.js index 82c5d34..a490862 100644 --- a/bin/server/RequestHandler.js +++ b/bin/server/RequestHandler.js @@ -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 { constructor(method, matcher, handleFn) { this.method = method; this.matcher = matcher; this.handleFn = handleFn; - this.cacheMaxAge = 0; - this.cacheStale = 0; - this.immutable = false; + this.cacheControl = ''; this.allowAllOrigins = false; this.staticHeaders = []; this.info = `Custom handler at ${this.method} ${this.matcher}`; } - setCache({ maxAgeSeconds = 0, staleSeconds = 0, immutable = false }) { - this.cacheMaxAge = maxAgeSeconds; - this.cacheStale = staleSeconds; - this.immutable = immutable; + setCache(options) { + this.cacheControl = options ? makeCacheControlHeader(options) : ''; return this; } @@ -32,13 +54,8 @@ class RequestHandler { if(this.allowAllOrigins) { res.setHeader('Access-Control-Allow-Origin', '*'); } - if(this.cacheMaxAge > 0) { - res.setHeader( - 'Cache-Control', - `public, max-age=${this.cacheMaxAge}` + - (this.cacheStale ? `, stale-if-error=${this.cacheStale}` : '') + - (this.immutable ? ', immutable' : '') - ); + if(this.cacheControl) { + res.setHeader('Cache-Control', this.cacheControl); } for(const header of this.staticHeaders) { res.setHeader(header.name, header.value);