In my upcoming project, I attempted to utilize regex path matching in the next.config.js file as explained in the documentation. The goal was to match all routes except for one specific route by adding the regex ^(?!.*books).
which successfully excludes anything with "books". However, when I tried implementing this in the headers section of my next.config.js file like so:
async headers() {
return [
{
source: "/^(?!.*books).*$",
headers: [
{
key: "Content-Security-Policy",
value: "base-uri 'self'; form-action 'self'; object-src 'self'; media-src 'self';"
},
{
key: "Strict-Transport-Security",
value: "max-age=63072000; includeSubDomains; preload"
},
{
key: "X-Content-Type-Options",
value: "nosniff"
},
{
key: "X-XSS-Protection",
value: "1; mode=block"
},
{
key: "X-Frame-Options",
value: "deny"
}
]
}
]
Upon running the application using npm run dev
, an error stating that "Pattern cannot start with "?"" is thrown. Despite being a valid regex and passing tests, the regex does not seem to work within the next.config.js file. What could be causing this discrepancy?