question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I set different max-age for different types of file?

See original GitHub issue

Hi there, I’m using version footprint to manage my static assets (js/css/images), I would like to give html files max-age:0, and give all other assets max-age:'365d'

Currently I’m doing this by writing my own middleware, is there a way to do it using serve-static? maybe some filter function?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:7
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

51reactions
dougwilsoncommented, Jul 17, 2020

That is a good question 😃 There are two main methods for doing this:

Method 1

You keep your js/css/images/etc. in different sub folders. For example, perhaps you keep everything in public/, except your html files are in public/templates/. In this case, you can split it by path:

var serveStatic = require('serve-static')

app.use('/templates', serveStatic(__dirname + '/public/templates', { maxAge: 0 }))
app.use(serveStatic(__dirname + '/public'), { maxAge: '1y' })

Method 2

Your files are all inter-mingled and you want to apply the 0 max age to all files that are text/html. In this case, you need to add a header setting filter:

var mime = require('mime-types')
var serveStatic = require('serve-static')

app.use(serveStatic(__dirname + '/public', {
  maxAge: '1y',
  setHeaders: function (res, path) {
    if (mime.lookup(path) === 'text/html') {
      res.setHeader('Cache-Control', 'public, max-age=0')
    }
  }
}))
1reaction
rodrigoreis22commented, Mar 5, 2015

I tried what you said @dougwilson , but the last one is the one getting set:

var oneDay = 86400000;
app.use('/dist/fonts', express.static(__dirname + '/dist/fonts', { maxAge : oneDay*30 })); //30 days
app.use('/dist/images', express.static(__dirname + '/dist/images', { maxAge : oneDay*30 })); //30 days
app.use('/dist/scripts', express.static(__dirname + '/dist/scripts', { maxAge : oneDay*30 })); //30 days
app.use('/dist/components', express.static(__dirname + '/dist/components', { maxAge : oneDay*30 })); //30 days
app.use('/dist/styles', express.static(__dirname + '/dist/styles', { maxAge : oneDay*30 })); //30 days
app.use('/dist/views', express.static(__dirname + '/dist/views', { maxAge : oneDay }));
app.use(express.static(__dirname + '/dist', { maxAge: 9000000 })); //15 min (index.html)

All my resources are with max-age=9000 (last line). What am I missing? @gfaceless it worked for you?

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Express - Setting different maxAge for certain files
My problem is that if I set maxAge, my index.html file gets cached and the first request to '/' doesn't go through router...
Read more >
Set max-age headers for specific pages, files, or paths
You normally set this by using the UI by navigating to Admin > Configuration > Development > Performance on Drupal 7 and entering...
Read more >
Access-Control-Max-Age - HTTP - MDN Web Docs
The Access-Control-Max-Age response header indicates how long the results of a preflight request (that is ... Header type, Response header.
Read more >
Express serve-static middleware
This example shows how to set a different max age depending on the served file type. In this example, HTML files are not...
Read more >
Server-side caching - Apollo GraphQL Docs
You can set a default maxAge that's applied to fields that otherwise receive the default maxAge of 0 . You should identify and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found