From 18938982a8610efcf25686298dc8053d3bbe5e4a Mon Sep 17 00:00:00 2001 From: Maedah Batool Date: Wed, 16 Feb 2022 16:03:55 +0500 Subject: [PATCH 1/3] Update details about Cache Control Headers --- docs/api-reference/next.config.js/headers.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/api-reference/next.config.js/headers.md b/docs/api-reference/next.config.js/headers.md index 5905a0860dd9..4e648d27ed5e 100644 --- a/docs/api-reference/next.config.js/headers.md +++ b/docs/api-reference/next.config.js/headers.md @@ -376,7 +376,18 @@ module.exports = { ### Cache-Control -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 revalidate the cache of a page that has been [statically generated](https://nextjs.org/docs/basic-features/pages#static-generation-recommended), you can do so by setting `revalidate` in the page's [`getStaticProps`](/docs/basic-features/data-fetching/get-static-props.md) function. +You can set the `Cache-Control` header in your [Next.js API Routes](/docs/api-routes/introduction.md) by using the `res.setHeader` method: + +```js +// pages/api/user.js + +export default function handler(req, res) { + res.setHeader('Cache-Control', 's-maxage=86400') + res.status(200).json({ name: 'John Doe' }) +} +``` + +You cannot use them in `next.config.js` file as these will be overwritten in production to ensure that static assets are cached effectively. If you need to revalidate the cache of a page that has been [statically generated](https://nextjs.org/docs/basic-features/pages#static-generation-recommended), you can do so by setting the `revalidate` prop in the page's [`getStaticProps`](/docs/basic-features/data-fetching/get-static-props.md) function. ## Related From f836238c91b4d9a93247c71543fd8159a6cbc4c8 Mon Sep 17 00:00:00 2001 From: Maedah Batool Date: Wed, 16 Feb 2022 22:35:53 -0800 Subject: [PATCH 2/3] Update docs/api-reference/next.config.js/headers.md Co-authored-by: Steven --- docs/api-reference/next.config.js/headers.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/api-reference/next.config.js/headers.md b/docs/api-reference/next.config.js/headers.md index 4e648d27ed5e..fdecc6a2b76e 100644 --- a/docs/api-reference/next.config.js/headers.md +++ b/docs/api-reference/next.config.js/headers.md @@ -387,7 +387,9 @@ export default function handler(req, res) { } ``` -You cannot use them in `next.config.js` file as these will be overwritten in production to ensure that static assets are cached effectively. If you need to revalidate the cache of a page that has been [statically generated](https://nextjs.org/docs/basic-features/pages#static-generation-recommended), you can do so by setting the `revalidate` prop in the page's [`getStaticProps`](/docs/basic-features/data-fetching/get-static-props.md) function. +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. + +If you need to revalidate the cache of a page that has been [statically generated](https://nextjs.org/docs/basic-features/pages#static-generation-recommended), you can do so by setting the `revalidate` prop in the page's [`getStaticProps`](/docs/basic-features/data-fetching/get-static-props.md) function. ## Related From 28d178496ee6751b67163502c3112f2987abbfd6 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 17 Feb 2022 18:10:43 -0500 Subject: [PATCH 3/3] Apply suggestions from code review --- docs/api-reference/next.config.js/headers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api-reference/next.config.js/headers.md b/docs/api-reference/next.config.js/headers.md index fdecc6a2b76e..3fb5be5578a3 100644 --- a/docs/api-reference/next.config.js/headers.md +++ b/docs/api-reference/next.config.js/headers.md @@ -389,7 +389,7 @@ export default function handler(req, res) { 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. -If you need to revalidate the cache of a page that has been [statically generated](https://nextjs.org/docs/basic-features/pages#static-generation-recommended), you can do so by setting the `revalidate` prop in the page's [`getStaticProps`](/docs/basic-features/data-fetching/get-static-props.md) function. +If you need to revalidate the cache of a page that has been [statically generated](/docs/basic-features/pages.md#static-generation-recommended), you can do so by setting the `revalidate` prop in the page's [`getStaticProps`](/docs/basic-features/data-fetching/get-static-props.md) function. ## Related