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

cache-control #14

Closed
selfagency opened this issue Aug 24, 2017 · 6 comments
Closed

cache-control #14

selfagency opened this issue Aug 24, 2017 · 6 comments

Comments

@selfagency
Copy link

selfagency commented Aug 24, 2017

for some reason when i enable express-static-gzip, it ignores the cache-control headers i set for my public folder with express.static. it should have max-age=604800 but instead my brotli and gzip compressed files return with max-age=0. i've also tried using other express caching middleware, but even though it works on my pages, it won't work on static assets served via brotli. how, then, can i set max-age of these objects?

  // static assets
  const oneYear = { maxAge: 604800000 }
  const staticFiles = [
    { file: 'browserconfig.xml', path: 'public/browserconfig.xml' },
    { file: 'favicon.ico', path: 'assets/img/favicon/favicon.ico' },
    { file: 'favicon', path: 'assets/img/favicon/' },
    { file: 'manifest.json', path: 'public/manifest.json' },
    { file: 'package.json', path: './package.json' },
    { file: 'public', path: 'public/' },
    { file: 'sw.js', path: './sw.js' }
  ]

  let robotsEnv
  process.env.ENVIRONMENT === 'production' ? robotsEnv = 'production' : robotsEnv = 'development'
  staticFiles.push({ file: 'robots.txt', path: `public/robots.txt.${robotsEnv}` })

  if (process.env.ENVIRONMENT === 'local') {
    staticFiles.push({ file: 'assets', path: 'assets/' })
    staticFiles.push({ file: 'src', path: 'src/' })
  }

  app.use('/public/', expressStaticGzip(`${process.env.DOC_ROOT}public/`, { enableBrotli: true }))

  for (let file of staticFiles) {
    app.use(`/${file.file}`, express.static(`${process.env.DOC_ROOT}${file.path}`, oneYear))
  }

  app.use(cacheControl({
    maxAge: 1800,
    public: true
  }))

screenshot 2017-08-24 01 20 41

@tkoenig89
Copy link
Owner

tkoenig89 commented Aug 24, 2017

Hi,

You might want to take a look at the readme of serve-static which is used by express-static-gzip for serving the files. All request made to express-static-gzip will only be changed to a matching compressed file and will the be forwarded to serve-static. You can also pass options to serve-static by adding these to the options object of express-static-gzip.
As serve-static has cacheControl and maxAge options this should solve your issue i guess (without having used this my self before).

Should look something like this:

app.use('/public/', expressStaticGzip(`${process.env.DOC_ROOT}public/`, { 
   enableBrotli: true,
   cacheControl: true, //optional as serve-static already defaults this to 'true'
   maxAge: 604800000 //ms
 }))

@selfagency
Copy link
Author

selfagency commented Aug 29, 2017

i tried that but there was no change in behavior. in your code it doesn't seem that you're passing through serve-static options.

@tkoenig89
Copy link
Owner

tkoenig89 commented Aug 30, 2017

So i just did a debug session on express-static-gzip and it does correctly pass it's options down to serve-static, which is passing the maxAge option to send. I did this with the following simple example and a folder containing a dummy index.html and a index.html.gz file:

const static = require("express-static-gzip"); //version 0.3.0
const express = require("express");

let app = express();

app.use("", static("static", {
    maxAge:20000,    
}));

app.listen(8080);

Opening my browser gives me a Cache-Control Header and the max-age in Seconds:
image

Changing the code to use a different maxAge like this:

//...

app.use("", static("static", {
    maxAge:604800000 ,    
}));

//...

Yields the according Header:
image

Following these observations i might suspect an issue with your setup. Maybe there are issues arising because you serve files with different middlewares from the same path. If you are using express-static-gzip you don't need to serve the original files (e.g. 'index.html') with serve-static (or express.static). Just place index.html and index.html.gz next to each other and point express-static-gzip to the folder.

@tkoenig89
Copy link
Owner

Closed due to inactivity. Feel free to reopen if this is still relevant.

@victorpavlenko
Copy link

@koenig89 are u sure this is in secconds?

@tkoenig89
Copy link
Owner

tkoenig89 commented Nov 29, 2021

@victorpavlenko you might want to check the specification of cache-control and max-age to verify the header should be in seconds. But configuring it via the serve-static middleware (which I forward all settings to) seems to require milliseconds. So i guess this might lead to some confusions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants