How to add X-Frame-Options on Next.js
Last updated · Reviewed by a CISSP-certified security practitioner
To add X-Frame-Options on Next.js, set the header in next.config.js with the value DENY. Prevents your site being embedded in iframes (clickjacking). Restart dev server or redeploy. Then confirm it with curl -I or a free FixSecurityHeaders scan.
What does X-Frame-Options do?
X-Frame-Options (or the modern CSP frame-ancestors) tells browsers whether other sites may put your pages inside an iframe.
Risk if missing: Attackers can overlay your page invisibly and trick users into clicking buttons (clickjacking) — e.g. changing settings or confirming payments.
What is the Next.js configuration?
Add this to next.config.js:
// next.config.js
module.exports = {
poweredByHeader: false,
async headers() {
return [
{
source: "/(.*)",
headers: [
{ key: "X-Frame-Options", value: "DENY" },
],
},
];
},
};How do I add it step by step?
- Open next.config.js.
- Paste the X-Frame-Options configuration shown below.
- Restart dev server or redeploy.
- Run curl -I https://yourdomain.com and look for the x-frame-options line.
- Scan your site on FixSecurityHeaders to confirm the grade improved.
How do I verify it worked?
curl -sI https://yourdomain.com | grep -i "x-frame-options"
# expected:
# x-frame-options: DENYCheck your site now
Free scan, instant grade, exact fixes for your stack.
What are the common mistakes?
- Use SAMEORIGIN if your own site frames its pages.
- ALLOW-FROM is obsolete; use CSP frame-ancestors for allow-lists.
- If CSP frame-ancestors is present, modern browsers ignore X-Frame-Options.
- For nonce-based CSP use middleware.ts instead of static headers.
- poweredByHeader: false removes X-Powered-By.
Quick reference
| Header | X-Frame-Options |
|---|---|
| Recommended value | DENY |
| Stack | Next.js |
| Where | next.config.js |
| Compliance | OWASP ASVS V14.4.7, NIST 800-171 3.13.1, CMMC SC.L2-3.13.1 |
FAQ
What value should I use for X-Frame-Options?
A safe default is DENY. Use SAMEORIGIN if your own site frames its pages.
Why isn't X-Frame-Options showing up on Next.js?
For nonce-based CSP use middleware.ts instead of static headers. Also clear any CDN or page cache, and check you edited the config that actually serves your domain.
Does X-Frame-Options affect SEO or performance?
No measurable performance cost — it's a few bytes per response. It doesn't affect rankings directly, but a misconfigured policy can break page resources, so test before enforcing.
Which compliance frameworks reference X-Frame-Options?
OWASP ASVS V14.4.7, NIST 800-171 3.13.1, CMMC SC.L2-3.13.1.