For Next.js versions 9.5 and after you can define custom headers in your next.config.js
file.
The following config would apply a custom header to all static and server-rendered pages.
module.exports = {
async headers() {
return [
{
source: '/:path*{/}?',
headers: [
{
key: 'x-custom-header',
value: 'my custom header value for all pages',
},
],
},
]
},
}
See next.config.js Headers
Currently matching all pages is a little unintuitive. As seen in the example above it requires the syntax '/:path*{/}?'
. This is currently tracked in GitHub Issue #14930.