next-sanity
Version:
Sanity.io toolkit for Next.js
83 lines (82 loc) • 3.68 kB
JavaScript
import { t as sanitizePerspective } from "./sanitizePerspective.js";
import { perspectiveCookieName } 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";
}
export { preconnect$1 as i, validateStrictFetchOptions as n, validateStrictSanityLiveProps as r, resolvePerspectiveFromCookies as t };
//# sourceMappingURL=resolvePerspectiveFromCookies.js.map