UNPKG

next-sanity

Version:
152 lines (151 loc) 5.92 kB
import { n as sanitizePerspective, t as sanitizeVariant } from "./sanitizeVariant.js"; import { perspectiveCookieName, variantCookieName } from "@sanity/preview-url-secret/constants"; import { preconnect } from "react-dom"; import { generateHelpUrl } from "@sanity/generate-help-url"; /** * Uses the React DOM `preconnect` function to preconnect to the Live Event API origin early, Next.js will set the right headers and meta tags to speed up the connection. */ function preconnect$1(client) { const { origin } = new URL(client.getUrl("", false)); preconnect(origin); } function validateStrictSanityLiveProps(props) { if (typeof props.includeDrafts !== "boolean") throw new Error(`<SanityLive> requires an explicit \`includeDrafts\` prop (true or false) when \`strict: true\` is set on \`defineLive\`.\n\nMore information: ${generateHelpUrl("next-sanity-live-strict")}`, { cause: props }); } function validateStrictFetchOptions(options) { if (typeof options.perspective === "undefined" || options.perspective === null) throw new Error(`sanityFetch() requires an explicit \`perspective\` option when \`strict: true\` is set on \`defineLive\`.\n\nMore information: ${generateHelpUrl("next-sanity-fetch-strict")}`, { cause: options }); if (typeof options.stega !== "boolean") throw new Error(`sanityFetch() requires an explicit \`stega\` option (true or false) when \`strict: true\` is set on \`defineLive\`.\n\nMore information: ${generateHelpUrl("next-sanity-fetch-strict")}`, { cause: options }); } /** * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`), * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries. * Resolve the perspective once outside the cache boundary and pass it in as a prop / cache key. * * @example * ```tsx * import {cookies, draftMode} from 'next/headers' * import {defineQuery} from 'next-sanity' * import {resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live' * import {sanityFetch, sanityFetchStaticParams} from '#sanity/live' * * export async function generateStaticParams() { * const query = defineQuery(`*[_type == "page" && defined(slug.current)]{"slug": slug.current}`) * return await sanityFetchStaticParams({query}) * } * * export default async function Page({params}: PageProps<'/[slug]'>) { * const {isEnabled: isDraftMode} = await draftMode() * * if (isDraftMode) { * return ( * <Suspense> * <DynamicPage params={params} /> * </Suspense> * ) * } * * const {slug} = await params * * return <CachedPage slug={slug} perspective="published" stega={false} /> * } * * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) { * const {slug} = await params * const perspective = await resolvePerspectiveFromCookies({cookies: await cookies()}) * * return <CachedPage slug={slug} perspective={perspective} stega /> * } * * async function CachedPage({ * slug, * perspective, * stega, * }: Awaited<PageProps<'/[slug]'>['params']> & { * perspective: LivePerspective * stega: boolean * }) { * 'use cache' * * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`) * const {data} = await sanityFetch({query, params: {slug}, perspective, stega}) * * return <article>...</article> * } * ``` * * @public */ async function resolvePerspectiveFromCookies({ cookies: jar }) { return jar.has(perspectiveCookieName) ? sanitizePerspective(jar.get(perspectiveCookieName)?.value, "drafts") : "drafts"; } /** * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`), * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries. * Resolve the variant once outside the cache boundary and pass it in as a prop / cache key. * * Unlike `resolvePerspectiveFromCookies` there is no fallback value: when no * variant cookie is set (or its value is invalid) it resolves to `undefined`, * meaning "no variant selected" and queries return base content. * * @example * ```tsx * import {cookies, draftMode} from 'next/headers' * import {defineQuery} from 'next-sanity' * import { * resolvePerspectiveFromCookies, * resolveVariantFromCookies, * type LivePerspective, * } from 'next-sanity/live' * import {sanityFetch} from '#sanity/live' * * export default async function Page({params}: PageProps<'/[slug]'>) { * const {isEnabled: isDraftMode} = await draftMode() * * if (isDraftMode) { * return ( * <Suspense> * <DynamicPage params={params} /> * </Suspense> * ) * } * * const {slug} = await params * * return <CachedPage slug={slug} perspective="published" stega={false} /> * } * * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) { * const {slug} = await params * const jar = await cookies() * const perspective = await resolvePerspectiveFromCookies({cookies: jar}) * const variant = await resolveVariantFromCookies({cookies: jar}) * * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega /> * } * * async function CachedPage({ * slug, * perspective, * variant, * stega, * }: Awaited<PageProps<'/[slug]'>['params']> & { * perspective: LivePerspective * variant: string | undefined * stega: boolean * }) { * 'use cache' * * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`) * const {data} = await sanityFetch({query, params: {slug}, perspective, variant, stega}) * * return <article>...</article> * } * ``` * * @public */ async function resolveVariantFromCookies({ cookies: jar }) { return jar.has(variantCookieName) ? sanitizeVariant(jar.get(variantCookieName)?.value) : void 0; } export { preconnect$1 as a, validateStrictSanityLiveProps as i, resolvePerspectiveFromCookies as n, validateStrictFetchOptions as r, resolveVariantFromCookies as t }; //# sourceMappingURL=resolveVariantFromCookies.js.map