Do this with AI
Install the cms-live-preview-visual-editor-support-assistant skill from Contentstack agent skills. Then ask your agent:
We're rebuilding on the Next.js App Router with SSR for most routes and static pages for marketing. Review our data layer and layout, tell us where Live Preview should initialize, where the per-request client goes and which routes need Draft Mode.
New to this? Set up your AI workbench first.
Live preview for a Next.js rebuild: what to check
If you're rebuilding a Next.js site and picking a headless CMS, judge its preview on how it fits the way your site renders, not on the demo. Check how draft content reaches your pages (a preview token, a preview host, Next.js Draft Mode), whether it works with App Router server components, what happens under SSR, SSG, ISR and edge middleware, whether caches are bypassed in preview, how click-to-edit tags get onto your markup, how locales and branches are handled, how much code it adds, and how it tells you when it breaks.
Below, each check is explained neutrally, then mapped to how Contentstack handles it with Live Preview, Visual Editor and @contentstack/live-preview-utils. It ends with a checklist you can run against any CMS in an afternoon.
What you'll learn
The questions to ask about any CMS's preview before you commit to it for a Next.js rebuild
How Contentstack Live Preview gets drafts into App Router pages under CSR, SSR and SSG
What click-to-edit needs in Contentstack Visual Editor, and what it costs in code
Where preview usually breaks, and how to test for it before launch
Check 1: How does the preview get the draft?
Every headless CMS has to answer one question: how does a request from your site get unpublished content instead of published content? Usually it's a separate token, a separate API host, or both. Find out what decides the switch. If preview and production calls are mixed in one render, you get a page that's part draft and part published, which matches neither what the editor sees nor the live site.
How it works in Contentstack
Contentstack splits this into two APIs with the same routes and response shape. The Delivery API serves published content with a delivery token and is cacheable. The Preview API serves drafts, including unsaved changes, and needs a preview token plus a live_preview hash.
When an editor opens an entry, the CMS creates a session and loads your site in an iframe (or a new tab) with live_preview, content_type_uid, entry_uid and locale in the query string. The hash is short-lived, scoped to that session, and can rotate during long sessions. Don't store it in a database, a long-lived cookie or a module-level client.
With the Delivery SDK, you configure both sides on one stack:
import contentstack from "@contentstack/delivery-sdk";
export const stack = contentstack.stack({
apiKey: process.env.NEXT_PUBLIC_CONTENTSTACK_API_KEY as string,
deliveryToken: process.env.NEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN as string,
environment: process.env.NEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT as string,
live_preview: {
enable: true,
preview_token: process.env.NEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN,
host: "rest-preview.contentstack.com",
},
});When the hash is present, requests go to the preview host. When it isn't, they go to delivery. rest-preview.contentstack.com is the North America host; other regions have their own. How Live Preview works covers the session lifecycle in full.
Check 2: Does it push content or ask you to refetch?
Some preview systems push changed values into the page. Others send a signal and let your code refetch. Pushing takes a different path than production, so bugs in your real data layer can stay hidden until launch.
How it works in Contentstack
Contentstack sends a change event with no content in it. The SDK calls your callback, and your site refetches from the Preview API through its normal data layer. You write a bit more code, and in return preview runs the same fetch and render path as production.
Check 3: Does it fit your rendering mode?
A Next.js rebuild rarely uses one rendering mode: static marketing pages, server-rendered product pages, a client-rendered account area. Ask how preview works for each, and look for code rather than a yes.
Client-side rendering
In CSR mode you initialize the SDK with ssr: false and pass it your stack config, and it calls onEntryChange when content changes. Your callback refetches and sets state, and the page re-renders in place. The Next.js kickstart does this in a small client component, components/Preview.tsx.
onEntryChange fires on edit, save and publish. If you want updates while the editor types, onLiveEdit fires more often. See Client-side rendering.
Server-side rendering and server components
With ssr: true, the SDK still runs in the browser, but on each change it asks the CMS to reload the iframe. Your server component then reads live_preview from the request and fetches the draft. The update is a full round trip, so it's slower: the SSR chapter puts CSR preview at roughly 100 to 300 ms from change to visible and SSR at roughly 500 to 2000 ms. If speed matters more, you can run preview in CSR mode while production uses SSR.
The rule that matters most under SSR: create the client per request. A shared stack that gets the hash set on it per request can leak one editor's draft into another editor's preview, or into production traffic, under load.
function createContentstackClient(livePreviewHash) {
const config = {
apiKey: process.env.API_KEY,
deliveryToken: process.env.DELIVERY_TOKEN,
environment: process.env.ENVIRONMENT,
};
if (livePreviewHash) {
config.live_preview = {
enable: true,
preview_token: process.env.PREVIEW_TOKEN,
host: "rest-preview.contentstack.com",
};
}
const stack = contentstack.stack(config);
if (livePreviewHash) {
stack.livePreviewQuery({ live_preview: livePreviewHash });
}
return stack;
}Static generation, ISR and Draft Mode
Static files can't show drafts, whatever the CMS. The question is whether the CMS works with your framework's escape hatch. In the App Router that's Draft Mode: an API route checks a secret, calls draftMode().enable() and redirects, and the page branches on draftMode().isEnabled to fetch from the Preview API instead of the built data. Under ISR in the Pages Router, the same idea applies: turn revalidate off while previewing so drafts aren't frozen in a cache.
With any CMS, avoid patching static HTML with drafts after hydration: it causes hydration mismatches and flicker. Static site generation shows the Next.js setup for both routers.
Middleware and edge
If requests pass through middleware, redirects or a BFF, check that they keep query parameters. A common bug is a first page that shows the draft and a second that shows published content because a redirect dropped live_preview. Carry live_preview, content_type_uid, entry_uid and locale through navigation. Middleware and complex architectures covers proxies and BFFs.
Check 4: Are caches bypassed in preview?
Next.js caches at several layers, and so does your CDN. Ask how preview requests avoid all of them.
How it works in Contentstack
Preview responses are session-scoped and change as the editor types, so they must never be cached. Send Cache-Control: no-store on any request that carries live_preview, and bypass framework and CDN caches for it. Never cache preview responses keyed by URL: two requests to the same URL with different hashes return different content.
Check 5: How does click-to-edit get onto your markup?
Visual editing lets an editor click a heading on the page and land in the right field. Ask whether you write those paths by hand or a helper generates them, how they handle nested blocks and references, and whether they survive GraphQL.
How it works in Contentstack
Contentstack uses data-cslp attributes in the form content_type_uid.entry_uid.locale.field_path. You don't write them by hand. Call addEditableTags once in your data layer after fetching, and every component spreads from entry.$:
const entry = result.entries?.[0];
if (entry) contentstack.Utils.addEditableTags(entry, "page", true, locale);<h1 {...page.$?.title}>{page?.title}</h1>It generates indexed keys for modular blocks (blocks__0) and tags on nested arrays. Referenced entries need their own call with their own content type. GraphQL responses need system { uid content_type_uid } and a reshape to the REST shape before tagging.
Visual Editor sits on top of Live Preview and these tags. Set mode: "builder" in ContentstackLivePreview.init() (it needs Live Preview Utils v3.0+ and Delivery SDK v3.20.3+). Tag the wrapper of each block to let editors add, delete and reorder blocks in place, use data-add-direction to control the add button, and add VB_EmptyBlockParentClass to an empty modular blocks container so editors can drop the first block in. The kickstart calls it Visual Builder. Edit tags and Visual Editor covers field paths and granularity.
Check 6: Locales and branches
With several locales or content branches, preview has to know which one it's showing.
How it works in Contentstack
The CMS passes locale in the preview URL, and each environment needs a base URL for every locale you preview. addEditableTags defaults the locale to en-us, so pass the locale of the entry you render, every time. For branches, pass branch in stackDetails when you preview anything other than main. The hash is tied to one stack and branch, so a hash that reaches a request for another branch is rejected.
Check 7: How much code, and does it leak into production?
Count the files you'd add, and check what ships to production.
How it works in Contentstack
The Next.js kickstart wires everything through four files: lib/contentstack.ts for the SDK and Live Preview setup, components/Preview.tsx for the refetch loop, components/Page.tsx for rendering with edit tags, and app/page.tsx to choose between preview and production. SSR and Draft Mode add a per-request client and a draft route.
Importing @contentstack/live-preview-utils is enough to put the edit button element in the page. To keep preview out of production, build with PURGE_PREVIEW_SDK=true (SDK 4.4.0 or later) or import the SDK dynamically, set editButton.exclude: ["outsideLivePreviewPortal"], and set cleanCslpOnProduction: true to strip data-cslp outside preview.
Check 8: How does it break, and how do you find out?
Every preview setup breaks at some point: a new route, a CDN rule, an SSO gate on staging. Ask what diagnostics exist and whether failure modes are documented.
How it works in Contentstack
Turn on Settings > Live Preview > Display Setup Status. It reports "SDK not initialized", "outdated SDK", "Preview Service not enabled" and connection problems in real time. Common failures are a site that refuses to be framed (X-Frame-Options or CSP frame-ancestors), an SSO gate that won't render in an iframe (use Always Open in New Tab, SDK v4.0.0 or later), a dropped hash, a cache, or a wrong locale in edit tags. error_code 382 on public traffic means a preview hash leaked out of its session.
Preview also resolves draft references as well as published ones, so a deep include_all can hit the reference limit (error_code 141) in preview before it does on delivery. Live Preview not working? A symptom index maps each symptom to its fix, and Contentstack limits on one page lists the reference depth limits.
Evaluate it in an afternoon
Run this against every CMS on your shortlist, using a page that looks like your real site, not the vendor's demo:
Get a test account and seed one page type with a modular blocks field, a reference and two locales.
Render it in a server component and confirm the first request in preview shows the draft without a flash of published content.
Edit a field and time how long the change takes to appear. Do it for SSR and for a client-rendered route.
Put the page behind Draft Mode as a static route and confirm drafts still show.
Click a link inside the preview and confirm the second page still shows draft content.
Deploy behind your real CDN and auth, then repeat steps 2 to 5.
Open two editors on two entries at once and confirm neither sees the other's draft.
Click a heading, a block inside a list and a referenced entry. Each should open the right field in the right locale.
Build for production and check that no preview script, edit button or editing attributes reach the HTML.
Break something on purpose, such as a missing token or a cached route, and see how quickly the tooling tells you.
Next steps
Start from working code with the Next.js kickstart.
Read The Ultimate Guide to Contentstack Visual Editor for the full model, starting with How Live Preview works.
Pick your rendering chapter: Server-side rendering or Static site generation.
Keep the symptom index open while you test.
Frequently asked questions
What should I check in a headless CMS's live preview before a Next.js rebuild?
Check how drafts reach your pages (preview token, preview host, Draft Mode), whether preview works with App Router server components and your rendering mode, whether caches are bypassed in preview, how click-to-edit tags are generated, how locales and branches are handled, how much code it adds and how it fails.
Does Contentstack Live Preview work with server-side rendering in Next.js?
Yes. With ssr: true the Live Preview SDK asks the CMS to reload the iframe on each change, and your server fetches draft content from the Preview API using the live_preview hash on that request. Create the Contentstack client per request so hashes never leak between editors.
How does Contentstack preview work on a statically generated Next.js site?
Static files can't show drafts. Use Next.js Draft Mode so preview requests bypass the static output and render dynamically, fetching from the Preview API. Don't patch static HTML with drafts on the client, which causes hydration mismatches and flicker.
What do I need for click-to-edit in Contentstack Visual Editor?
Initialize the Live Preview SDK with mode set to builder, call addEditableTags on entries in your data layer, and spread the generated data-cslp attributes from entry.$ onto your elements. Visual Editor needs Live Preview Utils v3.0+ and Delivery SDK v3.20.3+.
Should preview responses be cached?
No. Preview data is scoped to one editing session and changes as the editor types. Send Cache-Control: no-store on any request that carries the live_preview hash and bypass CDN, framework and application caches for it.