Skip to content

Commit

Permalink
dynamic cache control
Browse files Browse the repository at this point in the history
  • Loading branch information
daroczig committed Sep 27, 2024
1 parent 9865050 commit 92b636d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,24 @@ export function app(): express.Express {

// Serve static files
server.get('*.*', express.static(browserDistFolder, {
maxAge: '1y'
setHeaders: (res, path) => {
const generatedJsPattern = /-[A-Z0-9]+\.(js|css|woff2)$/;
if (generatedJsPattern.test(path)) {
// generated files with hashed filenames can be cached forever
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
} else {
// default to cache for 1 hour
res.setHeader('Cache-Control', 'public, max-age=3600');
}
}
}));

// Cache dynamic content for 10 mins
server.use((req, res, next) => {
res.setHeader('Cache-Control', 'public, max-age=600');
next();
});

// All regular routes use the Angular engine
server.get('*', (req, res, next) => {
const { protocol, originalUrl, baseUrl, headers } = req;
Expand Down

1 comment on commit 92b636d

@Palabola
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Please sign in to comment.