UNPKG

next-sanity

Version:
164 lines 5.29 kB
import { i as LivePerspective } from "./types.js"; import { cookies } from "next/headers"; import { SyncTag } from "@sanity/client"; /** * 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 */ declare function resolvePerspectiveFromCookies({ cookies: jar }: { cookies: Awaited<ReturnType<typeof cookies>>; }): Promise<LivePerspective>; /** * 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 */ declare function resolveVariantFromCookies({ cookies: jar }: { cookies: Awaited<ReturnType<typeof cookies>>; }): Promise<string | undefined>; declare const cacheTagPrefix: "sanity:"; interface ParsedTags { tags: `${typeof cacheTagPrefix}${SyncTag}`[]; tagsWithoutPrefix: SyncTag[]; prefix: typeof cacheTagPrefix; } /** * Prefixes live event tags according to the conventions used by `defineLive().sanityFetch()` * so that they can be used with `import {updateTag} from 'next/cache'`. * * @example * ```tsx * import {updateTag} from 'next/cache' * import {parseTags} from 'next-sanity/live' * import {SanityLive} from '#sanity/live * * <SanityLive * action={async (event, context) => { * 'use server' * * for (const tag of parseTags(event.tags, context)) { * updateTag(tag) * } * }} * /> * ``` */ declare function parseTags(unsafeTags: unknown): ParsedTags; export { resolveVariantFromCookies as n, resolvePerspectiveFromCookies as r, parseTags as t }; //# sourceMappingURL=parseTags.d.ts.map