Setting Up Contentful Draft Preview in Next.js 16 (and the Gotchas Along the Way)
How I wired up Next.js Draft Mode, Contentful's Preview API, and Contentful Studio's iframe preview on Netlify—without weakening security headers.

For a long time, the most annoying gap in my blogging workflow was simple: I couldn't preview unpublished drafts on the real site.
I could edit text in Contentful Studio, sure, but I couldn't see how headings, spacing, reading time, or embedded diagrams actually looked on reggiepangilinan.com until I hit Publish. And publishing a half-baked post just to check the layout is a terrible workflow.
So I finally wired up proper Contentful Draft Preview using Next.js 16's draftMode() and Contentful's Preview API (preview.contentful.com). The happy path is straightforward, but getting it to work cleanly inside Contentful Studio's embedded preview pane on Netlify surfaced a few real-world gotchas worth sharing.
1. How the Preview Flow Works
At a high level, previewing a draft takes four steps:
You click Open preview on a draft post in Contentful Studio.
Contentful loads
/api/draft(?secret=...&slug=...) inside its preview iframe (or in a new tab).The route handler validates the shared secret, checks that the draft exists in Contentful's Preview API, enables Next.js Draft Mode by setting the
__prerender_bypasscookie, and redirects to/blog/[slug].The blog page (
src/app/blog/[slug]/page.tsx) seesdraftMode().isEnabled === true, switches its Contentful client topreview.contentful.com, renders the unpublished post with a clearDraftPreviewBanner, and marks the pagenoindexso search engines never crawl it.

When you're done reviewing, clicking Exit Preview hits /api/disable-draft, clears the bypass cookie, and sends you right back to the normal cached site.
2. Wiring Up Two Contentful Clients (Plus One Easy-to-Miss Spot)
Contentful separates published content from drafts using two different hosts and tokens:
Published content:
cdn.contentful.comwithCONTENTFUL_ACCESS_TOKENDraft content:
preview.contentful.comwithCONTENTFUL_PREVIEW_ACCESS_TOKEN
In src/lib/contentful.ts, I added a second previewClient pointed at preview.contentful.com and passed an optional { preview?: boolean } flag into getBlogPosts() and getBlogPost(). Both still fall back cleanly to empty results if env vars aren't set locally, so zero-secret CI builds keep working.
Gotcha #1: Don't Forget Embedded Rich Text Assets
My site has a small helper (src/lib/rich-text-asset-resolver.ts) that resolves linked image assets inside Contentful Rich Text documents. Initially, it only used the Delivery API token.
If a draft post embeds an unpublished image asset, the Delivery API won't return it—meaning the post text loads in preview, while your images silently show up broken. Passing { preview: isDraftMode } into resolveRichTextAssets() so it also switches to preview.contentful.com fixed that before it became a mystery bug.
3. Keeping /api/draft Safe (and Unindexed)
A preview endpoint bypasses your cache and exposes unpublished content, so it's worth keeping tight:
Constant-time secret check:
src/app/api/draft/route.tscompares the incomingsecretagainstCONTENTFUL_PREVIEW_SECRETusingcrypto.timingSafeEqualand never logs the secret.Slug validation: Before redirecting to
/blog/${slug}, the handler strips slashes, validates the slug format (/^[a-zA-Z0-9\-_]+$/), and confirms the draft actually exists in Contentful—preventing open redirects.Safe exit redirect:
src/app/api/disable-draft/route.tsonly redirects to relative paths starting with a single/(never//).Automatic
noindex: InsidegenerateMetadata()insrc/app/blog/[slug]/page.tsx, whendraftMode().isEnabledis true, I overriderobotsto{ index: false, follow: false }.
4. The Real Gotcha: Contentful's Embedded Preview Iframe
Once everything was deployed to Netlify, I clicked Open preview in Contentful Studio and immediately hit a wall:
Your website refused to connect.
This is where strict security headers collide with embedded CMS previews—and there were three pieces to untangle.
Gotcha #2: X-Frame-Options: DENY in Two Places
Both src/proxy.ts and netlify.toml were setting X-Frame-Options: DENY, and my Content Security Policy in src/proxy.ts had frame-ancestors 'none'.
X-Frame-Options is a legacy header that doesn't support allowing a specific external origin like Contentful Studio. The clean modern fix is to remove X-Frame-Options: DENY from both src/proxy.ts and netlify.toml, and update the CSP directive in src/proxy.ts to:
frame-ancestors 'self' https://app.contentful.com
Every other site on the internet is still blocked from framing reggiepangilinan.com, while Contentful Studio is explicitly allowed.
Gotcha #3: Cross-Site Cookies Inside an Iframe
Even after fixing the framing headers, there's a subtle browser behavior: Next.js's draftMode().enable() sets the __prerender_bypass cookie with SameSite=Lax by default.
When your site loads inside an iframe on https://app.contentful.com, modern browsers treat that as a cross-site context and won't send a SameSite=Lax cookie—so Draft Mode silently turns itself off on the redirect!
Re-setting the __prerender_bypass cookie right after draft.enable() in src/app/api/draft/route.ts with sameSite: 'none', secure: true, and partitioned: true lets Draft Mode work reliably both inside Contentful Studio's iframe and in a standalone browser tab.
5. One Final Sanity Check (Netlify Secret Scanning)
One quick tip if you deploy on Netlify: when you add CONTENTFUL_PREVIEW_ACCESS_TOKEN and CONTENTFUL_PREVIEW_SECRET in Netlify's Environment Variables UI, double-check you pasted the actual token value and not the variable name itself. Netlify's built-in secret scanner checks your build output against every secret value, and if a secret accidentally matches a string in your source code, Netlify will fail the deploy to protect you.
Generate CONTENTFUL_PREVIEW_SECRET once with a URL-safe hex string (openssl rand -hex 32), drop it into both Netlify and Contentful's Settings → Content preview URL template, and you're set for every post going forward.
Wrapping Up
Being able to click Open preview in Contentful Studio and inspect an unpublished draft—complete with real typography, dark mode, and embedded diagrams—makes writing way less stressful. In fact, this post itself was reviewed using the exact preview pipeline it describes.
If you want to look at the implementation, the whole site is open source at github.com/reggiepangilinan/mywebsite.
Related Posts
Adding AI-Powered Blog Search with Google Gemini in Next.js
How we added an intelligent, Gemini-powered search bar to a Next.js + Contentful blog—covering AI basics for non-specialists, why hybrid search beat Google Search Grounding and Vertex AI Search, real-world free-tier traffic estimates, and built-in cost guardrails.
Running a Full Next.js Blog on Free Tiers: A Contentful + Netlify Case Study
A data-backed case study breaking down Contentful Free, Netlify Free, and GitHub Free quotas (at the time of this writing), with real math on how many visitors, blog posts, and images a personal site can serve before ever paying a cent for infrastructure.
Four Silent Sitemap & SEO Bugs I Found Auditing My Next.js 16 Blog
What sitemaps, robots.txt, and canonical URLs actually do for search indexing—plus four silent SEO bugs I found and fixed auditing my Next.js 16 + Contentful blog.