createVexDraftModeRoute
createVexDraftModeRoute(
props):object
Defined in: packages/next/src/livePreview/createVexDraftModeRoute.ts:123
Creates the Next.js Route Handler that enables or disables Draft Mode for a live-preview session, then redirects back to the previewed page.
Why a Route Handler, not middleware. draftMode().enable()/.disable() are
only callable from a Route Handler or Server Action — Next.js explicitly rejects
calling them from middleware (it needs the full Node cookies() write path
middleware’s Edge runtime doesn’t give it). The project’s own proxy/middleware
(proxy in the generated proxy.ts) still owns the first touch of a
?vexLivePreview=1 request; once it sees that request has NOT already
round-tripped through here (both LIVE_PREVIEW_COOKIE and the real
DRAFT_MODE_BYPASS_COOKIE present is the signal — see that proxy’s own
doc comment), it redirects here instead of passing the request straight through.
Why this matters for a never-published document. A public page route calls its
published-only query (access.bypass: true) on the server and 404s when nothing
comes back, so the page stays statically prerenderable. A document with only a draft
row — never published — has nothing for that query to find, so the 404 fired before
the client-side live-preview layer (which DOES know how to read the draft, via a
second, readDrafts-gated query) ever got to mount. Enabling Draft Mode here lets
that page check (await draftMode()).isEnabled and skip the 404 instead, handing
the document to the client layer — without losing static prerendering for every
visitor outside draft mode, which is exactly what Draft Mode is for.
Session-authorized, then permission-checked. Same first bar as
createVexRevalidateRoute: a signed-in session, checked with the host app’s own
getToken, not a shared secret. A session alone is not enough: getRequestAuth resolves
the caller and canAccessLivePreview requires that they could preview at least one
collection or global (readDrafts, or read where drafts are off). A caller who
cannot — an anonymous demo session, say — gets Draft Mode turned off and is sent
back to the page without vexLivePreview=1, so the proxy does not hand them off
again. Draft content itself stays gated behind each query’s own permission check.
Three ways to land here, two outcomes:
- Enable — a session is present, no
?disable=1, andredirectis a valid same-origin path. Calls.enable(), sets LIVE_PREVIEW_COOKIE, redirects. - Explicit disable —
?disable=1, with or without a session (leaving preview mode needs no authorization — it only turns a feature off). Calls.disable(), clears LIVE_PREVIEW_COOKIE, redirects toredirector/if absent. - Signed-out disable — no session, no
?disable=1, butredirectIS present. This is the shape the proxy hands off (redirectis always the page the visitor was on): a session that expired between the page load and this request should not leave a stale__prerender_bypasscookie granting Draft Mode to a now- anonymous visitor, so this disables and redirects exactly like an explicit disable rather than leaving them on a bare error page.
Signed-out stays distinguishable when there’s nothing to redirect to. No
session, no ?disable=1, AND no redirect param is the one case that still
returns 401 — a caller hitting this route directly (not through the proxy’s
hand-off, which always sets redirect) gets a real “you are not authorized”
signal instead of being silently bounced to /.
Open-redirect guarded on every redirect. The redirect search param, when
present, must be a same-origin relative path (a leading / that is not // or
/\, the two shapes a browser would resolve as protocol-relative) — anything
else is rejected with 400, never followed, regardless of which of the three
outcomes above the request would otherwise take.
Parameters
Section titled “Parameters”The host app’s session accessor, caller resolver and config.
Returns
Section titled “Returns”object
{ GET } — mount directly as the route module’s named export.
GET: (
request) =>Promise<NextResponse<unknown>>
Parameters
Section titled “Parameters”request
Section titled “request”NextRequest
Returns
Section titled “Returns”Promise<NextResponse<unknown>>
Example
Section titled “Example”import { createVexDraftModeRoute } from "@vexcms/next/livePreview";import { getToken, getUserOrgForVexApi } from "~/auth/server";import config from "~/vex.config.server";
export const { GET } = createVexDraftModeRoute({ getToken, getRequestAuth: getUserOrgForVexApi, config });