@prismicio/next
Version:
Helpers to integrate Prismic into Next.js apps
37 lines (36 loc) • 1.17 kB
JavaScript
import { cookie } from "@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$1 = (await cookies()).get(cookie.preview)?.value;
if (!cookie$1) return;
if (!cookie$1.includes("websitePreviewId=")) return;
return cookie$1;
}
//#endregion
export { getPreviewRef };
//# sourceMappingURL=getPreviewRef.js.map