Version-stamped static URLs cached forever, thumbnail cache 25000 with throttled trimming
This commit is contained in:
@@ -2187,7 +2187,10 @@ function indexHtml(c) {
|
||||
try { _indexSource = readFileSync('./public/index.html', 'utf8'); }
|
||||
catch { return c.text('index.html not found', 404); }
|
||||
}
|
||||
const html = _indexSource.replace('__BUILD_TAG__', BUILD_TAG);
|
||||
// Local css/js get `?v=<build>`: that URL's bytes never change, so the static
|
||||
// handler below lets browsers keep it forever; a new build is a new URL.
|
||||
const html = _indexSource.replace('__BUILD_TAG__', BUILD_TAG)
|
||||
.replace(/((?:href|src)=")((?![a-z]+:|\/)[^"?]+\.(?:css|js))(")/g, `$1$2?v=${BUILD_TAG}$3`);
|
||||
return sendCompressed(c, compressedEntry('index.html', Buffer.from(html), MIME.html), 'no-cache');
|
||||
}
|
||||
app.get('/', indexHtml);
|
||||
@@ -2203,7 +2206,11 @@ app.get('/*', async (c, next) => {
|
||||
if (!(await file.exists())) return next();
|
||||
const raw = Buffer.from(await file.arrayBuffer());
|
||||
const ext = p.slice(p.lastIndexOf('.') + 1).toLowerCase();
|
||||
return sendCompressed(c, compressedEntry(p, raw, MIME[ext] || 'application/octet-stream'), 'no-cache');
|
||||
// `?v=` equal to the running build → forever (a new build changes the URL).
|
||||
// Any other / missing v keeps revalidating.
|
||||
const forever = c.req.query('v') === BUILD_TAG;
|
||||
return sendCompressed(c, compressedEntry(p, raw, MIME[ext] || 'application/octet-stream'),
|
||||
forever ? 'public, max-age=31536000, immutable' : 'no-cache');
|
||||
});
|
||||
// Self-hosted fonts and icons rarely change: let the browser keep them for 30 days
|
||||
// (the service worker precaches the shell anyway); everything else revalidates.
|
||||
|
||||
Reference in New Issue
Block a user