What this thread does
The problem
A scanner or a pre-launch checklist says your Next.js site is missing security headers (HSTS, nosniff, framing protection). You want to add a baseline to every route without touching each page, and an agent can write the config once you tell it which headers and where.
What the Next.js docs say
From the headers reference (page last updated 30 June 2026, read 18 October 2026):
- Put an async or sync
headersfunction innext.config.js. It returns an array of objects, each with asourcepath pattern and aheadersarray ofkeyandvaluepairs. - To apply a header to every route, use a catch-all source such as
/(.*)or/:path*. A:slugpattern does not match nested paths. - If two entries match the same path and set the same header, the last one wins. Put the catch-all first and specific overrides after it.
- Headers are checked before the filesystem, so they also apply to files in
/public. Immutable hashed assets keep aCache-Controlyou cannot override. - With
basePathori18nthe source is prefixed for you unless you setbasePath: falseorlocale: false.
The same page lists these headers, with the values it suggests:
| Header | Suggested value in the docs | What it does |
|---|---|---|
Strict-Transport-Security | max-age=63072000; includeSubDomains; preload | Browsers use HTTPS only, for 2 years, including subdomains |
X-Content-Type-Options | nosniff | Stops the browser guessing a content type |
Referrer-Policy | origin-when-cross-origin | Limits what is sent to other sites |
Permissions-Policy | camera=(), microphone=(), geolocation=(), browsing-topics=() | Turns off browser features you do not use |
X-Frame-Options | SAMEORIGIN | Clickjacking protection; the docs say CSP frame-ancestors supersedes it |
X-DNS-Prefetch-Control | on | Performance, not security |
Content-Security-Policy has its own guide; see the neighbouring thread.
A prompt you can give your agent
Add aheadersfunction to next.config that sets Strict-Transport-Security, X-Content-Type-Options nosniff, Referrer-Policy and Permissions-Policy on every route, using the values from the Next.js headers docs. Do not add a Content-Security-Policy yet. Then run the production build, start it and show me the response headers for/withcurl -I.
Watch out
preloadplusincludeSubDomainsin HSTS is hard to undo: browsers keep the rule for the full max-age and the preload list is separate. If the site or any subdomain still needs plain HTTP, start with a short max-age and nopreload. This is my caution; the docs show the long value as their example.- If a CDN or reverse proxy in front of the app already sets a header, check which copy reaches the browser.
- I have not run this configuration; it follows the docs. Verify with
curl -Ion your deployed URL, not only on localhost.
Sources
- Next.js docs, headers (nextjs.org, Vercel).
Started from a post by @Next.js docs (Vercel), packaged by the editor. Original post