Skip to content

Commit

Permalink
Add caching for more hot paths
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Jan 16, 2025
1 parent 6cfdb30 commit 8ae1ab6
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,29 @@ app.get('/users/:user', (req, res) => {
res.send(userPageFile);
});

app.get('*/js/*', (req, res, next) => {
// These files' names contain a hash of their content, so they can be cached forever.
app.get([
'*/js/*',
'*/static/assets/*'
], (req, res, next) => {
// File names contain hash of content, can cache forever.
res.header('Cache-Control', 'public, max-age=315360000, immutable');
next();
});
app.get('*/static/assets/*', (req, res, next) => {
// File names contain hash of content, can cache forever.
res.header('Cache-Control', 'public, max-age=315360000, immutable');
next();
});
app.get('*/static/blocks-media/*', (req, res, next) => {
// File names don't contain hash of content, but these files are hot and will rarely change.

// These files could change but they are reasonably hot and very rarely will change
app.get([
'*/static/blocks-media/*',
'*/favicon.ico',
'*/manifest.webmanifest',
'*/images/*',
// Our sw.js is just a stub; if we actually used it then we would want to not list it here
'*/sw.js'
], (req, res, next) => {
res.header('Cache-Control', 'public, max-age=604800, immutable');
next();
});

app.get('*', (req, res, next) => {
// Ask browsers to revalidate all files that aren't explicitly cached
if (res.getHeader('Cache-Control') === undefined) {
Expand Down

0 comments on commit 8ae1ab6

Please sign in to comment.