22

I am using the default nextjs server to run my nextjs program by this command next start.

However, I am not able to change the cache-control header for the files under the public folder.

Is there any method to set the cache-control header without setting the Custom Server?

5 Answers 5

38

There is undocumented feature or bug, but it works. More info can be found here https://nextjs.org/docs/api-reference/next.config.js/headers

Add config to you next.config.js file for example:

  async headers() {
    return [
      {
        source: '/:all*(svg|jpg|png)',
        locale: false,
        headers: [
          {
            key: 'Cache-Control',
            value: 'public, max-age=9999999999, must-revalidate',
          }
        ],
      },
    ]
  },
Sign up to request clarification or add additional context in comments.

3 Comments

I believe this feature is now documented.
It is documented not to work for Cache-Control. nextjs.org/docs/api-reference/next.config.js/…
It doesn't work now
10

Per this bug report and discussion the Next devs believe static file serving should only be used as a developer convenience, not in production, and hence they don't consider it a priority to add such features.

However, in the issue comments somebody has suggested a workaround using Express to detect requests that will end up serving static files. For example, if the Next.js route handler is the handler() method you can do this to set a one-year cache policy for *.woff font files:

  // this is a hack to make the cache headers friendlier..
  server.get('*.woff2?', (req, res) => {
    res.setHeader('Cache-Control', 'public,max-age=31536000');
    return handler(req, res);
  });

2 Comments

Please put the whole code example for the server file, I don't see how to apply this given no server file I have.
If you don't use a separate server.js file with Express (or similar API), you can not use this method.
3

According to the NEXT. official documentation i.e.,

You cannot set Cache-Control headers in next.config.js file as these will be overwritten in production to ensure that API Routes and static assets are cached effectively.

Comments

1

If you only want to set the max-age and only for images, you can use minimumCacheTTL in next.config.js, like so:

module.exports = {
  images: {
    minimumCacheTTL: 31536000,
  },
}

Note that

There is no mechanism to invalidate the cache at this time, so its best to keep minimumCacheTTL low. Otherwise you may need to manually change the src prop or delete <distDir>/cache/images.

This won't work if you're using Static Image Imports, because for those the filename includes a hash, so the server uses the immutable directive.

Comments

0

I'm using Express as a custom server for Next.js, and this is how I set "Cache-Control" header for static files:

const express = require("express");
const server = express();
...
server.use(express.static(__dirname + "/public", { maxAge: "365d" }));
server.use(function (req, res, next) {
  if (req.url.match(".js|.css|.woff|.jpg|.png|.gif|.ttf")) {
    res.setHeader("Cache-Control", "public,max-age=31536000"); // 365 days
  }
  next();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.