Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gfaceless opened this issue Feb 26, 2015 · 6 comments
Closed

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

gfaceless opened this issue Feb 26, 2015 · 6 comments
Assignees
Labels

Comments

@gfaceless
Copy link

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 dougwilson self-assigned this Feb 26, 2015
@dougwilson
Copy link
Contributor

dougwilson commented Feb 26, 2015

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
Copy link
Author

Exactly what I want.
thank you very much :D

@rodrigoreis22
Copy link

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
Copy link
Contributor

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
Copy link

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

@passivedragon
Copy link

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.

@expressjs expressjs locked as resolved and limited conversation to collaborators May 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants