@prismicio/next
Version:
Helpers to integrate Prismic into Next.js apps
38 lines (37 loc) • 1.25 kB
JavaScript
require("./_virtual/_rolldown/runtime.cjs");
let _prismicio_client = require("@prismicio/client");
//#region src/getPreviewRef.ts
/**
* Reads the Prismic preview ref for the current request when an active preview session exists.
*
* This is the read-side counterpart to `redirectToPreviewURL`, which writes the preview cookie. Use
* it with Next.js Cache Components to read the ref _outside_ a cached function and pass it _in_ as
* an argument so it becomes part of the cache key:
*
* @example
* ;```typescript
* import { getPreviewRef } from "@prismicio/next"
*
* const page = await fetchPage(uid, await getPreviewRef())
* ```
*
* @returns The active preview ref, or `undefined` if no preview session is active.
* @experimental
*/
async function getPreviewRef() {
const { cookies, draftMode } = await import("next/headers.js");
let isDraftModeEnabled = false;
try {
isDraftModeEnabled = (await draftMode()).isEnabled;
} catch {
return;
}
if (!isDraftModeEnabled) return;
const cookie = (await cookies()).get(_prismicio_client.cookie.preview)?.value;
if (!cookie) return;
if (!cookie.includes("websitePreviewId=")) return;
return cookie;
}
//#endregion
exports.getPreviewRef = getPreviewRef;
//# sourceMappingURL=getPreviewRef.cjs.map