Sanity Library Reference Docs
    Preparing search index...

    Function defineEnableDraftMode

    • Sets up an API route for enabling draft mode, can be paired with the previewUrl.previewMode.enable in sanity/presentation. Can also be used with sanity-plugin-iframe-pane.

      When the preview loads in a cross-site iframe (Presentation Tool), draft-mode cookies are set with the CHIPS Partitioned attribute so Safari 18.4+ stores them despite third-party cookie blocking. Top-level / same-site requests keep unpartitioned cookies so draftMode().disable() continues to work.

      Some browsers reject even Partitioned cookies in cross-site iframes, for example Firefox with Enhanced Tracking Protection configured to block all third-party cookies ("Cookie has been rejected as third-party"). Because a silent redirect would then land on a preview that can never enter draft mode, cross-site iframe requests are redirected back to this route once with a probe search param to verify the cookies were stored. If they were not, the route responds with an interstitial that requests unpartitioned cookie access through the Storage Access API and retries, or explains how to unblock the preview when the browser refuses. Requests that carry the Sec-Fetch-Storage-Access: inactive header are asked to retry with the already-granted permission activated (Storage Access Headers).

      Parameters

      Returns EnableDraftMode

      // src/app/api/draft-mode/enable/route.ts

      import { defineEnableDraftMode } from "next-sanity/draft-mode";
      import { client } from "@/sanity/lib/client";

      export const { GET } = defineEnableDraftMode({
      client: client.withConfig({ token: process.env.SANITY_API_READ_TOKEN }),
      });