Add cache-control stale-while-revalidate config
This commit is contained in:
parent
5b99ba2937
commit
2e425b5223
|
@ -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=';
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue