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
+1 -1
View File
@@ -87,7 +87,7 @@
"sass": "^1.57.1",
"typescript": "catalog:",
"vite": "^5.2.11",
"vite-plugin-compression2": "^0.12.0",
"vite-plugin-compression2": "^1.3.3",
"vite-plugin-svgr": "^4.2.0",
"vite-tsconfig-paths": "^4.3.1",
"vitest": "catalog:"
+10
View File
@@ -0,0 +1,10 @@
import * as esbuild from 'esbuild';
import { esbuildCommon } from './esbuildCommon.js';
await esbuild.build({
...esbuildCommon,
entryPoints: ['src/app.ts'],
minify: false,
dropLabels: [],
outfile: 'dist/index.cjs',
});
+8
View File
@@ -0,0 +1,8 @@
import * as esbuild from 'esbuild';
import { esbuildCommon } from './esbuildCommon.js';
await esbuild.build({
...esbuildCommon,
entryPoints: ['src/index.ts'],
outfile: 'dist/docker.cjs',
});
+8
View File
@@ -0,0 +1,8 @@
import * as esbuild from 'esbuild';
import { esbuildCommon } from './esbuildCommon.js';
await esbuild.build({
...esbuildCommon,
entryPoints: ['src/app.ts'],
outfile: 'dist/index.cjs',
});
+10
View File
@@ -0,0 +1,10 @@
export const esbuildCommon = {
logLevel: 'error',
platform: 'node',
target: ['node20'],
format: 'cjs',
bundle: true,
minify: true,
legalComments: 'external',
dropLabels: ['DEV'],
};
-3
View File
@@ -1,3 +0,0 @@
{
"ext": "js,json,ts"
}
+6 -7
View File
@@ -33,7 +33,7 @@
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "catalog:",
"@typescript-eslint/parser": "catalog:",
"esbuild": "^0.19.10",
"esbuild": "^0.24.0",
"eslint": "catalog:",
"eslint-plugin-prettier": "catalog:",
"ontime-types": "workspace:*",
@@ -51,12 +51,11 @@
"dev": "cross-env NODE_ENV=development tsx watch ./src/index.ts",
"dev:inspect": "cross-env NODE_ENV=development tsx watch --inspect ./src/index.ts",
"dev:test": "cross-env IS_TEST=true tsx ./src/index.ts",
"build": "esbuild src/app.ts --log-level=error --platform=node --format=cjs --bundle --minify --legal-comments=external --drop-labels=DEV --outfile=dist/index.cjs",
"build:electron": "esbuild src/app.ts --log-level=error --platform=node --format=cjs --bundle --minify --legal-comments=external --drop-labels=DEV --outfile=dist/index.cjs",
"build:local": "esbuild src/app.ts --log-level=error --platform=node --format=cjs --bundle --minify --legal-comments=external --drop-labels=DEV --outfile=dist/index.cjs",
"build:docker": "esbuild src/index.ts --log-level=error --platform=node --format=cjs --minify --bundle --legal-comments=external --drop-labels=DEV --outfile=dist/docker.cjs",
"build:localdocker": "cross-env NODE_ENV=local esbuild src/index.ts --log-level=error --platform=node --format=cjs --minify --bundle --legal-comments=external --drop-labels=DEV --outfile=dist/docker.cjs",
"build:debug": "esbuild src/app.ts --platform=node --format=cjs --bundle --legal-comments=external --outfile=dist/index.cjs",
"build": "node esbuild.electron.js",
"build:electron": "node esbuild.electron.js",
"build:local": "node esbuild.dev.js",
"build:docker": "node esbuild.docker.js",
"build:localdocker": "cross-env NODE_ENV=local node esbuild.docker.js",
"lint": "eslint . --quiet",
"test": "cross-env IS_TEST=true vitest",
"test:pipeline": "cross-env IS_TEST=true vitest run",
+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');
}
},
},
}),
);