Question presented to candidate: "Our marketing site is statically generated from a headless CMS and revalidates every hour. Editors complain they cannot see their changes before publishing. How does Draft Mode solve this, and how would you wire it up?"
What a strong answer should cover:
- Draft Mode is a per-browser switch: a
__prerender_bypasscookie that makes that browser's requests skip the static/ISR output and every data cache, while other visitors keep getting the cached pages. - Enabling: a Route Handler (
/api/draft) that the CMS "Preview" button opens; it checks a shared secret and the slug, calls(await draftMode()).enable()and redirects to a path it built itself (no open redirect). - Rendering:
(await draftMode()).isEnabledlets the page fetch the CMS draft endpoint (with a preview token) and show a "Preview mode" banner with an exit button that callsdisable(). - What gets bypassed: the fetch cache,
'use cache',unstable_cacheand the ISR response cache; draft responses areCache-Control: private, no-cache, no-store. - Security and edge cases: the secret stays server-side, the cookie value is regenerated per build, exit via POST (a prefetched GET
<Link>would clear the cookie).
Clarifying questions expected:
- "Does the CMS serve drafts from the same URL or a separate preview endpoint?" β decides whether the fetch code needs to branch on
isEnabled. - "Who may preview, and how is the secret shared with the CMS?" β the secret check is the only gate.
Code / implementation expected: Yes β the enable Route Handler, a data function that branches on isEnabled, and an exit Server Action.