Improve caching; allow using stale cache for longer on network error

This commit is contained in:
David Evans 2021-10-24 12:16:53 +01:00
parent 4dc77897e9
commit 5b99ba2937
2 changed files with 25 additions and 8 deletions

View File

@ -32,12 +32,23 @@ function devMapper(file, type, data) {
}
}
const STATIC_MAX_AGE = 10 * 60; // 10 minutes
const RENDER_MAX_AGE = 60 * 60 * 24 * 7; // 1 week
const STATIC_CACHE = {
maxAgeSeconds: 10 * 60, // 10 minutes
staleSeconds: 60 * 60 * 24 * 365, // 1 year
};
const RENDER_CACHE = {
immutable: true,
maxAgeSeconds: 60 * 60 * 24 * 30, // 1 month
staleSeconds: 60 * 60 * 24 * 365, // 1 year
};
const PREVIEW_CACHE = {
maxAgeSeconds: 60 * 60, // 1 hour
staleSeconds: 60 * 60 * 24, // 1 day
};
const SKETCH_CSS_SHA = 'sha256-s7UPtBgvov5WNF9C1DlTZDpqwLgEmfiWha5a5p/Zn7E=';
const statics = new StaticRequestHandler('')
.setCacheMaxAge(DEV ? 0 : STATIC_MAX_AGE)
.setCache(DEV ? {} : STATIC_CACHE)
.addHeader('Content-Security-Policy', [
'base-uri \'self\'',
'default-src \'none\'',
@ -86,7 +97,7 @@ if(DEV) {
}
const render = new RenderRequestHandler('/render')
.setCacheMaxAge(DEV ? 0 : RENDER_MAX_AGE)
.setCache(DEV ? {} : RENDER_CACHE)
.setCrossOrigin(true)
.addHeader('Content-Security-Policy', [
'base-uri \'self\'',
@ -99,7 +110,7 @@ const render = new RenderRequestHandler('/render')
.addHeader('X-Content-Type-Options', 'nosniff');
const preview = new PreviewRequestHandler('/preview')
.setCacheMaxAge(DEV ? 0 : RENDER_MAX_AGE)
.setCache(DEV ? {} : PREVIEW_CACHE)
.addHeader('Content-Security-Policy', [
'base-uri \'self\'',
'default-src \'none\'',

View File

@ -4,13 +4,17 @@ class RequestHandler {
this.matcher = matcher;
this.handleFn = handleFn;
this.cacheMaxAge = 0;
this.cacheStale = 0;
this.immutable = false;
this.allowAllOrigins = false;
this.staticHeaders = [];
this.info = `Custom handler at ${this.method} ${this.matcher}`;
}
setCacheMaxAge(seconds) {
this.cacheMaxAge = seconds;
setCache({ maxAgeSeconds = 0, staleSeconds = 0, immutable = false }) {
this.cacheMaxAge = maxAgeSeconds;
this.cacheStale = staleSeconds;
this.immutable = immutable;
return this;
}
@ -31,7 +35,9 @@ class RequestHandler {
if(this.cacheMaxAge > 0) {
res.setHeader(
'Cache-Control',
`public, max-age=${this.cacheMaxAge}`
`public, max-age=${this.cacheMaxAge}` +
(this.cacheStale ? `, stale-if-error=${this.cacheStale}` : '') +
(this.immutable ? ', immutable' : '')
);
}
for(const header of this.staticHeaders) {