feat: chaching for static react files (#862)

* set immutable tag for static route

* captur more server timings in NODE_ENV === 'development'
This commit is contained in:
Alex Christoffer Rasmussen
2024-03-31 22:07:42 +02:00
committed by GitHub
parent 9a03e4850a
commit 01000f8837
3 changed files with 23 additions and 4 deletions
+10 -1
View File
@@ -5,6 +5,7 @@ import express from 'express';
import expressStaticGzip from 'express-static-gzip';
import http, { type Server } from 'http';
import cors from 'cors';
import serverTiming from 'server-timing';
// import utils
import { resolve } from 'path';
@@ -54,6 +55,10 @@ if (!isProduction) {
// Create express APP
const app = express();
if (process.env.NODE_ENV === 'development') {
// log more serever timings
app.use(serverTiming());
}
app.disable('x-powered-by');
// setup cors for all routes
@@ -83,11 +88,15 @@ app.use(
expressStaticGzip(reactAppPath, {
enableBrotli: true,
orderPreference: ['br'],
// when we build the client all the react subfiles will get a hashed name we can 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' },
}),
);
app.get('*', (_req, res) => {
res.sendFile(resolve(resolvedPath(), 'index.html'));
res.sendFile(resolve(reactAppPath, 'index.html'));
});
// Implement catch all