expressjs / serve-static

Serve static files
MIT License
1.4k stars 228 forks source link

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

Closed gfaceless closed 9 years ago

gfaceless commented 9 years ago

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?

dougwilson commented 9 years ago

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')
    }
  }
}))
gfaceless commented 9 years ago

Exactly what I want. thank you very much :D

rodrigoreis22 commented 9 years ago

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?

dougwilson commented 9 years ago

Unless your URLs are http://127.0.0.1/dist/fonts/blah.woff, then you should not have /dist/ in the URL part of app.use.

rodrigoreis22 commented 9 years ago

That's true @dougwilson , my bad. sorry.

passivedragon commented 4 years ago

running the first suggestion now results in a type error.

TypeError: Router.use() requires a middleware function but got a Object at \node_modules\express\lib\router\index.js:458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) I'm unsure how to adapt this.