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.

Images served by next do not have cache-control set

See original GitHub issue

I’ve noticed that Next does not set any cache-control headers when serving images generated by this plugin. Given that many CDNs use these headers to determine whether or not to cache an image, it seems like this renders the point of the content hashes moot.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
sincraianulcommented, May 27, 2020

This issue can easily be fixed without overriding the built-in server and losing its benefits. In your next.config.js:

const withPlugins = require('next-compose-plugins');
const optimizedImages = require('next-optimized-images');

module.exports = withPlugins([
  [optimizedImages, {
    imagesFolder: 'chunks/images'
  }]
]);

The ‘trick’ here is that Next.js slaps a Cache-Control: public, max-age=31536000, immutable header on whatever is in the .next/static/chunks folder, which is fortunate (and appropriate). 🎉

0reactions
cyrilwannercommented, May 31, 2020

Thank you very much for this workaround @sincraianul! Yes, I will make this the default folder with the next major version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Cache-Control response header for `/_next/image`
I had to rollback to next 10.0.1 , since this problem does not happen in that version. I cannot serve my images with...
Read more >
How can I cache dynamic images in Next app? - Stack Overflow
The object contains property imageUrl, which is a link to to the image. How can I set cache-control to that image? javascript ·...
Read more >
Going to Production - Next.js
Cache-Control headers set in next.config.js will be overwritten in production to ensure that static assets can be cached effectively. If you need to ......
Read more >
How to set up caching for images created by Next.JS <Image/>
The images are served by Netlify Functions and headers in .toml do not apply to Functions. I believe, Next.js plugin handles caching headers ......
Read more >
Image Optimization Through Next.js - Topcoder
The cache-control header's max-age value is utilized specifically. When both s-maxage and max-age are found, s-maxage is chosen.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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