Headers

Examples

Version History

VersionChanges
v10.2.0has added.
v9.5.0Headers added.

Headers allow you to set custom HTTP headers on the response to an incoming request on a given path.

To set custom HTTP headers you can use the headers key in next.config.js:

  1. module.exports = {
  2. async headers() {
  3. return [
  4. {
  5. source: '/about',
  6. headers: [
  7. {
  8. key: 'x-custom-header',
  9. value: 'my custom header value',
  10. },
  11. {
  12. key: 'x-another-custom-header',
  13. value: 'my other custom header value',
  14. },
  15. ],
  16. },
  17. ]
  18. },
  19. }

headers is an async function that expects an array to be returned holding objects with source and headers properties:

  • source is the incoming request path pattern.
  • headers is an array of response header objects, with key and value properties.
  • basePath: false or undefined - if false the basePath won’t be included when matching, can be used for external rewrites only.
  • locale: false or undefined - whether the locale should not be included when matching.
  • has is an array of has objects with the type, key and value properties.

Headers are checked before the filesystem which includes pages and /public files.

Header Overriding Behavior

If two headers match the same path and set the same header key, the last header key will override the first. Using the below headers, the path /hello will result in the header x-hello being world due to the last header value set being world.

  1. module.exports = {
  2. async headers() {
  3. return [
  4. {
  5. source: '/:path*',
  6. headers: [
  7. {
  8. key: 'x-hello',
  9. value: 'there',
  10. },
  11. ],
  12. },
  13. {
  14. source: '/hello',
  15. headers: [
  16. {
  17. key: 'x-hello',
  18. value: 'world',
  19. },
  20. ],
  21. },
  22. ]
  23. },
  24. }

Path Matching

Path matches are allowed, for example /blog/:slug will match /blog/hello-world (no nested paths):

  1. module.exports = {
  2. async headers() {
  3. return [
  4. {
  5. source: '/blog/:slug',
  6. headers: [
  7. {
  8. key: 'x-slug',
  9. value: ':slug', // Matched parameters can be used in the value
  10. },
  11. {
  12. key: 'x-slug-:slug', // Matched parameters can be used in the key
  13. value: 'my other custom header value',
  14. },
  15. ],
  16. },
  17. ]
  18. },
  19. }

Wildcard Path Matching

To match a wildcard path you can use * after a parameter, for example /blog/:slug* will match /blog/a/b/c/d/hello-world:

  1. module.exports = {
  2. async headers() {
  3. return [
  4. {
  5. source: '/blog/:slug*',
  6. headers: [
  7. {
  8. key: 'x-slug',
  9. value: ':slug*', // Matched parameters can be used in the value
  10. },
  11. {
  12. key: 'x-slug-:slug*', // Matched parameters can be used in the key
  13. value: 'my other custom header value',
  14. },
  15. ],
  16. },
  17. ]
  18. },
  19. }

Regex Path Matching

To match a regex path you can wrap the regex in parenthesis after a parameter, for example /blog/:slug(\\d{1,}) will match /blog/123 but not /blog/abc:

  1. module.exports = {
  2. async headers() {
  3. return [
  4. {
  5. source: '/blog/:post(\\d{1,})',
  6. headers: [
  7. {
  8. key: 'x-post',
  9. value: ':post',
  10. },
  11. ],
  12. },
  13. ]
  14. },
  15. }

The following characters (, ), {, }, :, *, +, ? are used for regex path matching, so when used in the source as non-special values they must be escaped by adding \\ before them:

  1. module.exports = {
  2. async headers() {
  3. return [
  4. {
  5. // this will match `/english(default)/something` being requested
  6. source: '/english\\(default\\)/:slug',
  7. headers: [
  8. {
  9. key: 'x-header',
  10. value: 'value',
  11. },
  12. ],
  13. },
  14. ]
  15. },
  16. }

To only apply a header when either header, cookie, or query values also match the has field can be used. Both the source and all has items must match for the header to be applied.

has items have the following fields:

  • type: String - must be either header, cookie, host, or query.
  • key: String - the key from the selected type to match against.
  • value: String or undefined - the value to check for, if undefined any value will match. A regex like string can be used to capture a specific part of the value, e.g. if the value first-(?<paramName>.*) is used for first-second then second will be usable in the destination with :paramName.
  1. module.exports = {
  2. async headers() {
  3. return [
  4. // if the header `x-add-header` is present,
  5. // the `x-another-header` header will be applied
  6. {
  7. source: '/:path*',
  8. has: [
  9. {
  10. type: 'header',
  11. key: 'x-add-header',
  12. },
  13. ],
  14. headers: [
  15. {
  16. key: 'x-another-header',
  17. value: 'hello',
  18. },
  19. ],
  20. },
  21. // if the source, query, and cookie are matched,
  22. // the `x-authorized` header will be applied
  23. {
  24. source: '/specific/:path*',
  25. has: [
  26. {
  27. type: 'query',
  28. key: 'page',
  29. // the page value will not be available in the
  30. // header key/values since value is provided and
  31. // doesn't use a named capture group e.g. (?<page>home)
  32. value: 'home',
  33. },
  34. {
  35. type: 'cookie',
  36. key: 'authorized',
  37. value: 'true',
  38. },
  39. ],
  40. headers: [
  41. {
  42. key: 'x-authorized',
  43. value: ':authorized',
  44. },
  45. ],
  46. },
  47. // if the header `x-authorized` is present and
  48. // contains a matching value, the `x-another-header` will be applied
  49. {
  50. source: '/:path*',
  51. has: [
  52. {
  53. type: 'header',
  54. key: 'x-authorized',
  55. value: '(?<authorized>yes|true)',
  56. },
  57. ],
  58. headers: [
  59. {
  60. key: 'x-another-header',
  61. value: ':authorized',
  62. },
  63. ],
  64. },
  65. // if the host is `example.com`,
  66. // this header will be applied
  67. {
  68. source: '/:path*',
  69. has: [
  70. {
  71. type: 'host',
  72. value: 'example.com',
  73. },
  74. ],
  75. headers: [
  76. {
  77. key: 'x-another-header',
  78. value: ':authorized',
  79. },
  80. ],
  81. },
  82. ]
  83. },
  84. }

Headers with basePath support

When leveraging basePath support with headers each source is automatically prefixed with the basePath unless you add basePath: false to the header:

  1. module.exports = {
  2. basePath: '/docs',
  3. async headers() {
  4. return [
  5. {
  6. source: '/with-basePath', // becomes /docs/with-basePath
  7. headers: [
  8. {
  9. key: 'x-hello',
  10. value: 'world',
  11. },
  12. ],
  13. },
  14. {
  15. source: '/without-basePath', // is not modified since basePath: false is set
  16. headers: [
  17. {
  18. key: 'x-hello',
  19. value: 'world',
  20. },
  21. ],
  22. basePath: false,
  23. },
  24. ]
  25. },
  26. }

Headers with i18n support

When leveraging i18n support with headers each source is automatically prefixed to handle the configured locales unless you add locale: false to the header. If locale: false is used you must prefix the source with a locale for it to be matched correctly.

  1. module.exports = {
  2. i18n: {
  3. locales: ['en', 'fr', 'de'],
  4. defaultLocale: 'en',
  5. },
  6. async headers() {
  7. return [
  8. {
  9. source: '/with-locale', // automatically handles all locales
  10. headers: [
  11. {
  12. key: 'x-hello',
  13. value: 'world',
  14. },
  15. ],
  16. },
  17. {
  18. // does not handle locales automatically since locale: false is set
  19. source: '/nl/with-locale-manual',
  20. locale: false,
  21. headers: [
  22. {
  23. key: 'x-hello',
  24. value: 'world',
  25. },
  26. ],
  27. },
  28. {
  29. // this matches '/' since `en` is the defaultLocale
  30. source: '/en',
  31. locale: false,
  32. headers: [
  33. {
  34. key: 'x-hello',
  35. value: 'world',
  36. },
  37. ],
  38. },
  39. {
  40. // this gets converted to /(en|fr|de)/(.*) so will not match the top-level
  41. // `/` or `/fr` routes like /:path* would
  42. source: '/(.*)',
  43. headers: [
  44. {
  45. key: 'x-hello',
  46. value: 'world',
  47. },
  48. ],
  49. },
  50. ]
  51. },
  52. }

Cache-Control

You can set the Cache-Control header in your Next.js API Routes by using the res.setHeader method:

  1. // pages/api/user.js
  2. export default function handler(req, res) {
  3. res.setHeader('Cache-Control', 's-maxage=86400')
  4. res.status(200).json({ name: 'John Doe' })
  5. }

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, you can do so by setting the revalidate prop in the page’s getStaticProps function.

For more information, we recommend the following sections:

Security HeadersImprove the security of your Next.js application by add HTTP response headers.