-
|
I'm trying to add a few headers to my Next.js config but I'm having trouble making them work only for all pages but not API routes. Here's what I've tried so far —
async headers() {
return [{
source: '/(.*)',
headers: [{
key: 'X-Custom-Page-Only-Header',
value: 'ok',
}]
}]
}
source: '/(?!^\/api\/$)' |
Beta Was this translation helpful? Give feedback.
Answered by
ijjk
Sep 2, 2020
Replies: 1 comment 2 replies
-
|
Hi, you should be seeing an error with the above Error parsing `/(?!^/api/$)` https://err.sh/vercel/next.js/invalid-route-source
Reason: Pattern cannot start with "?" at 2
/(?!^/api/$)
^
`source` parse failed for route {"source":"/(?!^/api/$)","headers":[{"key":"x-hello","value":"world"}]}
Error: Invalid header foundTo match against all routes except module.exports = {
headers() {
return [
{
source: '/((?!api$|api/).*)',
headers: [
{
key: 'X-Custom-Page-Only-Header',
value: 'ok'
}
]
}
]
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
paambaati
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, you should be seeing an error with the above
source('/(?!^\/api\/$)') what version of Next.js are you on? Using the above source the following error is shown on the latest version of Next.js v9.5.3To match against all routes except
/api/*you would want the something like the below route.