2

I am new to Next.js, but it looks like Next.js is overriding my ETag header. I am editing ETag because I connect my server to S3 and want to use S3 Object's Etag for cache controlling.

To reproduce it, in below code, it sets ETag header. But when I call this api from browser, ETag header is changed.

@/pages/api/test.ts

import { NextApiHandler } from 'next'

const handler: NextApiHandler = async (req, res) => {
    res.setHeader("ETag", "5cja7f7wbfb1c657e82dd4ssfj8sf0c6");
    return res.send("Some data here");
}

export default handler;

I tried to turn off etags generating in next.config.js but it won't help.

module.exports = {
    generateEtags: false,
}

So I am looking for a way to solve this... better if we can disable it's ETag overriding...

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Yi Zhao
  • 21
  • 2

1 Answers1

1

The generateEtags entry on next.config.js doesn't affect API routes, only regular routes.

As a workaround, you could call res.writeHead followed by res.end directly to avoid the ETag header being set by Next.js.

const handler: NextApiHandler = async (req, res) => {
    res.writeHead(200, { ETag: "5cja7f7wbfb1c657e82dd4ssfj8sf0c6" });
    return res.end("Some data here");
}
juliomalves
  • 42,130
  • 20
  • 150
  • 146