refactor: prevent caching html files

This commit is contained in:
Carlos Valente
2024-11-29 14:55:08 +01:00
committed by Carlos Valente
parent 35f4472daa
commit f561e52bb5
9 changed files with 240 additions and 280 deletions
+16 -4
View File
@@ -6,9 +6,9 @@ import expressStaticGzip from 'express-static-gzip';
import http, { type Server } from 'http';
import cors from 'cors';
import serverTiming from 'server-timing';
import { extname, resolve } from 'path';
// import utils
import { resolve } from 'path';
import { publicDir, srcDir } from './setup/index.js';
import { environment, isProduction, updateRouterPrefix } from './externals.js';
import { ONTIME_VERSION } from './ONTIME_VERSION.js';
@@ -92,10 +92,22 @@ app.use(
expressStaticGzip(srcDir.clientDir, {
enableBrotli: true,
orderPreference: ['br'],
// when we build the client all the react subfiles will get a hashed name we can the immutable tag
// when we build the client the file names contain a unique hash for the build
// this allows us to use the immutable tag
// as the contents of a build file will never change without also changing its name
// so the client dose not need to revalidate the file contetnts with the server
serveStatic: { etag: false, lastModified: false, immutable: true, maxAge: '1y' },
// meaning that the client does not need to revalidate the contents with the server
serveStatic: {
etag: false,
lastModified: false,
immutable: true,
maxAge: '1y',
setHeaders: (res, file) => {
// make sure the HTML files are always revalidated
if (extname(file) === '.html') {
res.setHeader('Cache-Control', 'public, max-age=0');
}
},
},
}),
);