Using Craft CMS Live Preview with Next.js
Craft's Live Preview works out of the box with Twig. Wiring it up to a decoupled Next.js front end takes a few extra steps.
Step one: the preview API route
Create pages/api/preview.js to receive requests from Craft:
export default async (req, res) => {
if (req.query.token === null) {
return res.status(401).json({ message: 'No preview token' })
}
if (req.query.uri === null) {
return res.status(401).json({ message: 'No URI provided' })
}
res.setPreviewData(
{
token: req.query.token ?? null,
},
{
maxAge: 60,
}
)
res.redirect(`/${req.query.uri}`)
}
This checks the preview token and URI, enables Next.js Preview Mode for 60 seconds, then redirects to the requested page.
Step two: pass the token through your queries
Read preview context in getStaticProps:
export const getStaticProps = async ({ params, preview, previewData }) => {
// Access preview boolean and previewData object
}
Append the token to your Craft GraphQL URL. With Apollo Client:
const apolloClient = (previewToken) => {
const uri = `${process.env.NEXT_PUBLIC_CRAFT_GRAPHQL_URL}${
previewToken ? `?token=${previewToken}` : ``
}`
// Rest of Apollo configuration
}
Then pass it through at query time:
const previewToken = preview ? previewData.token : undefined
const { data } = await apolloClient(previewToken).query({...})
Step three: add a preview target in Craft
In the section settings, add a new Preview Target:
- Label: Next.js Frontend
- URL Format:
https://www.website.com/api?uri=blog/{slug}
Notes
We've only tested this on Vercel. Local previews tend to fail because the iframe cookies require HTTPS. The GraphQL piece will look different depending on your client — adapt as needed.
More from the blog
The Best Craft CMS Learning Resources in 2026
Where we'd send a developer learning Craft CMS in 2026: the official docs and tutorial, CraftQuest, the Discord, the newsletter and the conference talks.
How design and brand agencies can use Craft CMS to say "yes" to more briefs
We work with design and brand agencies as their development partner, helping them say "yes" to more briefs.
Common Accessibility Problems to Avoid in 2026
The accessibility problems we still see most often, what WCAG 2.2 and the European Accessibility Act changed, and how to catch what automated tools miss.