next-sanity
Version:
Sanity.io toolkit for Next.js
1 lines • 3.77 kB
Source Map (JSON)
{"version":3,"file":"draft-mode.cjs","sources":["../src/draft-mode/define-enable-draft-mode.ts"],"sourcesContent":["import {validatePreviewUrl} from '@sanity/preview-url-secret'\nimport {perspectiveCookieName} from '@sanity/preview-url-secret/constants'\nimport {cookies, draftMode} from 'next/headers'\nimport {redirect} from 'next/navigation'\n\nimport type {SanityClient} from '../client'\n\n/**\n * @public\n */\nexport interface DefineEnableDraftModeOptions {\n client: SanityClient\n}\n\n/**\n * @public\n */\nexport interface EnableDraftMode {\n GET: (request: Request) => Promise<Response>\n}\n\n/**\n * Sets up an API route for enabling draft mode, can be paired with the `previewUrl.previewMode.enable` in `sanity/presentation`.\n * Can also be used with `sanity-plugin-iframe-pane`.\n * @example\n * ```ts\n * // src/app/api/draft-mode/enable/route.ts\n *\n * import { defineEnableDraftMode } from \"next-sanity/draft-mode\";\n * import { client } from \"@/sanity/lib/client\";\n *\n * export const { GET } = defineEnableDraftMode({\n * client: client.withConfig({ token: process.env.SANITY_API_READ_TOKEN }),\n * });\n * ```\n *\n * @public\n */\nexport function defineEnableDraftMode(options: DefineEnableDraftModeOptions): EnableDraftMode {\n const {client} = options\n return {\n GET: async (request: Request) => {\n // eslint-disable-next-line no-warning-comments\n // @TODO check if already in draft mode at a much earlier stage, and skip validation\n\n const {\n isValid,\n redirectTo = '/',\n studioPreviewPerspective,\n } = await validatePreviewUrl(client, request.url)\n if (!isValid) {\n return new Response('Invalid secret', {status: 401})\n }\n\n const draftModeStore = await draftMode()\n\n // Let's enable draft mode if it's not already enabled\n if (!draftModeStore.isEnabled) {\n draftModeStore.enable()\n }\n\n const dev = process.env.NODE_ENV !== 'production'\n\n // Override cookie header for draft mode for usage in live-preview\n // https://github.com/vercel/next.js/issues/49927\n const cookieStore = await cookies()\n const cookie = cookieStore.get('__prerender_bypass')!\n cookieStore.set({\n name: '__prerender_bypass',\n value: cookie?.value,\n httpOnly: true,\n path: '/',\n secure: !dev,\n sameSite: dev ? 'lax' : 'none',\n })\n\n if (studioPreviewPerspective) {\n cookieStore.set({\n name: perspectiveCookieName,\n value: studioPreviewPerspective,\n httpOnly: true,\n path: '/',\n secure: !dev,\n sameSite: dev ? 'lax' : 'none',\n })\n }\n\n // the `redirect` function throws, and eventually returns a Promise<Response>. TSC doesn't \"see\" that so we have to tell it\n return redirect(redirectTo) as Promise<Response>\n },\n }\n}\n"],"names":["validatePreviewUrl","draftMode","cookies","perspectiveCookieName","redirect"],"mappings":";;;AAsCO,SAAS,sBAAsB,SAAwD;AACtF,QAAA,EAAC,WAAU;AACV,SAAA;AAAA,IACL,KAAK,OAAO,YAAqB;AAIzB,YAAA;AAAA,QACJ;AAAA,QACA,aAAa;AAAA,QACb;AAAA,MACE,IAAA,MAAMA,iBAAmB,mBAAA,QAAQ,QAAQ,GAAG;AAChD,UAAI,CAAC;AACH,eAAO,IAAI,SAAS,kBAAkB,EAAC,QAAQ,KAAI;AAG/C,YAAA,iBAAiB,MAAMC,kBAAU;AAGlC,qBAAe,aAClB,eAAe,OAAO;AAGxB,YAAM,MAAM,QAAQ,IAAI,aAAa,cAI/B,cAAc,MAAMC,QAAAA,QACpB,GAAA,SAAS,YAAY,IAAI,oBAAoB;AACnD,aAAA,YAAY,IAAI;AAAA,QACd,MAAM;AAAA,QACN,OAAO,QAAQ;AAAA,QACf,UAAU;AAAA,QACV,MAAM;AAAA,QACN,QAAQ,CAAC;AAAA,QACT,UAAU,MAAM,QAAQ;AAAA,MAAA,CACzB,GAEG,4BACF,YAAY,IAAI;AAAA,QACd,MAAMC,UAAA;AAAA,QACN,OAAO;AAAA,QACP,UAAU;AAAA,QACV,MAAM;AAAA,QACN,QAAQ,CAAC;AAAA,QACT,UAAU,MAAM,QAAQ;AAAA,MAAA,CACzB,GAIIC,WAAA,SAAS,UAAU;AAAA,IAAA;AAAA,EAE9B;AACF;;"}