Skip to content

Commit

Permalink
set immutable cache-headers to files matching hash regex
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlstrm committed Jan 21, 2024
1 parent ce55211 commit f499e5a
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/ilmomasiina-backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,18 @@ export default async function initApp(): Promise<FastifyInstance> {
// but this helps run a low-effort server.
// frontend files should not be gzipped on the fly, rather done on the build step.
if (config.frontendFilesPath) {
const javascriptHashRegex = /.*[.-][0-9a-fA-F]+[..*]+/;
console.info(`Serving frontend files from '${config.frontendFilesPath}'`);
server.register(fastifyStatic, {
root: path.resolve(config.frontendFilesPath),
preCompressed: true,
wildcard: false, // Disable wildcard matching, so that own index.html is served
setHeaders: (res, filePath) => {
// set immutable cache for javascript files with hash in the name
if (javascriptHashRegex.test(filePath)) {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
}
},
});
server.get('*', (_req, reply) => {
reply.sendFile('index.html');
Expand Down

0 comments on commit f499e5a

Please sign in to comment.