next-sanity
Version:
Sanity.io toolkit for Next.js
70 lines (69 loc) • 2.32 kB
JavaScript
import { t as isCorsOriginError } from "../../../isCorsOriginError.js";
import { t as parseTags } from "../../../parseTags.js";
function defineLive(_config) {
throw new Error(`defineLive can't be imported by a client component`);
}
/**
* 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
*/
const resolvePerspectiveFromCookies = () => {
throw new Error(`resolvePerspectiveFromCookies can't be imported by a client component`);
};
export { defineLive, isCorsOriginError, parseTags, resolvePerspectiveFromCookies };
//# sourceMappingURL=index.js.map