twopluszero / next-images

Import images in Next.js (supports jpg, jpeg, svg, png and gif images)
MIT License
948 stars 67 forks source link

How will I be able to set Cache control for the images? #37

Closed raindecastro closed 4 years ago

raindecastro commented 4 years ago

My main image keeps on fetching and I want to be able to cache it.

This is the header that i'm getting in dev tools - Cache-Control: public, max-age=0

Is there a way for me to chance the max-age?

raindecastro commented 4 years ago

Was able to fix it up when I reconfigured my express app

aimichal commented 4 years ago

@raindecastro Hi! I'm also seeing Cache-Control: public, max-age=0 for requests like /_next/static/images/blah-abc123...jpg. Can you say what you did in the express server to configure the cache-control header for images?

raindecastro commented 4 years ago

@aimichal For sure! here's the code snippet, it's just pretty much getting the path of the image files and setting their headers.

  const server = express();

  server.get(/^\/_next\/static\/(images|fonts)\//, (_, res, nextHandler) => {
    res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
    nextHandler();
  });