Skip to main content

Preview API

Route: /api/preview

File:

apps/ui/src/app/api/preview/route.ts

This route is used by Strapi preview links.

It:

  1. Validates secret against STRAPI_PREVIEW_SECRET.
  2. Enables or disables Next.js draftMode().
  3. Rewrites the __prerender_bypass cookie with sameSite: "none".
  4. Redirects to the requested localized URL.

The sameSite: "none" cookie rewrite is required for Strapi's iframe-embedded preview. Without it, draft mode may silently fall back to published content.

Secret required

Preview requests return an error unless STRAPI_PREVIEW_SECRET is configured and the secret query param matches it.

STRAPI_URL must match the admin origin

The preview iframe auto-refreshes on save via a postMessage from the Strapi admin, and StrapiPreviewListener only accepts messages whose origin matches STRAPI_URL. Locally, http://localhost:1337 and http://127.0.0.1:1337 are different origins: if STRAPI_URL uses one and you open the admin on the other, every update message is dropped and the iframe silently stays stale — nothing is logged. Set STRAPI_URL to the origin you actually browse.

Draft mode

Draft previews depend on Next.js draft mode cookies. The route adjusts the draft-mode cookie so preview can work inside Strapi's iframe preview flow.

Expected query params:

ParamPurpose
secretMust match STRAPI_PREVIEW_SECRET.
urlPage URL to open.
statusdraft or published; defaults to published.
localeFrontend locale; falls back to the default locale.

Related docs: