Live Preview & Draft Mode
How editors see unpublished changes as they type, and how to point previews at a deployed site.
Every page type and every component has a Live Preview URL. When an editor opens an entry in Contentful, the preview pane loads the site in Next.js Draft Mode. Draft Mode reads from the Preview API and updates as the editor types.
How it works
- Setup creates the preview URLs.
aces setup(oraces previews) builds a preview environment in the space, with a URL for each page type and component fromaces.config.ts. - Contentful calls a draft route. The route gets the entry’s slug or ID, the locale and the secret, for example
/api/draft/pages?secret=…&slug=…&locale=…. - The route checks the secret and enables Draft Mode. It compares the secret with the server-only
CF_PREVIEW_SECRET, finds the right path for the entry, enables Draft Mode and redirects. - Pages render client components. In Draft Mode the page body uses each component’s
client.tsx, which subscribes withuseContentfulLiveUpdates. Edits appear without a reload. When an editor adds a block to a Page Body, the page fetches its data again so the new block appears.
| Route | Previews |
|---|---|
/api/draft/pages |
Page entries (Specialty Pages resolve to their path) |
/api/draft/articles |
Article entries |
/api/draft/page-type |
Page types added with aces page add |
/api/draft/entries |
A single component entry, rendered on its own |
/api/draft/app |
The Apps entry (navigation, footer) |
/api/disable-draft |
Leaves Draft Mode and reloads the published site |
Each route is a few lines built on createDraftHandler from @aces-core/contentful/draft. To preview a new kind of entry, copy one and change resolve so it returns the path to open.
import { createDraftHandler } from "@aces-core/contentful/draft";
import { fetchEntryData } from "@aces/contentful";
export const GET = createDraftHandler({
required: ["id"],
resolve: async ({ params, locale }) => {
const id = params.get("id")!;
const entry = await fetchEntryData(id, true, locale);
return entry?.[0] ? `/draft/entries?id=${encodeURIComponent(id)}` : null;
},
});
Leaving Draft Mode
The Draft Mode bar at the top of the page has an Exit Draft Mode link. It calls /api/disable-draft, which clears the Draft Mode cookies and does a full page load of the published site.
After you deploy
Preview URLs point at http://localhost:3000 until you tell ACES where the site lives. Set site.url in aces.config.ts, or pass the URL, then rebuild the URLs:
pnpm aces previews --site-url https://www.example.com
The site’s own next.config.ts already allows Contentful’s app (app.contentful.com) to frame it, so Live Preview works on the deployed site with no further changes.
Troubleshooting
- The preview pane shows a 401. The secret in the preview URL doesn’t match
CF_PREVIEW_SECRETon that deployment. Set the same value in your host’s environment, or runaces previewsagain after changing it. - A new block doesn’t appear. Check that the component is in the page type’s page body (
aces.config.ts), then runpnpm aces sync --contentful. - The preview shows the published version. The page wasn’t opened through a draft route, so Draft Mode is off. Open it from the entry’s Live Preview pane in Contentful.
Something wrong or missing? Open an issue.