Headers

This feature was introduced in Next.js 9.5 and up. If you’re using older versions of Next.js, please upgrade before trying it out.

Headers allow you to set custom HTTP headers for an incoming request 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 header objects with the key and value properties.

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 rewrites() {
  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. }

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. }