next-sanity 13.3.0 → 13.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,10 +1,133 @@
1
1
  import { t as isCorsOriginError } from "./isCorsOriginError.js";
2
2
  import { createClient, unstable__adapter, unstable__environment } from "@sanity/client";
3
- import { ClientReturnStega, StegaBranded, StegaCleaned, StegaString, stegaBrand, stegaClean } from "@sanity/client/stega";
3
+ import { ClientReturnStega, StegaBranded, StegaBranded as StegaBranded$1, StegaCleaned, StegaCleaned as StegaCleaned$1, StegaString, stegaBrand, stegaClean } from "@sanity/client/stega";
4
4
  import { CreateDataAttribute, CreateDataAttributeProps, createDataAttribute } from "@sanity/visual-editing/create-data-attribute";
5
+ import { InferComponents as InferComponents$1, InferStrictComponents as InferStrictComponents$1, InferValue as InferValue$1 } from "@portabletext/react";
5
6
  import groq, { defineQuery } from "groq";
6
7
  export * from "@sanity/client";
7
8
  export * from "@portabletext/react";
9
+ /**
10
+ * Widens a query result type to cover both sides of stega branding:
11
+ * the clean TypeGen shape (`sanityFetch` with `stega: false`) and the
12
+ * stega-branded shape (`sanityFetch` when stega may be enabled).
13
+ *
14
+ * This is what lets the `Infer*` types accept `data` from any `sanityFetch`
15
+ * call without requiring a wholesale `stegaClean()`, which would strip the
16
+ * hidden characters Visual Editing needs to make each paragraph clickable.
17
+ */
18
+ type StegaAware<T> = StegaCleaned$1<T> | StegaBranded$1<T>;
19
+ /**
20
+ * Infer the Portable Text array value type from
21
+ * {@link https://www.sanity.io/docs/apis-and-sdks/sanity-typegen | Sanity TypeGen}
22
+ * generated types.
23
+ *
24
+ * Stega-aware version of `InferValue` from `@portabletext/react`: the inferred
25
+ * value type accepts both clean query results (`sanityFetch` with
26
+ * `stega: false`) and stega-branded results (`sanityFetch` when stega may be
27
+ * enabled). Don't clean the value with `stegaClean()` before rendering it,
28
+ * that strips the hidden characters `@sanity/visual-editing` uses to make each
29
+ * paragraph clickable. Instead, use `stegaClean` on individual strings at the
30
+ * point where they're compared against literals.
31
+ *
32
+ * Useful when building a re-usable wrapper component that only takes `value`
33
+ * as an input prop and sets up `components` internally. Pass a TypeGen-
34
+ * generated query result type that contains Portable Text fields - such as
35
+ * an individual query result like `PostQueryResult`, or the `SanityQueries`
36
+ * interface from `@sanity/client` - and `InferValue<T>` returns an array type
37
+ * containing every Portable Text item shape it can find.
38
+ *
39
+ * Always feed `InferValue<T>` query result types, not Sanity schema types.
40
+ * Schema types describe how content is stored, which can differ from how it
41
+ * is queried (e.g. references resolved with `->`).
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * // Re-usable component typed against every registered query. Relies on
46
+ * // `overloadClientMethods` being enabled in `sanity.cli.ts#typegen` (the default).
47
+ * import {
48
+ * PortableText,
49
+ * type InferStrictComponents,
50
+ * type InferValue,
51
+ * type SanityQueries,
52
+ * } from 'next-sanity'
53
+ *
54
+ * type PortableTextValue = InferValue<SanityQueries[keyof SanityQueries]>
55
+ *
56
+ * export function CustomPortableText(props: {value: PortableTextValue}) {
57
+ * const components = {
58
+ * types: {
59
+ * // custom types are autocompleted and fully typed
60
+ * },
61
+ * } satisfies InferStrictComponents<PortableTextValue>
62
+ *
63
+ * return <PortableText components={components} value={props.value} />
64
+ * }
65
+ * ```
66
+ *
67
+ * @example
68
+ * ```tsx
69
+ * // Rendering `sanityFetch` results, with or without stega, type-checks:
70
+ * const {data} = await sanityFetch({query: postQuery, params})
71
+ * return Array.isArray(data?.content) && <CustomPortableText value={data.content} />
72
+ * ```
73
+ */
74
+ type InferValue<T> = InferValue$1<StegaAware<T>>;
75
+ /**
76
+ * Infer Portable Text components from a value type. This matches the inference
77
+ * behavior of the `components` prop on `<PortableText>`.
78
+ *
79
+ * Stega-aware version of `InferComponents` from `@portabletext/react`: the
80
+ * inferred component props cover both clean query results (`sanityFetch` with
81
+ * `stega: false`) and stega-branded results (`sanityFetch` when stega may be
82
+ * enabled), so the same `components` object can render both. Strings that may
83
+ * carry stega payloads stay branded inside component props: render them as-is
84
+ * to keep Visual Editing working, and use `stegaClean` on the individual
85
+ * values you compare against string literals.
86
+ *
87
+ * This is useful when working with
88
+ * {@link https://www.sanity.io/docs/apis-and-sdks/sanity-typegen | Sanity TypeGen},
89
+ * where `defineQuery()` and `sanityFetch()` can infer the shape of Portable
90
+ * Text fields.
91
+ *
92
+ * @example
93
+ * ```tsx
94
+ * import {PortableText, type InferComponents, defineQuery} from 'next-sanity'
95
+ * import {sanityFetch} from '@/sanity/lib/live'
96
+ *
97
+ * export default async function Page({slug}: {slug: string}) {
98
+ * const query = defineQuery(`*[_type == "post" && slug.current == $slug][0]{title,content}`)
99
+ * const {data} = await sanityFetch({query, params: {slug}})
100
+ * const components = {
101
+ * block: {
102
+ * // custom types are autocompleted and fully typed
103
+ * },
104
+ * } satisfies InferComponents<typeof data.content>
105
+ *
106
+ * return (
107
+ * <>
108
+ * ...
109
+ * {Array.isArray(data?.content) && <PortableText components={components} value={data.content} />}
110
+ * </>
111
+ * )
112
+ * }
113
+ * ```
114
+ */
115
+ type InferComponents<T> = InferComponents$1<StegaAware<T>>;
116
+ /**
117
+ * Infer Portable Text components from a value type, requiring handlers for all
118
+ * custom object types and disallowing extra custom object type handlers.
119
+ *
120
+ * Stega-aware version of `InferStrictComponents` from `@portabletext/react`,
121
+ * see {@link InferComponents} for how stega branding is handled.
122
+ *
123
+ * This is used the same way as {@link InferComponents}, but serves a different
124
+ * purpose: `InferComponents` is forgiving and mirrors the inline
125
+ * `<PortableText components={...} />` experience, allowing custom handlers to
126
+ * be omitted and allowing handlers for types that do not exist in, or could not
127
+ * be inferred from, the value type. `InferStrictComponents` is strict: it
128
+ * requires inferred custom handlers and rejects unknown ones.
129
+ */
130
+ type InferStrictComponents<T> = InferStrictComponents$1<StegaAware<T>>;
8
131
  /**
9
132
  * API version required for editing variants queries.
10
133
  * Uses the `X` alias while variants are in beta; will move to a dated version when stable.
@@ -12,5 +135,5 @@ export * from "@portabletext/react";
12
135
  * @public
13
136
  */
14
137
  declare const variantsApiVersion = "X";
15
- export { type ClientReturnStega, type CreateDataAttribute, type CreateDataAttributeProps, type StegaBranded, type StegaCleaned, type StegaString, createClient, createDataAttribute, defineQuery, groq, isCorsOriginError, stegaBrand, stegaClean, unstable__adapter, unstable__environment, variantsApiVersion };
138
+ export { type ClientReturnStega, type CreateDataAttribute, type CreateDataAttributeProps, type InferComponents, type InferStrictComponents, type InferValue, type StegaBranded, type StegaCleaned, type StegaString, createClient, createDataAttribute, defineQuery, groq, isCorsOriginError, stegaBrand, stegaClean, unstable__adapter, unstable__environment, variantsApiVersion };
16
139
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/variants/constants.ts"],"mappings":";;;;;;;;;;;;;cAMa"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/portable-text.ts","../src/variants/constants.ts"],"mappings":";;;;;;;;;;;;;;;;;KAgBK,WAAW,KAAK,eAAa,KAAK,eAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAyDxC,WAAW,KAAK,aAAuB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA0ClD,gBAAgB,KAAK,kBAA4B,WAAW;;;;;;;;;;;;;;;KAgB5D,sBAAsB,KAAK,wBAAkC,WAAW;;;;;;;cC7HvE"}
@@ -29,6 +29,7 @@ import { n as resolveVariantFromCookies$1, r as resolvePerspectiveFromCookies$1,
29
29
  * resolvePerspectiveFromCookies,
30
30
  * resolveVariantFromCookies,
31
31
  * type LivePerspective,
32
+ * type StrictDefinedFetchType,
32
33
  * } from 'next-sanity/live'
33
34
  *
34
35
  * const client = createClient({
@@ -46,6 +47,14 @@ import { n as resolveVariantFromCookies$1, r as resolvePerspectiveFromCookies$1,
46
47
  * strict: true,
47
48
  * })
48
49
  *
50
+ * // The app's one shared 'use cache' boundary. `sanityFetch` calls
51
+ * // `cacheTag`/`cacheLife` internally but doesn't create the boundary —
52
+ * // this wrapper provides it once, so callers don't add their own.
53
+ * export const cachedSanity: StrictDefinedFetchType = async (options) => {
54
+ * 'use cache'
55
+ * return sanityFetch(options)
56
+ * }
57
+ *
49
58
  * export interface DynamicFetchOptions {
50
59
  * perspective: LivePerspective
51
60
  * variant?: string
@@ -96,8 +105,8 @@ import { n as resolveVariantFromCookies$1, r as resolvePerspectiveFromCookies$1,
96
105
  * import {defineQuery} from 'next-sanity'
97
106
  *
98
107
  * import {
108
+ * cachedSanity,
99
109
  * getDynamicFetchOptions,
100
- * sanityFetch,
101
110
  * type DynamicFetchOptions,
102
111
  * } from '@/sanity/live'
103
112
  *
@@ -109,7 +118,7 @@ import { n as resolveVariantFromCookies$1, r as resolvePerspectiveFromCookies$1,
109
118
  * `)
110
119
  *
111
120
  * export async function generateStaticParams() {
112
- * const {data} = await sanityFetch({
121
+ * const {data} = await cachedSanity({
113
122
  * query: POSTS_SLUGS_QUERY,
114
123
  * perspective: 'published',
115
124
  * stega: false,
@@ -145,9 +154,7 @@ import { n as resolveVariantFromCookies$1, r as resolvePerspectiveFromCookies$1,
145
154
  * variant,
146
155
  * stega,
147
156
  * }: {slug: string} & DynamicFetchOptions) {
148
- * 'use cache'
149
- *
150
- * const {data} = await sanityFetch({
157
+ * const {data} = await cachedSanity({
151
158
  * query: POST_QUERY,
152
159
  * params: {slug},
153
160
  * perspective,
@@ -264,11 +271,11 @@ declare function defineLive(config: DefineLiveOptions & {
264
271
  * import {cookies, draftMode} from 'next/headers'
265
272
  * import {defineQuery} from 'next-sanity'
266
273
  * import {resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live'
267
- * import {sanityFetch, sanityFetchStaticParams} from '#sanity/live'
274
+ * import {cachedSanity, cachedSanityStaticParams} from '#sanity/live'
268
275
  *
269
276
  * export async function generateStaticParams() {
270
277
  * const query = defineQuery(`*[_type == "page" && defined(slug.current)]{"slug": slug.current}`)
271
- * return await sanityFetchStaticParams({query})
278
+ * return await cachedSanityStaticParams({query})
272
279
  * }
273
280
  *
274
281
  * export default async function Page({params}: PageProps<'/[slug]'>) {
@@ -302,10 +309,8 @@ declare function defineLive(config: DefineLiveOptions & {
302
309
  * perspective: LivePerspective
303
310
  * stega: boolean
304
311
  * }) {
305
- * 'use cache'
306
- *
307
312
  * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`)
308
- * const {data} = await sanityFetch({query, params: {slug}, perspective, stega})
313
+ * const {data} = await cachedSanity({query, params: {slug}, perspective, stega})
309
314
  *
310
315
  * return <article>...</article>
311
316
  * }
@@ -332,7 +337,7 @@ declare const resolvePerspectiveFromCookies: typeof resolvePerspectiveFromCookie
332
337
  * resolveVariantFromCookies,
333
338
  * type LivePerspective,
334
339
  * } from 'next-sanity/live'
335
- * import {sanityFetch} from '#sanity/live'
340
+ * import {cachedSanity} from '#sanity/live'
336
341
  *
337
342
  * export default async function Page({params}: PageProps<'/[slug]'>) {
338
343
  * const {isEnabled: isDraftMode} = await draftMode()
@@ -369,10 +374,8 @@ declare const resolvePerspectiveFromCookies: typeof resolvePerspectiveFromCookie
369
374
  * variant: string | undefined
370
375
  * stega: boolean
371
376
  * }) {
372
- * 'use cache'
373
- *
374
377
  * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`)
375
- * const {data} = await sanityFetch({query, params: {slug}, perspective, variant, stega})
378
+ * const {data} = await cachedSanity({query, params: {slug}, perspective, variant, stega})
376
379
  *
377
380
  * return <article>...</article>
378
381
  * }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../../../../src/live/conditions/default/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0KgB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmFlB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAoFrB,sCAAsC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAsEtC,kCAAkC"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../../../src/live/conditions/default/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAiLgB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmFlB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAkFrB,sCAAsC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAoEtC,kCAAkC"}
@@ -13,11 +13,11 @@ function defineLive(_config) {
13
13
  * import {cookies, draftMode} from 'next/headers'
14
14
  * import {defineQuery} from 'next-sanity'
15
15
  * import {resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live'
16
- * import {sanityFetch, sanityFetchStaticParams} from '#sanity/live'
16
+ * import {cachedSanity, cachedSanityStaticParams} from '#sanity/live'
17
17
  *
18
18
  * export async function generateStaticParams() {
19
19
  * const query = defineQuery(`*[_type == "page" && defined(slug.current)]{"slug": slug.current}`)
20
- * return await sanityFetchStaticParams({query})
20
+ * return await cachedSanityStaticParams({query})
21
21
  * }
22
22
  *
23
23
  * export default async function Page({params}: PageProps<'/[slug]'>) {
@@ -51,10 +51,8 @@ function defineLive(_config) {
51
51
  * perspective: LivePerspective
52
52
  * stega: boolean
53
53
  * }) {
54
- * 'use cache'
55
- *
56
54
  * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`)
57
- * const {data} = await sanityFetch({query, params: {slug}, perspective, stega})
55
+ * const {data} = await cachedSanity({query, params: {slug}, perspective, stega})
58
56
  *
59
57
  * return <article>...</article>
60
58
  * }
@@ -83,7 +81,7 @@ const resolvePerspectiveFromCookies = () => {
83
81
  * resolveVariantFromCookies,
84
82
  * type LivePerspective,
85
83
  * } from 'next-sanity/live'
86
- * import {sanityFetch} from '#sanity/live'
84
+ * import {cachedSanity} from '#sanity/live'
87
85
  *
88
86
  * export default async function Page({params}: PageProps<'/[slug]'>) {
89
87
  * const {isEnabled: isDraftMode} = await draftMode()
@@ -120,10 +118,8 @@ const resolvePerspectiveFromCookies = () => {
120
118
  * variant: string | undefined
121
119
  * stega: boolean
122
120
  * }) {
123
- * 'use cache'
124
- *
125
121
  * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`)
126
- * const {data} = await sanityFetch({query, params: {slug}, perspective, variant, stega})
122
+ * const {data} = await cachedSanity({query, params: {slug}, perspective, variant, stega})
127
123
  *
128
124
  * return <article>...</article>
129
125
  * }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../../../src/live/conditions/default/index.ts"],"sourcesContent":["import type {resolvePerspectiveFromCookies as _resolvePerspectiveFromCookies} from '#live/resolvePerspectiveFromCookies'\nimport type {resolveVariantFromCookies as _resolveVariantFromCookies} from '#live/resolveVariantFromCookies'\nimport type {\n DefinedFetchType,\n DefinedLiveProps,\n DefineLiveOptions,\n StrictDefinedFetchType,\n StrictDefinedLiveProps,\n} from '#live/types'\n\n/**\n * Set up Sanity Live for Cache Components. `defineLive` returns `sanityFetch`\n * and `<SanityLive />`, which connect your Sanity client to the Live Content API\n * so cached pages can update in response to fine-grained content changes.\n *\n * With `strict: true`, `perspective` and `stega` become required\n * `sanityFetch` options, and `includeDrafts` becomes required on\n * `<SanityLive />`. Resolve dynamic values from `draftMode()` and `cookies()`\n * outside `'use cache'` boundaries, then pass them into cached components.\n *\n * `sanityFetch` brands `data` with stega string types when `stega` is `true`,\n * a non-literal `boolean`, or omitted (react-server may auto-enable stega).\n * Pass the literal `stega: false` for clean TypeGen types. Use `stegaClean`\n * before comparing branded strings to literals.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * // sanity/live.ts\n * import {cookies, draftMode} from 'next/headers'\n * import {createClient} from 'next-sanity'\n * import {\n * defineLive,\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * } from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * strict: true,\n * })\n *\n * export interface DynamicFetchOptions {\n * perspective: LivePerspective\n * variant?: string\n * // `boolean` brands `sanityFetch` `data`; use literal `false` for clean types\n * stega: boolean\n * }\n *\n * // Resolve dynamic values outside 'use cache' boundaries.\n * export async function getDynamicFetchOptions(): Promise<DynamicFetchOptions> {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (!isDraftMode) {\n * return {perspective: 'published', stega: false}\n * }\n *\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n * return {perspective: perspective ?? 'drafts', variant, stega: true}\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {draftMode} from 'next/headers'\n *\n * import {SanityLive} from '@/sanity/live'\n *\n * export default async function RootLayout({children}: {children: React.ReactNode}) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive includeDrafts={isDraftMode} />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {draftMode} from 'next/headers'\n * import {Suspense} from 'react'\n * import {defineQuery} from 'next-sanity'\n *\n * import {\n * getDynamicFetchOptions,\n * sanityFetch,\n * type DynamicFetchOptions,\n * } from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await sanityFetch({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (isDraftMode) {\n * return (\n * <Suspense fallback={<div>Loading...</div>}>\n * <DynamicPage params={props.params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await props.params\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage(props: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await props.params\n * const {perspective, variant, stega} = await getDynamicFetchOptions()\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega={stega} />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: {slug: string} & DynamicFetchOptions) {\n * 'use cache'\n *\n * const {data} = await sanityFetch({\n * query: POST_QUERY,\n * params: {slug},\n * perspective,\n * variant,\n * stega,\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict: true}): {\n sanityFetch: StrictDefinedFetchType\n SanityLive: React.ComponentType<StrictDefinedLiveProps>\n}\n/**\n * Set up Sanity Live. `defineLive` returns `sanityFetch` and `<SanityLive />`,\n * which connect your Sanity client to the Live Content API so pages can serve\n * cached content and update in response to fine-grained content changes.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * import {createClient} from 'next-sanity'\n * import {defineLive} from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * })\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {SanityLive} from '@/sanity/live'\n *\n * export default function RootLayout({children}: {children: React.ReactNode}) {\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {defineQuery} from 'next-sanity'\n * import {sanityFetch} from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await sanityFetch({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {slug} = await props.params\n * const {data} = await sanityFetch({\n * query: POST_QUERY,\n * params: {slug},\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict?: false}): {\n sanityFetch: DefinedFetchType\n SanityLive: React.ComponentType<DefinedLiveProps>\n}\nexport function defineLive(_config: DefineLiveOptions): never {\n throw new Error(`defineLive can't be imported by a client component`)\n}\n\nexport type {\n DefinedFetchType,\n DefinedLiveProps,\n DefineLiveOptions,\n LivePerspective,\n SanityLiveAction,\n SanityLiveContext,\n SanityLiveOnError,\n SanityLiveOnGoaway,\n SanityLiveOnReconnect,\n SanityLiveOnRestart,\n SanityLiveOnWelcome,\n StrictDefinedFetchType,\n StrictDefinedLiveProps,\n} from '#live/types'\n\nexport {isCorsOriginError} from '#live/isCorsOriginError'\nexport {parseTags} from '#live/parseTags'\n\n/**\n * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`),\n * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries.\n * Resolve the perspective once outside the cache boundary and pass it in as a prop / cache key.\n *\n * @example\n * ```tsx\n * import {cookies, draftMode} from 'next/headers'\n * import {defineQuery} from 'next-sanity'\n * import {resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live'\n * import {sanityFetch, sanityFetchStaticParams} from '#sanity/live'\n *\n * export async function generateStaticParams() {\n * const query = defineQuery(`*[_type == \"page\" && defined(slug.current)]{\"slug\": slug.current}`)\n * return await sanityFetchStaticParams({query})\n * }\n *\n * export default async function Page({params}: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * if (isDraftMode) {\n * return (\n * <Suspense>\n * <DynamicPage params={params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await params\n *\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await params\n * const perspective = await resolvePerspectiveFromCookies({cookies: await cookies()})\n *\n * return <CachedPage slug={slug} perspective={perspective} stega />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * stega,\n * }: Awaited<PageProps<'/[slug]'>['params']> & {\n * perspective: LivePerspective\n * stega: boolean\n * }) {\n * 'use cache'\n *\n * const query = defineQuery(`*[_type == \"page\" && slug.current == $slug][0]`)\n * const {data} = await sanityFetch({query, params: {slug}, perspective, stega})\n *\n * return <article>...</article>\n * }\n * ```\n *\n * @public\n */\nexport const resolvePerspectiveFromCookies: typeof _resolvePerspectiveFromCookies = () => {\n throw new Error(`resolvePerspectiveFromCookies can't be imported by a client component`)\n}\n\n/**\n * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`),\n * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries.\n * Resolve the variant once outside the cache boundary and pass it in as a prop / cache key.\n *\n * Unlike `resolvePerspectiveFromCookies` there is no fallback value: when no\n * variant cookie is set (or its value is invalid) it resolves to `undefined`,\n * meaning \"no variant selected\" and queries return base content.\n *\n * @example\n * ```tsx\n * import {cookies, draftMode} from 'next/headers'\n * import {defineQuery} from 'next-sanity'\n * import {\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * } from 'next-sanity/live'\n * import {sanityFetch} from '#sanity/live'\n *\n * export default async function Page({params}: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * if (isDraftMode) {\n * return (\n * <Suspense>\n * <DynamicPage params={params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await params\n *\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await params\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: Awaited<PageProps<'/[slug]'>['params']> & {\n * perspective: LivePerspective\n * variant: string | undefined\n * stega: boolean\n * }) {\n * 'use cache'\n *\n * const query = defineQuery(`*[_type == \"page\" && slug.current == $slug][0]`)\n * const {data} = await sanityFetch({query, params: {slug}, perspective, variant, stega})\n *\n * return <article>...</article>\n * }\n * ```\n *\n * @public\n */\nexport const resolveVariantFromCookies: typeof _resolveVariantFromCookies = () => {\n throw new Error(`resolveVariantFromCookies can't be imported by a client component`)\n}\n"],"mappings":";;AAmQA,SAAgB,WAAW,SAAmC;CAC5D,MAAM,IAAI,MAAM,oDAAoD;AACtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgFA,MAAa,sCAA6E;CACxF,MAAM,IAAI,MAAM,uEAAuE;AACzF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEA,MAAa,kCAAqE;CAChF,MAAM,IAAI,MAAM,mEAAmE;AACrF"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../../src/live/conditions/default/index.ts"],"sourcesContent":["import type {resolvePerspectiveFromCookies as _resolvePerspectiveFromCookies} from '#live/resolvePerspectiveFromCookies'\nimport type {resolveVariantFromCookies as _resolveVariantFromCookies} from '#live/resolveVariantFromCookies'\nimport type {\n DefinedFetchType,\n DefinedLiveProps,\n DefineLiveOptions,\n StrictDefinedFetchType,\n StrictDefinedLiveProps,\n} from '#live/types'\n\n/**\n * Set up Sanity Live for Cache Components. `defineLive` returns `sanityFetch`\n * and `<SanityLive />`, which connect your Sanity client to the Live Content API\n * so cached pages can update in response to fine-grained content changes.\n *\n * With `strict: true`, `perspective` and `stega` become required\n * `sanityFetch` options, and `includeDrafts` becomes required on\n * `<SanityLive />`. Resolve dynamic values from `draftMode()` and `cookies()`\n * outside `'use cache'` boundaries, then pass them into cached components.\n *\n * `sanityFetch` brands `data` with stega string types when `stega` is `true`,\n * a non-literal `boolean`, or omitted (react-server may auto-enable stega).\n * Pass the literal `stega: false` for clean TypeGen types. Use `stegaClean`\n * before comparing branded strings to literals.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * // sanity/live.ts\n * import {cookies, draftMode} from 'next/headers'\n * import {createClient} from 'next-sanity'\n * import {\n * defineLive,\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * type StrictDefinedFetchType,\n * } from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * strict: true,\n * })\n *\n * // The app's one shared 'use cache' boundary. `sanityFetch` calls\n * // `cacheTag`/`cacheLife` internally but doesn't create the boundary —\n * // this wrapper provides it once, so callers don't add their own.\n * export const cachedSanity: StrictDefinedFetchType = async (options) => {\n * 'use cache'\n * return sanityFetch(options)\n * }\n *\n * export interface DynamicFetchOptions {\n * perspective: LivePerspective\n * variant?: string\n * // `boolean` brands `sanityFetch` `data`; use literal `false` for clean types\n * stega: boolean\n * }\n *\n * // Resolve dynamic values outside 'use cache' boundaries.\n * export async function getDynamicFetchOptions(): Promise<DynamicFetchOptions> {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (!isDraftMode) {\n * return {perspective: 'published', stega: false}\n * }\n *\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n * return {perspective: perspective ?? 'drafts', variant, stega: true}\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {draftMode} from 'next/headers'\n *\n * import {SanityLive} from '@/sanity/live'\n *\n * export default async function RootLayout({children}: {children: React.ReactNode}) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive includeDrafts={isDraftMode} />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {draftMode} from 'next/headers'\n * import {Suspense} from 'react'\n * import {defineQuery} from 'next-sanity'\n *\n * import {\n * cachedSanity,\n * getDynamicFetchOptions,\n * type DynamicFetchOptions,\n * } from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await cachedSanity({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (isDraftMode) {\n * return (\n * <Suspense fallback={<div>Loading...</div>}>\n * <DynamicPage params={props.params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await props.params\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage(props: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await props.params\n * const {perspective, variant, stega} = await getDynamicFetchOptions()\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega={stega} />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: {slug: string} & DynamicFetchOptions) {\n * const {data} = await cachedSanity({\n * query: POST_QUERY,\n * params: {slug},\n * perspective,\n * variant,\n * stega,\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict: true}): {\n sanityFetch: StrictDefinedFetchType\n SanityLive: React.ComponentType<StrictDefinedLiveProps>\n}\n/**\n * Set up Sanity Live. `defineLive` returns `sanityFetch` and `<SanityLive />`,\n * which connect your Sanity client to the Live Content API so pages can serve\n * cached content and update in response to fine-grained content changes.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * import {createClient} from 'next-sanity'\n * import {defineLive} from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * })\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {SanityLive} from '@/sanity/live'\n *\n * export default function RootLayout({children}: {children: React.ReactNode}) {\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {defineQuery} from 'next-sanity'\n * import {sanityFetch} from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await sanityFetch({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {slug} = await props.params\n * const {data} = await sanityFetch({\n * query: POST_QUERY,\n * params: {slug},\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict?: false}): {\n sanityFetch: DefinedFetchType\n SanityLive: React.ComponentType<DefinedLiveProps>\n}\nexport function defineLive(_config: DefineLiveOptions): never {\n throw new Error(`defineLive can't be imported by a client component`)\n}\n\nexport type {\n DefinedFetchType,\n DefinedLiveProps,\n DefineLiveOptions,\n LivePerspective,\n SanityLiveAction,\n SanityLiveContext,\n SanityLiveOnError,\n SanityLiveOnGoaway,\n SanityLiveOnReconnect,\n SanityLiveOnRestart,\n SanityLiveOnWelcome,\n StrictDefinedFetchType,\n StrictDefinedLiveProps,\n} from '#live/types'\n\nexport {isCorsOriginError} from '#live/isCorsOriginError'\nexport {parseTags} from '#live/parseTags'\n\n/**\n * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`),\n * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries.\n * Resolve the perspective once outside the cache boundary and pass it in as a prop / cache key.\n *\n * @example\n * ```tsx\n * import {cookies, draftMode} from 'next/headers'\n * import {defineQuery} from 'next-sanity'\n * import {resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live'\n * import {cachedSanity, cachedSanityStaticParams} from '#sanity/live'\n *\n * export async function generateStaticParams() {\n * const query = defineQuery(`*[_type == \"page\" && defined(slug.current)]{\"slug\": slug.current}`)\n * return await cachedSanityStaticParams({query})\n * }\n *\n * export default async function Page({params}: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * if (isDraftMode) {\n * return (\n * <Suspense>\n * <DynamicPage params={params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await params\n *\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await params\n * const perspective = await resolvePerspectiveFromCookies({cookies: await cookies()})\n *\n * return <CachedPage slug={slug} perspective={perspective} stega />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * stega,\n * }: Awaited<PageProps<'/[slug]'>['params']> & {\n * perspective: LivePerspective\n * stega: boolean\n * }) {\n * const query = defineQuery(`*[_type == \"page\" && slug.current == $slug][0]`)\n * const {data} = await cachedSanity({query, params: {slug}, perspective, stega})\n *\n * return <article>...</article>\n * }\n * ```\n *\n * @public\n */\nexport const resolvePerspectiveFromCookies: typeof _resolvePerspectiveFromCookies = () => {\n throw new Error(`resolvePerspectiveFromCookies can't be imported by a client component`)\n}\n\n/**\n * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`),\n * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries.\n * Resolve the variant once outside the cache boundary and pass it in as a prop / cache key.\n *\n * Unlike `resolvePerspectiveFromCookies` there is no fallback value: when no\n * variant cookie is set (or its value is invalid) it resolves to `undefined`,\n * meaning \"no variant selected\" and queries return base content.\n *\n * @example\n * ```tsx\n * import {cookies, draftMode} from 'next/headers'\n * import {defineQuery} from 'next-sanity'\n * import {\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * } from 'next-sanity/live'\n * import {cachedSanity} from '#sanity/live'\n *\n * export default async function Page({params}: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * if (isDraftMode) {\n * return (\n * <Suspense>\n * <DynamicPage params={params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await params\n *\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await params\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: Awaited<PageProps<'/[slug]'>['params']> & {\n * perspective: LivePerspective\n * variant: string | undefined\n * stega: boolean\n * }) {\n * const query = defineQuery(`*[_type == \"page\" && slug.current == $slug][0]`)\n * const {data} = await cachedSanity({query, params: {slug}, perspective, variant, stega})\n *\n * return <article>...</article>\n * }\n * ```\n *\n * @public\n */\nexport const resolveVariantFromCookies: typeof _resolveVariantFromCookies = () => {\n throw new Error(`resolveVariantFromCookies can't be imported by a client component`)\n}\n"],"mappings":";;AA0QA,SAAgB,WAAW,SAAmC;CAC5D,MAAM,IAAI,MAAM,oDAAoD;AACtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8EA,MAAa,sCAA6E;CACxF,MAAM,IAAI,MAAM,uEAAuE;AACzF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,MAAa,kCAAqE;CAChF,MAAM,IAAI,MAAM,mEAAmE;AACrF"}
@@ -29,6 +29,7 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
29
29
  * resolvePerspectiveFromCookies,
30
30
  * resolveVariantFromCookies,
31
31
  * type LivePerspective,
32
+ * type StrictDefinedFetchType,
32
33
  * } from 'next-sanity/live'
33
34
  *
34
35
  * const client = createClient({
@@ -46,6 +47,14 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
46
47
  * strict: true,
47
48
  * })
48
49
  *
50
+ * // The app's one shared 'use cache' boundary. `sanityFetch` calls
51
+ * // `cacheTag`/`cacheLife` internally but doesn't create the boundary —
52
+ * // this wrapper provides it once, so callers don't add their own.
53
+ * export const cachedSanity: StrictDefinedFetchType = async (options) => {
54
+ * 'use cache'
55
+ * return sanityFetch(options)
56
+ * }
57
+ *
49
58
  * export interface DynamicFetchOptions {
50
59
  * perspective: LivePerspective
51
60
  * variant?: string
@@ -96,8 +105,8 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
96
105
  * import {defineQuery} from 'next-sanity'
97
106
  *
98
107
  * import {
108
+ * cachedSanity,
99
109
  * getDynamicFetchOptions,
100
- * sanityFetch,
101
110
  * type DynamicFetchOptions,
102
111
  * } from '@/sanity/live'
103
112
  *
@@ -109,7 +118,7 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
109
118
  * `)
110
119
  *
111
120
  * export async function generateStaticParams() {
112
- * const {data} = await sanityFetch({
121
+ * const {data} = await cachedSanity({
113
122
  * query: POSTS_SLUGS_QUERY,
114
123
  * perspective: 'published',
115
124
  * stega: false,
@@ -145,9 +154,7 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
145
154
  * variant,
146
155
  * stega,
147
156
  * }: {slug: string} & DynamicFetchOptions) {
148
- * 'use cache'
149
- *
150
- * const {data} = await sanityFetch({
157
+ * const {data} = await cachedSanity({
151
158
  * query: POST_QUERY,
152
159
  * params: {slug},
153
160
  * perspective,
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../../../../src/live/conditions/next-js/defineLive.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAiLgB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmFlB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../../../src/live/conditions/next-js/defineLive.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAwLgB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmFlB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["sanityCacheLife","SanityLive","SanityLiveClientComponent"],"sources":["../../../../src/live/conditions/next-js/defineLive.tsx"],"sourcesContent":["import {sanity as sanityCacheLife} from 'next-sanity/live/cache-life'\nimport {SanityLive as SanityLiveClientComponent} from 'next-sanity/live/client-components'\nimport {revalidateSyncTagsAction} from 'next-sanity/live/server-actions'\nimport {cacheLife, cacheTag} from 'next/cache'\nimport {PHASE_PRODUCTION_BUILD} from 'next/constants'\n\nimport {cacheTagPrefix, defaultApiHost} from '#live/constants'\nimport {preconnect} from '#live/preconnect'\nimport {validateStrictFetchOptions, validateStrictSanityLiveProps} from '#live/strictValidation'\nimport type {\n DefinedFetchType,\n DefinedLiveProps,\n DefineLiveOptions,\n StrictDefinedFetchType,\n StrictDefinedLiveProps,\n} from '#live/types'\n\n/**\n * Set up Sanity Live for Cache Components. `defineLive` returns `sanityFetch`\n * and `<SanityLive />`, which connect your Sanity client to the Live Content API\n * so cached pages can update in response to fine-grained content changes.\n *\n * With `strict: true`, `perspective` and `stega` become required\n * `sanityFetch` options, and `includeDrafts` becomes required on\n * `<SanityLive />`. Resolve dynamic values from `draftMode()` and `cookies()`\n * outside `'use cache'` boundaries, then pass them into cached components.\n *\n * `sanityFetch` brands `data` with stega string types when `stega` is `true`,\n * a non-literal `boolean`, or omitted (react-server may auto-enable stega).\n * Pass the literal `stega: false` for clean TypeGen types. Use `stegaClean`\n * before comparing branded strings to literals.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * // sanity/live.ts\n * import {cookies, draftMode} from 'next/headers'\n * import {createClient} from 'next-sanity'\n * import {\n * defineLive,\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * } from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * strict: true,\n * })\n *\n * export interface DynamicFetchOptions {\n * perspective: LivePerspective\n * variant?: string\n * // `boolean` brands `sanityFetch` `data`; use literal `false` for clean types\n * stega: boolean\n * }\n *\n * // Resolve dynamic values outside 'use cache' boundaries.\n * export async function getDynamicFetchOptions(): Promise<DynamicFetchOptions> {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (!isDraftMode) {\n * return {perspective: 'published', stega: false}\n * }\n *\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n * return {perspective: perspective ?? 'drafts', variant, stega: true}\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {draftMode} from 'next/headers'\n *\n * import {SanityLive} from '@/sanity/live'\n *\n * export default async function RootLayout({children}: {children: React.ReactNode}) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive includeDrafts={isDraftMode} />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {draftMode} from 'next/headers'\n * import {Suspense} from 'react'\n * import {defineQuery} from 'next-sanity'\n *\n * import {\n * getDynamicFetchOptions,\n * sanityFetch,\n * type DynamicFetchOptions,\n * } from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await sanityFetch({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (isDraftMode) {\n * return (\n * <Suspense fallback={<div>Loading...</div>}>\n * <DynamicPage params={props.params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await props.params\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage(props: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await props.params\n * const {perspective, variant, stega} = await getDynamicFetchOptions()\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega={stega} />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: {slug: string} & DynamicFetchOptions) {\n * 'use cache'\n *\n * const {data} = await sanityFetch({\n * query: POST_QUERY,\n * params: {slug},\n * perspective,\n * variant,\n * stega,\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict: true}): {\n sanityFetch: StrictDefinedFetchType\n SanityLive: React.ComponentType<StrictDefinedLiveProps>\n}\n/**\n * Set up Sanity Live. `defineLive` returns `sanityFetch` and `<SanityLive />`,\n * which connect your Sanity client to the Live Content API so pages can serve\n * cached content and update in response to fine-grained content changes.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * import {createClient} from 'next-sanity'\n * import {defineLive} from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * })\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {SanityLive} from '@/sanity/live'\n *\n * export default function RootLayout({children}: {children: React.ReactNode}) {\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {defineQuery} from 'next-sanity'\n * import {sanityFetch} from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await sanityFetch({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {slug} = await props.params\n * const {data} = await sanityFetch({\n * query: POST_QUERY,\n * params: {slug},\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict?: false}): {\n sanityFetch: DefinedFetchType\n SanityLive: React.ComponentType<DefinedLiveProps>\n}\nexport function defineLive(config: DefineLiveOptions) {\n const {client: _client, serverToken, browserToken, strict = false} = config\n\n if (!_client) {\n throw new Error('`client` is required for `defineLive` to function')\n }\n\n if (process.env.NODE_ENV === 'development' && !serverToken && serverToken !== false) {\n console.warn(\n 'No `serverToken` provided to `defineLive`. This means that only published content will be fetched and respond to live events. You can silence this warning by setting `serverToken: false`.',\n )\n }\n\n if (process.env.NODE_ENV === 'development' && !browserToken && browserToken !== false) {\n console.warn(\n 'No `browserToken` provided to `defineLive`. This means that live previewing drafts will only work when using the Presentation Tool in your Sanity Studio. To support live previewing drafts stand-alone, provide a `browserToken`. It is shared with the browser so it should only have Viewer rights or lower. You can silence this warning by setting `browserToken: false`.',\n )\n }\n\n const client = _client.withConfig({\n allowReconfigure: false,\n useCdn: true,\n perspective: 'published',\n stega: false,\n })\n\n const sanityFetch: DefinedFetchType = async function sanityFetch({\n query,\n params = {},\n perspective,\n variant,\n stega,\n tags: customCacheTags = [],\n requestTag = 'next-loader.fetch.cache-components',\n }) {\n if (strict) {\n validateStrictFetchOptions({perspective, stega})\n }\n\n const useCdn = perspective ? perspective === 'published' : undefined\n const isBuildPhase = process.env['NEXT_PHASE'] === PHASE_PRODUCTION_BUILD\n const cacheMode = useCdn !== false && !isBuildPhase ? 'noStale' : undefined\n const token =\n ((perspective && perspective !== 'published') || stega) && serverToken\n ? serverToken\n : undefined\n\n const {result, resultSourceMap, syncTags} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective,\n variant,\n stega,\n returnQuery: false,\n useCdn,\n cacheMode,\n tag: requestTag,\n token,\n })\n const tags = [...customCacheTags, ...(syncTags || []).map((tag) => `${cacheTagPrefix}${tag}`)]\n /**\n * The tags used here, are expired later on in the `action` Server Action given to `<SanityLive />` with the `revalidateTag` function from `next/cache`,\n * or by a route handler that userland sets up.\n */\n cacheTag(...tags)\n /**\n * Sanity Live handles on-demand revalidation, so the default 15min time-based revalidation is too short,\n * userland can still set a shorter revalidate time by calling `cacheLife` themselves.\n */\n cacheLife(sanityCacheLife)\n\n return {data: result, sourceMap: resultSourceMap || null, tags}\n }\n\n const SanityLive: React.ComponentType<DefinedLiveProps> = function SanityLive(props) {\n if (strict) {\n validateStrictSanityLiveProps(props)\n }\n const {\n includeDrafts: _includeDrafts = false,\n requestTag = 'next-loader.live.cache-components',\n waitFor,\n\n action,\n onError,\n onWelcome,\n onReconnect,\n onRestart,\n onGoAway,\n } = props\n const {projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix} =\n client.config()\n\n const includeDrafts = typeof browserToken === 'string' && !!browserToken && _includeDrafts\n const shouldWaitFor = waitFor === 'function' && !includeDrafts ? waitFor : undefined\n\n // Preconnect to the Live Event API origin early, as the Sanity API is almost always on a different origin than the app\n preconnect(client)\n\n return (\n <SanityLiveClientComponent\n config={{\n projectId,\n dataset,\n apiHost: apiHost === defaultApiHost ? undefined : apiHost,\n apiVersion,\n useProjectHostname: useProjectHostname ? undefined : useProjectHostname,\n requestTagPrefix,\n token: includeDrafts ? browserToken : undefined,\n }}\n includeDrafts={includeDrafts ? true : undefined}\n requestTag={requestTag}\n waitFor={shouldWaitFor}\n action={\n action ??\n (shouldWaitFor === 'function' || includeDrafts ? 'refresh' : revalidateSyncTagsAction)\n }\n onError={onError}\n onWelcome={onWelcome}\n onReconnect={onReconnect}\n onRestart={onRestart}\n onGoAway={onGoAway}\n />\n )\n }\n SanityLive.displayName = 'SanityLiveServerComponent'\n\n return {sanityFetch, SanityLive}\n}\n"],"mappings":";;;;;;;;;;AA0QA,SAAgB,WAAW,QAA2B;CACpD,MAAM,EAAC,QAAQ,SAAS,aAAa,cAAc,SAAS,UAAS;CAErE,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,mDAAmD;CAGrE,IAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,eAAe,gBAAgB,OAC5E,QAAQ,KACN,6LACF;CAGF,IAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,gBAAgB,iBAAiB,OAC9E,QAAQ,KACN,gXACF;CAGF,MAAM,SAAS,QAAQ,WAAW;EAChC,kBAAkB;EAClB,QAAQ;EACR,aAAa;EACb,OAAO;CACT,CAAC;CAED,MAAM,cAAgC,eAAe,YAAY,EAC/D,OACA,SAAS,CAAC,GACV,aACA,SACA,OACA,MAAM,kBAAkB,CAAC,GACzB,aAAa,wCACZ;EACD,IAAI,QACF,2BAA2B;GAAC;GAAa;EAAK,CAAC;EAGjD,MAAM,SAAS,cAAc,gBAAgB,cAAc,KAAA;EAC3D,MAAM,eAAe,QAAQ,IAAI,kBAAkB;EACnD,MAAM,YAAY,WAAW,SAAS,CAAC,eAAe,YAAY,KAAA;EAClE,MAAM,SACF,eAAe,gBAAgB,eAAgB,UAAU,cACvD,cACA,KAAA;EAEN,MAAM,EAAC,QAAQ,iBAAiB,aAAY,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GAClF,gBAAgB;GAChB;GACA;GACA;GACA,aAAa;GACb;GACA;GACA,KAAK;GACL;EACF,CAAC;EACD,MAAM,OAAO,CAAC,GAAG,iBAAiB,IAAI,YAAY,CAAC,EAAA,CAAG,KAAK,QAAQ,GAAG,iBAAiB,KAAK,CAAC;;;;;EAK7F,SAAS,GAAG,IAAI;;;;;EAKhB,UAAUA,MAAe;EAEzB,OAAO;GAAC,MAAM;GAAQ,WAAW,mBAAmB;GAAM;EAAI;CAChE;CAEA,MAAMC,eAAoD,SAASA,aAAW,OAAO;EACnF,IAAI,QACF,8BAA8B,KAAK;EAErC,MAAM,EACJ,eAAe,iBAAiB,OAChC,aAAa,qCACb,SAEA,QACA,SACA,WACA,aACA,WACA,aACE;EACJ,MAAM,EAAC,WAAW,SAAS,SAAS,YAAY,oBAAoB,qBAClE,OAAO,OAAO;EAEhB,MAAM,gBAAgB,OAAO,iBAAiB,YAAY,CAAC,CAAC,gBAAgB;EAC5E,MAAM,gBAAgB,YAAY,cAAc,CAAC,gBAAgB,UAAU,KAAA;EAG3E,WAAW,MAAM;EAEjB,OACE,oBAACC,YAAD;GACE,QAAQ;IACN;IACA;IACA,SAAS,YAAA,0BAA6B,KAAA,IAAY;IAClD;IACA,oBAAoB,qBAAqB,KAAA,IAAY;IACrD;IACA,OAAO,gBAAgB,eAAe,KAAA;GACxC;GACA,eAAe,gBAAgB,OAAO,KAAA;GAC1B;GACZ,SAAS;GACT,QACE,WACC,kBAAkB,cAAc,gBAAgB,YAAY;GAEtD;GACE;GACE;GACF;GACD;EACX,CAAA;CAEL;CACA,aAAW,cAAc;CAEzB,OAAO;EAAC;EAAa,YAAA;CAAU;AACjC"}
1
+ {"version":3,"file":"index.js","names":["sanityCacheLife","SanityLive","SanityLiveClientComponent"],"sources":["../../../../src/live/conditions/next-js/defineLive.tsx"],"sourcesContent":["import {sanity as sanityCacheLife} from 'next-sanity/live/cache-life'\nimport {SanityLive as SanityLiveClientComponent} from 'next-sanity/live/client-components'\nimport {revalidateSyncTagsAction} from 'next-sanity/live/server-actions'\nimport {cacheLife, cacheTag} from 'next/cache'\nimport {PHASE_PRODUCTION_BUILD} from 'next/constants'\n\nimport {cacheTagPrefix, defaultApiHost} from '#live/constants'\nimport {preconnect} from '#live/preconnect'\nimport {validateStrictFetchOptions, validateStrictSanityLiveProps} from '#live/strictValidation'\nimport type {\n DefinedFetchType,\n DefinedLiveProps,\n DefineLiveOptions,\n StrictDefinedFetchType,\n StrictDefinedLiveProps,\n} from '#live/types'\n\n/**\n * Set up Sanity Live for Cache Components. `defineLive` returns `sanityFetch`\n * and `<SanityLive />`, which connect your Sanity client to the Live Content API\n * so cached pages can update in response to fine-grained content changes.\n *\n * With `strict: true`, `perspective` and `stega` become required\n * `sanityFetch` options, and `includeDrafts` becomes required on\n * `<SanityLive />`. Resolve dynamic values from `draftMode()` and `cookies()`\n * outside `'use cache'` boundaries, then pass them into cached components.\n *\n * `sanityFetch` brands `data` with stega string types when `stega` is `true`,\n * a non-literal `boolean`, or omitted (react-server may auto-enable stega).\n * Pass the literal `stega: false` for clean TypeGen types. Use `stegaClean`\n * before comparing branded strings to literals.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * // sanity/live.ts\n * import {cookies, draftMode} from 'next/headers'\n * import {createClient} from 'next-sanity'\n * import {\n * defineLive,\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * type StrictDefinedFetchType,\n * } from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * strict: true,\n * })\n *\n * // The app's one shared 'use cache' boundary. `sanityFetch` calls\n * // `cacheTag`/`cacheLife` internally but doesn't create the boundary —\n * // this wrapper provides it once, so callers don't add their own.\n * export const cachedSanity: StrictDefinedFetchType = async (options) => {\n * 'use cache'\n * return sanityFetch(options)\n * }\n *\n * export interface DynamicFetchOptions {\n * perspective: LivePerspective\n * variant?: string\n * // `boolean` brands `sanityFetch` `data`; use literal `false` for clean types\n * stega: boolean\n * }\n *\n * // Resolve dynamic values outside 'use cache' boundaries.\n * export async function getDynamicFetchOptions(): Promise<DynamicFetchOptions> {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (!isDraftMode) {\n * return {perspective: 'published', stega: false}\n * }\n *\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n * return {perspective: perspective ?? 'drafts', variant, stega: true}\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {draftMode} from 'next/headers'\n *\n * import {SanityLive} from '@/sanity/live'\n *\n * export default async function RootLayout({children}: {children: React.ReactNode}) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive includeDrafts={isDraftMode} />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {draftMode} from 'next/headers'\n * import {Suspense} from 'react'\n * import {defineQuery} from 'next-sanity'\n *\n * import {\n * cachedSanity,\n * getDynamicFetchOptions,\n * type DynamicFetchOptions,\n * } from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await cachedSanity({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (isDraftMode) {\n * return (\n * <Suspense fallback={<div>Loading...</div>}>\n * <DynamicPage params={props.params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await props.params\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage(props: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await props.params\n * const {perspective, variant, stega} = await getDynamicFetchOptions()\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega={stega} />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: {slug: string} & DynamicFetchOptions) {\n * const {data} = await cachedSanity({\n * query: POST_QUERY,\n * params: {slug},\n * perspective,\n * variant,\n * stega,\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict: true}): {\n sanityFetch: StrictDefinedFetchType\n SanityLive: React.ComponentType<StrictDefinedLiveProps>\n}\n/**\n * Set up Sanity Live. `defineLive` returns `sanityFetch` and `<SanityLive />`,\n * which connect your Sanity client to the Live Content API so pages can serve\n * cached content and update in response to fine-grained content changes.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * import {createClient} from 'next-sanity'\n * import {defineLive} from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * })\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {SanityLive} from '@/sanity/live'\n *\n * export default function RootLayout({children}: {children: React.ReactNode}) {\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {defineQuery} from 'next-sanity'\n * import {sanityFetch} from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await sanityFetch({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {slug} = await props.params\n * const {data} = await sanityFetch({\n * query: POST_QUERY,\n * params: {slug},\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict?: false}): {\n sanityFetch: DefinedFetchType\n SanityLive: React.ComponentType<DefinedLiveProps>\n}\nexport function defineLive(config: DefineLiveOptions) {\n const {client: _client, serverToken, browserToken, strict = false} = config\n\n if (!_client) {\n throw new Error('`client` is required for `defineLive` to function')\n }\n\n if (process.env.NODE_ENV === 'development' && !serverToken && serverToken !== false) {\n console.warn(\n 'No `serverToken` provided to `defineLive`. This means that only published content will be fetched and respond to live events. You can silence this warning by setting `serverToken: false`.',\n )\n }\n\n if (process.env.NODE_ENV === 'development' && !browserToken && browserToken !== false) {\n console.warn(\n 'No `browserToken` provided to `defineLive`. This means that live previewing drafts will only work when using the Presentation Tool in your Sanity Studio. To support live previewing drafts stand-alone, provide a `browserToken`. It is shared with the browser so it should only have Viewer rights or lower. You can silence this warning by setting `browserToken: false`.',\n )\n }\n\n const client = _client.withConfig({\n allowReconfigure: false,\n useCdn: true,\n perspective: 'published',\n stega: false,\n })\n\n const sanityFetch: DefinedFetchType = async function sanityFetch({\n query,\n params = {},\n perspective,\n variant,\n stega,\n tags: customCacheTags = [],\n requestTag = 'next-loader.fetch.cache-components',\n }) {\n if (strict) {\n validateStrictFetchOptions({perspective, stega})\n }\n\n const useCdn = perspective ? perspective === 'published' : undefined\n const isBuildPhase = process.env['NEXT_PHASE'] === PHASE_PRODUCTION_BUILD\n const cacheMode = useCdn !== false && !isBuildPhase ? 'noStale' : undefined\n const token =\n ((perspective && perspective !== 'published') || stega) && serverToken\n ? serverToken\n : undefined\n\n const {result, resultSourceMap, syncTags} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective,\n variant,\n stega,\n returnQuery: false,\n useCdn,\n cacheMode,\n tag: requestTag,\n token,\n })\n const tags = [...customCacheTags, ...(syncTags || []).map((tag) => `${cacheTagPrefix}${tag}`)]\n /**\n * The tags used here, are expired later on in the `action` Server Action given to `<SanityLive />` with the `revalidateTag` function from `next/cache`,\n * or by a route handler that userland sets up.\n */\n cacheTag(...tags)\n /**\n * Sanity Live handles on-demand revalidation, so the default 15min time-based revalidation is too short,\n * userland can still set a shorter revalidate time by calling `cacheLife` themselves.\n */\n cacheLife(sanityCacheLife)\n\n return {data: result, sourceMap: resultSourceMap || null, tags}\n }\n\n const SanityLive: React.ComponentType<DefinedLiveProps> = function SanityLive(props) {\n if (strict) {\n validateStrictSanityLiveProps(props)\n }\n const {\n includeDrafts: _includeDrafts = false,\n requestTag = 'next-loader.live.cache-components',\n waitFor,\n\n action,\n onError,\n onWelcome,\n onReconnect,\n onRestart,\n onGoAway,\n } = props\n const {projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix} =\n client.config()\n\n const includeDrafts = typeof browserToken === 'string' && !!browserToken && _includeDrafts\n const shouldWaitFor = waitFor === 'function' && !includeDrafts ? waitFor : undefined\n\n // Preconnect to the Live Event API origin early, as the Sanity API is almost always on a different origin than the app\n preconnect(client)\n\n return (\n <SanityLiveClientComponent\n config={{\n projectId,\n dataset,\n apiHost: apiHost === defaultApiHost ? undefined : apiHost,\n apiVersion,\n useProjectHostname: useProjectHostname ? undefined : useProjectHostname,\n requestTagPrefix,\n token: includeDrafts ? browserToken : undefined,\n }}\n includeDrafts={includeDrafts ? true : undefined}\n requestTag={requestTag}\n waitFor={shouldWaitFor}\n action={\n action ??\n (shouldWaitFor === 'function' || includeDrafts ? 'refresh' : revalidateSyncTagsAction)\n }\n onError={onError}\n onWelcome={onWelcome}\n onReconnect={onReconnect}\n onRestart={onRestart}\n onGoAway={onGoAway}\n />\n )\n }\n SanityLive.displayName = 'SanityLiveServerComponent'\n\n return {sanityFetch, SanityLive}\n}\n"],"mappings":";;;;;;;;;;AAiRA,SAAgB,WAAW,QAA2B;CACpD,MAAM,EAAC,QAAQ,SAAS,aAAa,cAAc,SAAS,UAAS;CAErE,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,mDAAmD;CAGrE,IAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,eAAe,gBAAgB,OAC5E,QAAQ,KACN,6LACF;CAGF,IAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,gBAAgB,iBAAiB,OAC9E,QAAQ,KACN,gXACF;CAGF,MAAM,SAAS,QAAQ,WAAW;EAChC,kBAAkB;EAClB,QAAQ;EACR,aAAa;EACb,OAAO;CACT,CAAC;CAED,MAAM,cAAgC,eAAe,YAAY,EAC/D,OACA,SAAS,CAAC,GACV,aACA,SACA,OACA,MAAM,kBAAkB,CAAC,GACzB,aAAa,wCACZ;EACD,IAAI,QACF,2BAA2B;GAAC;GAAa;EAAK,CAAC;EAGjD,MAAM,SAAS,cAAc,gBAAgB,cAAc,KAAA;EAC3D,MAAM,eAAe,QAAQ,IAAI,kBAAkB;EACnD,MAAM,YAAY,WAAW,SAAS,CAAC,eAAe,YAAY,KAAA;EAClE,MAAM,SACF,eAAe,gBAAgB,eAAgB,UAAU,cACvD,cACA,KAAA;EAEN,MAAM,EAAC,QAAQ,iBAAiB,aAAY,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GAClF,gBAAgB;GAChB;GACA;GACA;GACA,aAAa;GACb;GACA;GACA,KAAK;GACL;EACF,CAAC;EACD,MAAM,OAAO,CAAC,GAAG,iBAAiB,IAAI,YAAY,CAAC,EAAA,CAAG,KAAK,QAAQ,GAAG,iBAAiB,KAAK,CAAC;;;;;EAK7F,SAAS,GAAG,IAAI;;;;;EAKhB,UAAUA,MAAe;EAEzB,OAAO;GAAC,MAAM;GAAQ,WAAW,mBAAmB;GAAM;EAAI;CAChE;CAEA,MAAMC,eAAoD,SAASA,aAAW,OAAO;EACnF,IAAI,QACF,8BAA8B,KAAK;EAErC,MAAM,EACJ,eAAe,iBAAiB,OAChC,aAAa,qCACb,SAEA,QACA,SACA,WACA,aACA,WACA,aACE;EACJ,MAAM,EAAC,WAAW,SAAS,SAAS,YAAY,oBAAoB,qBAClE,OAAO,OAAO;EAEhB,MAAM,gBAAgB,OAAO,iBAAiB,YAAY,CAAC,CAAC,gBAAgB;EAC5E,MAAM,gBAAgB,YAAY,cAAc,CAAC,gBAAgB,UAAU,KAAA;EAG3E,WAAW,MAAM;EAEjB,OACE,oBAACC,YAAD;GACE,QAAQ;IACN;IACA;IACA,SAAS,YAAA,0BAA6B,KAAA,IAAY;IAClD;IACA,oBAAoB,qBAAqB,KAAA,IAAY;IACrD;IACA,OAAO,gBAAgB,eAAe,KAAA;GACxC;GACA,eAAe,gBAAgB,OAAO,KAAA;GAC1B;GACZ,SAAS;GACT,QACE,WACC,kBAAkB,cAAc,gBAAgB,YAAY;GAEtD;GACE;GACE;GACF;GACD;EACX,CAAA;CAEL;CACA,aAAW,cAAc;CAEzB,OAAO;EAAC;EAAa,YAAA;CAAU;AACjC"}
@@ -29,6 +29,7 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
29
29
  * resolvePerspectiveFromCookies,
30
30
  * resolveVariantFromCookies,
31
31
  * type LivePerspective,
32
+ * type StrictDefinedFetchType,
32
33
  * } from 'next-sanity/live'
33
34
  *
34
35
  * const client = createClient({
@@ -46,6 +47,14 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
46
47
  * strict: true,
47
48
  * })
48
49
  *
50
+ * // The app's one shared 'use cache' boundary. `sanityFetch` calls
51
+ * // `cacheTag`/`cacheLife` internally but doesn't create the boundary —
52
+ * // this wrapper provides it once, so callers don't add their own.
53
+ * export const cachedSanity: StrictDefinedFetchType = async (options) => {
54
+ * 'use cache'
55
+ * return sanityFetch(options)
56
+ * }
57
+ *
49
58
  * export interface DynamicFetchOptions {
50
59
  * perspective: LivePerspective
51
60
  * variant?: string
@@ -96,8 +105,8 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
96
105
  * import {defineQuery} from 'next-sanity'
97
106
  *
98
107
  * import {
108
+ * cachedSanity,
99
109
  * getDynamicFetchOptions,
100
- * sanityFetch,
101
110
  * type DynamicFetchOptions,
102
111
  * } from '@/sanity/live'
103
112
  *
@@ -109,7 +118,7 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
109
118
  * `)
110
119
  *
111
120
  * export async function generateStaticParams() {
112
- * const {data} = await sanityFetch({
121
+ * const {data} = await cachedSanity({
113
122
  * query: POSTS_SLUGS_QUERY,
114
123
  * perspective: 'published',
115
124
  * stega: false,
@@ -145,9 +154,7 @@ import { n as resolveVariantFromCookies, r as resolvePerspectiveFromCookies, t a
145
154
  * variant,
146
155
  * stega,
147
156
  * }: {slug: string} & DynamicFetchOptions) {
148
- * 'use cache'
149
- *
150
- * const {data} = await sanityFetch({
157
+ * const {data} = await cachedSanity({
151
158
  * query: POST_QUERY,
152
159
  * params: {slug},
153
160
  * perspective,
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../../../../src/live/conditions/react-server/defineLive.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmLgB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmFlB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../../../src/live/conditions/react-server/defineLive.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0LgB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmFlB,WAAW,QAAQ;EAAqB;;EACtD,aAAa;EACb,YAAY,MAAM,cAAc"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["SanityLive","SanityLiveClientComponent"],"sources":["../../../../src/live/conditions/react-server/defineLive.tsx"],"sourcesContent":["import {SanityLive as SanityLiveClientComponent} from 'next-sanity/live/client-components'\nimport {revalidateSyncTagsAction} from 'next-sanity/live/server-actions'\nimport {PHASE_PRODUCTION_BUILD} from 'next/constants'\nimport {cookies, draftMode} from 'next/headers'\n\nimport {cacheTagPrefix, defaultApiHost} from '#live/constants'\nimport {preconnect} from '#live/preconnect'\nimport {resolvePerspectiveFromCookies} from '#live/resolvePerspectiveFromCookies'\nimport {resolveVariantFromCookies} from '#live/resolveVariantFromCookies'\nimport {validateStrictFetchOptions, validateStrictSanityLiveProps} from '#live/strictValidation'\nimport type {\n DefinedFetchType,\n DefinedLiveProps,\n DefineLiveOptions,\n LivePerspective,\n StrictDefinedFetchType,\n StrictDefinedLiveProps,\n} from '#live/types'\n\n/**\n * Set up Sanity Live for Cache Components. `defineLive` returns `sanityFetch`\n * and `<SanityLive />`, which connect your Sanity client to the Live Content API\n * so cached pages can update in response to fine-grained content changes.\n *\n * With `strict: true`, `perspective` and `stega` become required\n * `sanityFetch` options, and `includeDrafts` becomes required on\n * `<SanityLive />`. Resolve dynamic values from `draftMode()` and `cookies()`\n * outside `'use cache'` boundaries, then pass them into cached components.\n *\n * `sanityFetch` brands `data` with stega string types when `stega` is `true`,\n * a non-literal `boolean`, or omitted (react-server may auto-enable stega).\n * Pass the literal `stega: false` for clean TypeGen types. Use `stegaClean`\n * before comparing branded strings to literals.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * // sanity/live.ts\n * import {cookies, draftMode} from 'next/headers'\n * import {createClient} from 'next-sanity'\n * import {\n * defineLive,\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * } from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * strict: true,\n * })\n *\n * export interface DynamicFetchOptions {\n * perspective: LivePerspective\n * variant?: string\n * // `boolean` brands `sanityFetch` `data`; use literal `false` for clean types\n * stega: boolean\n * }\n *\n * // Resolve dynamic values outside 'use cache' boundaries.\n * export async function getDynamicFetchOptions(): Promise<DynamicFetchOptions> {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (!isDraftMode) {\n * return {perspective: 'published', stega: false}\n * }\n *\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n * return {perspective: perspective ?? 'drafts', variant, stega: true}\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {draftMode} from 'next/headers'\n *\n * import {SanityLive} from '@/sanity/live'\n *\n * export default async function RootLayout({children}: {children: React.ReactNode}) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive includeDrafts={isDraftMode} />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {draftMode} from 'next/headers'\n * import {Suspense} from 'react'\n * import {defineQuery} from 'next-sanity'\n *\n * import {\n * getDynamicFetchOptions,\n * sanityFetch,\n * type DynamicFetchOptions,\n * } from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await sanityFetch({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (isDraftMode) {\n * return (\n * <Suspense fallback={<div>Loading...</div>}>\n * <DynamicPage params={props.params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await props.params\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage(props: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await props.params\n * const {perspective, variant, stega} = await getDynamicFetchOptions()\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega={stega} />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: {slug: string} & DynamicFetchOptions) {\n * 'use cache'\n *\n * const {data} = await sanityFetch({\n * query: POST_QUERY,\n * params: {slug},\n * perspective,\n * variant,\n * stega,\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict: true}): {\n sanityFetch: StrictDefinedFetchType\n SanityLive: React.ComponentType<StrictDefinedLiveProps>\n}\n/**\n * Set up Sanity Live. `defineLive` returns `sanityFetch` and `<SanityLive />`,\n * which connect your Sanity client to the Live Content API so pages can serve\n * cached content and update in response to fine-grained content changes.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * import {createClient} from 'next-sanity'\n * import {defineLive} from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * })\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {SanityLive} from '@/sanity/live'\n *\n * export default function RootLayout({children}: {children: React.ReactNode}) {\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {defineQuery} from 'next-sanity'\n * import {sanityFetch} from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await sanityFetch({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {slug} = await props.params\n * const {data} = await sanityFetch({\n * query: POST_QUERY,\n * params: {slug},\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict?: false}): {\n sanityFetch: DefinedFetchType\n SanityLive: React.ComponentType<DefinedLiveProps>\n}\nexport function defineLive(config: DefineLiveOptions) {\n const {client: _client, serverToken, browserToken, strict = false} = config\n\n if (!_client) {\n throw new Error('`client` is required for `defineLive` to function')\n }\n\n if (process.env.NODE_ENV === 'development' && !serverToken && serverToken !== false) {\n console.warn(\n 'No `serverToken` provided to `defineLive`. This means that only published content will be fetched and respond to live events. You can silence this warning by setting `serverToken: false`.',\n )\n }\n\n if (process.env.NODE_ENV === 'development' && !browserToken && browserToken !== false) {\n console.warn(\n 'No `browserToken` provided to `defineLive`. This means that live previewing drafts will only work when using the Presentation Tool in your Sanity Studio. To support live previewing drafts stand-alone, provide a `browserToken`. It is shared with the browser so it should only have Viewer rights or lower. You can silence this warning by setting `browserToken: false`.',\n )\n }\n\n const client = _client.withConfig({\n allowReconfigure: false,\n useCdn: true,\n perspective: 'published',\n stega: false,\n })\n const studioUrlDefined = typeof client.config().stega.studioUrl !== 'undefined'\n\n const sanityFetch: DefinedFetchType = async function sanityFetch({\n query,\n params = {},\n perspective: _perspective,\n variant: _variant,\n stega: _stega,\n tags = [],\n requestTag = 'next-loader.fetch',\n }) {\n if (strict) {\n validateStrictFetchOptions({perspective: _perspective, stega: _stega})\n }\n const stega = strict\n ? _stega\n : (_stega ?? (serverToken && studioUrlDefined ? (await draftMode()).isEnabled : false))\n const perspective = strict\n ? _perspective\n : (_perspective ?? (serverToken ? await resolveCookiePerspective() : undefined))\n // The variant cookie is only auto-resolved when the perspective is too:\n // an explicit `perspective` opts out of cookie resolution entirely, which\n // keeps fetches with explicit options free of dynamic API calls.\n const variant = strict\n ? _variant\n : (_variant ??\n (serverToken && typeof _perspective === 'undefined'\n ? await resolveCookieVariant()\n : undefined))\n const useCdn = perspective ? perspective === 'published' : undefined\n const isBuildPhase = process.env['NEXT_PHASE'] === PHASE_PRODUCTION_BUILD\n const cacheMode = useCdn !== false && !isBuildPhase ? 'noStale' : undefined\n const token =\n ((perspective && perspective !== 'published') || stega) && serverToken\n ? serverToken\n : undefined\n\n // 1. Fetch the tags first, with an uncached request, but that does not count towards the Sanity API quota\n const {syncTags} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective,\n variant,\n stega: false,\n resultSourceMap: false,\n returnQuery: false,\n useCdn,\n cacheMode,\n tag: [requestTag, 'fetch-sync-tags'].filter(Boolean).join('.'),\n token,\n })\n\n const cacheTags = [...tags, ...(syncTags?.map((tag) => `${cacheTagPrefix}${tag}`) || [])]\n\n // 2. Then fetch the data, using the fetch cache with specified tags\n const {result, resultSourceMap} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective,\n variant,\n stega,\n next: {revalidate: false, tags: cacheTags},\n useCdn,\n cacheMode,\n tag: requestTag,\n token,\n })\n return {data: result, sourceMap: resultSourceMap || null, tags: cacheTags}\n }\n\n const SanityLive: React.ComponentType<DefinedLiveProps> = async function SanityLive(props) {\n if (strict) {\n validateStrictSanityLiveProps(props)\n }\n const {\n includeDrafts: _includeDrafts,\n requestTag = 'next-loader.live',\n waitFor,\n\n action,\n onError,\n onWelcome,\n onReconnect,\n onRestart,\n onGoAway,\n } = props\n const {projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix} =\n client.config()\n\n const includeDrafts =\n typeof browserToken === 'string' &&\n !!browserToken &&\n (_includeDrafts ?? (await draftMode()).isEnabled)\n const shouldWaitFor = waitFor === 'function' && !includeDrafts ? waitFor : undefined\n\n // Preconnect to the Live Event API origin early, as the Sanity API is almost always on a different origin than the app\n preconnect(client)\n\n return (\n <SanityLiveClientComponent\n config={{\n projectId,\n dataset,\n apiHost: apiHost === defaultApiHost ? undefined : apiHost,\n apiVersion,\n useProjectHostname: useProjectHostname ? undefined : useProjectHostname,\n requestTagPrefix,\n token: includeDrafts ? browserToken : undefined,\n }}\n includeDrafts={includeDrafts ? true : undefined}\n requestTag={requestTag}\n waitFor={shouldWaitFor}\n action={\n action ??\n (shouldWaitFor === 'function' || includeDrafts ? 'refresh' : revalidateSyncTagsAction)\n }\n onError={onError}\n onWelcome={onWelcome}\n onReconnect={onReconnect}\n onRestart={onRestart}\n onGoAway={onGoAway}\n />\n )\n }\n SanityLive.displayName = 'SanityLiveServerComponent'\n\n return {sanityFetch, SanityLive}\n}\n\nasync function resolveCookiePerspective(): Promise<LivePerspective | undefined> {\n return (await draftMode()).isEnabled\n ? await resolvePerspectiveFromCookies({cookies: await cookies()})\n : undefined\n}\n\nasync function resolveCookieVariant(): Promise<string | undefined> {\n return (await draftMode()).isEnabled\n ? await resolveVariantFromCookies({cookies: await cookies()})\n : undefined\n}\n"],"mappings":";;;;;;;;;AA4QA,SAAgB,WAAW,QAA2B;CACpD,MAAM,EAAC,QAAQ,SAAS,aAAa,cAAc,SAAS,UAAS;CAErE,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,mDAAmD;CAGrE,IAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,eAAe,gBAAgB,OAC5E,QAAQ,KACN,6LACF;CAGF,IAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,gBAAgB,iBAAiB,OAC9E,QAAQ,KACN,gXACF;CAGF,MAAM,SAAS,QAAQ,WAAW;EAChC,kBAAkB;EAClB,QAAQ;EACR,aAAa;EACb,OAAO;CACT,CAAC;CACD,MAAM,mBAAmB,OAAO,OAAO,OAAO,CAAC,CAAC,MAAM,cAAc;CAEpE,MAAM,cAAgC,eAAe,YAAY,EAC/D,OACA,SAAS,CAAC,GACV,aAAa,cACb,SAAS,UACT,OAAO,QACP,OAAO,CAAC,GACR,aAAa,uBACZ;EACD,IAAI,QACF,2BAA2B;GAAC,aAAa;GAAc,OAAO;EAAM,CAAC;EAEvE,MAAM,QAAQ,SACV,SACC,WAAW,eAAe,oBAAoB,MAAM,UAAU,EAAA,CAAG,YAAY;EAClF,MAAM,cAAc,SAChB,eACC,iBAAiB,cAAc,MAAM,yBAAyB,IAAI,KAAA;EAIvE,MAAM,UAAU,SACZ,WACC,aACA,eAAe,OAAO,iBAAiB,cACpC,MAAM,qBAAqB,IAC3B,KAAA;EACR,MAAM,SAAS,cAAc,gBAAgB,cAAc,KAAA;EAC3D,MAAM,eAAe,QAAQ,IAAI,kBAAkB;EACnD,MAAM,YAAY,WAAW,SAAS,CAAC,eAAe,YAAY,KAAA;EAClE,MAAM,SACF,eAAe,gBAAgB,eAAgB,UAAU,cACvD,cACA,KAAA;EAGN,MAAM,EAAC,aAAY,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GACzD,gBAAgB;GAChB;GACA;GACA,OAAO;GACP,iBAAiB;GACjB,aAAa;GACb;GACA;GACA,KAAK,CAAC,YAAY,iBAAiB,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG;GAC7D;EACF,CAAC;EAED,MAAM,YAAY,CAAC,GAAG,MAAM,GAAI,UAAU,KAAK,QAAQ,UAAoB,KAAK,KAAK,CAAC,CAAE;EAGxF,MAAM,EAAC,QAAQ,oBAAmB,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GACxE,gBAAgB;GAChB;GACA;GACA;GACA,MAAM;IAAC,YAAY;IAAO,MAAM;GAAS;GACzC;GACA;GACA,KAAK;GACL;EACF,CAAC;EACD,OAAO;GAAC,MAAM;GAAQ,WAAW,mBAAmB;GAAM,MAAM;EAAS;CAC3E;CAEA,MAAMA,eAAoD,eAAeA,aAAW,OAAO;EACzF,IAAI,QACF,8BAA8B,KAAK;EAErC,MAAM,EACJ,eAAe,gBACf,aAAa,oBACb,SAEA,QACA,SACA,WACA,aACA,WACA,aACE;EACJ,MAAM,EAAC,WAAW,SAAS,SAAS,YAAY,oBAAoB,qBAClE,OAAO,OAAO;EAEhB,MAAM,gBACJ,OAAO,iBAAiB,YACxB,CAAC,CAAC,iBACD,mBAAmB,MAAM,UAAU,EAAA,CAAG;EACzC,MAAM,gBAAgB,YAAY,cAAc,CAAC,gBAAgB,UAAU,KAAA;EAG3E,WAAW,MAAM;EAEjB,OACE,oBAACC,YAAD;GACE,QAAQ;IACN;IACA;IACA,SAAS,YAAA,0BAA6B,KAAA,IAAY;IAClD;IACA,oBAAoB,qBAAqB,KAAA,IAAY;IACrD;IACA,OAAO,gBAAgB,eAAe,KAAA;GACxC;GACA,eAAe,gBAAgB,OAAO,KAAA;GAC1B;GACZ,SAAS;GACT,QACE,WACC,kBAAkB,cAAc,gBAAgB,YAAY;GAEtD;GACE;GACE;GACF;GACD;EACX,CAAA;CAEL;CACA,aAAW,cAAc;CAEzB,OAAO;EAAC;EAAa,YAAA;CAAU;AACjC;AAEA,eAAe,2BAAiE;CAC9E,QAAQ,MAAM,UAAU,EAAA,CAAG,YACvB,MAAM,8BAA8B,EAAC,SAAS,MAAM,QAAQ,EAAC,CAAC,IAC9D,KAAA;AACN;AAEA,eAAe,uBAAoD;CACjE,QAAQ,MAAM,UAAU,EAAA,CAAG,YACvB,MAAM,0BAA0B,EAAC,SAAS,MAAM,QAAQ,EAAC,CAAC,IAC1D,KAAA;AACN"}
1
+ {"version":3,"file":"index.js","names":["SanityLive","SanityLiveClientComponent"],"sources":["../../../../src/live/conditions/react-server/defineLive.tsx"],"sourcesContent":["import {SanityLive as SanityLiveClientComponent} from 'next-sanity/live/client-components'\nimport {revalidateSyncTagsAction} from 'next-sanity/live/server-actions'\nimport {PHASE_PRODUCTION_BUILD} from 'next/constants'\nimport {cookies, draftMode} from 'next/headers'\n\nimport {cacheTagPrefix, defaultApiHost} from '#live/constants'\nimport {preconnect} from '#live/preconnect'\nimport {resolvePerspectiveFromCookies} from '#live/resolvePerspectiveFromCookies'\nimport {resolveVariantFromCookies} from '#live/resolveVariantFromCookies'\nimport {validateStrictFetchOptions, validateStrictSanityLiveProps} from '#live/strictValidation'\nimport type {\n DefinedFetchType,\n DefinedLiveProps,\n DefineLiveOptions,\n LivePerspective,\n StrictDefinedFetchType,\n StrictDefinedLiveProps,\n} from '#live/types'\n\n/**\n * Set up Sanity Live for Cache Components. `defineLive` returns `sanityFetch`\n * and `<SanityLive />`, which connect your Sanity client to the Live Content API\n * so cached pages can update in response to fine-grained content changes.\n *\n * With `strict: true`, `perspective` and `stega` become required\n * `sanityFetch` options, and `includeDrafts` becomes required on\n * `<SanityLive />`. Resolve dynamic values from `draftMode()` and `cookies()`\n * outside `'use cache'` boundaries, then pass them into cached components.\n *\n * `sanityFetch` brands `data` with stega string types when `stega` is `true`,\n * a non-literal `boolean`, or omitted (react-server may auto-enable stega).\n * Pass the literal `stega: false` for clean TypeGen types. Use `stegaClean`\n * before comparing branded strings to literals.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * // sanity/live.ts\n * import {cookies, draftMode} from 'next/headers'\n * import {createClient} from 'next-sanity'\n * import {\n * defineLive,\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * type StrictDefinedFetchType,\n * } from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * strict: true,\n * })\n *\n * // The app's one shared 'use cache' boundary. `sanityFetch` calls\n * // `cacheTag`/`cacheLife` internally but doesn't create the boundary —\n * // this wrapper provides it once, so callers don't add their own.\n * export const cachedSanity: StrictDefinedFetchType = async (options) => {\n * 'use cache'\n * return sanityFetch(options)\n * }\n *\n * export interface DynamicFetchOptions {\n * perspective: LivePerspective\n * variant?: string\n * // `boolean` brands `sanityFetch` `data`; use literal `false` for clean types\n * stega: boolean\n * }\n *\n * // Resolve dynamic values outside 'use cache' boundaries.\n * export async function getDynamicFetchOptions(): Promise<DynamicFetchOptions> {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (!isDraftMode) {\n * return {perspective: 'published', stega: false}\n * }\n *\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n * return {perspective: perspective ?? 'drafts', variant, stega: true}\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {draftMode} from 'next/headers'\n *\n * import {SanityLive} from '@/sanity/live'\n *\n * export default async function RootLayout({children}: {children: React.ReactNode}) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive includeDrafts={isDraftMode} />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {draftMode} from 'next/headers'\n * import {Suspense} from 'react'\n * import {defineQuery} from 'next-sanity'\n *\n * import {\n * cachedSanity,\n * getDynamicFetchOptions,\n * type DynamicFetchOptions,\n * } from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await cachedSanity({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n * if (isDraftMode) {\n * return (\n * <Suspense fallback={<div>Loading...</div>}>\n * <DynamicPage params={props.params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await props.params\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage(props: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await props.params\n * const {perspective, variant, stega} = await getDynamicFetchOptions()\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega={stega} />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: {slug: string} & DynamicFetchOptions) {\n * const {data} = await cachedSanity({\n * query: POST_QUERY,\n * params: {slug},\n * perspective,\n * variant,\n * stega,\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict: true}): {\n sanityFetch: StrictDefinedFetchType\n SanityLive: React.ComponentType<StrictDefinedLiveProps>\n}\n/**\n * Set up Sanity Live. `defineLive` returns `sanityFetch` and `<SanityLive />`,\n * which connect your Sanity client to the Live Content API so pages can serve\n * cached content and update in response to fine-grained content changes.\n *\n * @see [Live Content API](https://www.sanity.io/docs/content-lake/live-content-api)\n * @see [Sanity Live](https://www.sanity.io/live)\n *\n * @example\n * ```tsx\n * import {createClient} from 'next-sanity'\n * import {defineLive} from 'next-sanity/live'\n *\n * const client = createClient({\n * projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,\n * dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,\n * useCdn: true,\n * perspective: 'published',\n * })\n * const token = process.env.SANITY_API_READ_TOKEN\n *\n * export const {sanityFetch, SanityLive} = defineLive({\n * client,\n * browserToken: token,\n * serverToken: token,\n * })\n * ```\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import {SanityLive} from '@/sanity/live'\n *\n * export default function RootLayout({children}: {children: React.ReactNode}) {\n * return (\n * <html lang=\"en\">\n * <body>\n * {children}\n * <SanityLive />\n * </body>\n * </html>\n * )\n * }\n * ```\n *\n * @example\n * ```tsx\n * // app/[slug]/page.tsx\n * import {defineQuery} from 'next-sanity'\n * import {sanityFetch} from '@/sanity/live'\n *\n * const POSTS_SLUGS_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current]{\"slug\": slug.current}\n * `)\n * const POST_QUERY = defineQuery(`\n * *[_type == \"post\" && slug.current == $slug][0]\n * `)\n *\n * export async function generateStaticParams() {\n * const {data} = await sanityFetch({\n * query: POSTS_SLUGS_QUERY,\n * perspective: 'published',\n * stega: false,\n * })\n *\n * return data\n * }\n *\n * export default async function Page(props: PageProps<'/[slug]'>) {\n * const {slug} = await props.params\n * const {data} = await sanityFetch({\n * query: POST_QUERY,\n * params: {slug},\n * })\n *\n * return <pre>{JSON.stringify(data, null, 2)}</pre>\n * }\n * ```\n *\n * @public\n */\nexport function defineLive(config: DefineLiveOptions & {strict?: false}): {\n sanityFetch: DefinedFetchType\n SanityLive: React.ComponentType<DefinedLiveProps>\n}\nexport function defineLive(config: DefineLiveOptions) {\n const {client: _client, serverToken, browserToken, strict = false} = config\n\n if (!_client) {\n throw new Error('`client` is required for `defineLive` to function')\n }\n\n if (process.env.NODE_ENV === 'development' && !serverToken && serverToken !== false) {\n console.warn(\n 'No `serverToken` provided to `defineLive`. This means that only published content will be fetched and respond to live events. You can silence this warning by setting `serverToken: false`.',\n )\n }\n\n if (process.env.NODE_ENV === 'development' && !browserToken && browserToken !== false) {\n console.warn(\n 'No `browserToken` provided to `defineLive`. This means that live previewing drafts will only work when using the Presentation Tool in your Sanity Studio. To support live previewing drafts stand-alone, provide a `browserToken`. It is shared with the browser so it should only have Viewer rights or lower. You can silence this warning by setting `browserToken: false`.',\n )\n }\n\n const client = _client.withConfig({\n allowReconfigure: false,\n useCdn: true,\n perspective: 'published',\n stega: false,\n })\n const studioUrlDefined = typeof client.config().stega.studioUrl !== 'undefined'\n\n const sanityFetch: DefinedFetchType = async function sanityFetch({\n query,\n params = {},\n perspective: _perspective,\n variant: _variant,\n stega: _stega,\n tags = [],\n requestTag = 'next-loader.fetch',\n }) {\n if (strict) {\n validateStrictFetchOptions({perspective: _perspective, stega: _stega})\n }\n const stega = strict\n ? _stega\n : (_stega ?? (serverToken && studioUrlDefined ? (await draftMode()).isEnabled : false))\n const perspective = strict\n ? _perspective\n : (_perspective ?? (serverToken ? await resolveCookiePerspective() : undefined))\n // The variant cookie is only auto-resolved when the perspective is too:\n // an explicit `perspective` opts out of cookie resolution entirely, which\n // keeps fetches with explicit options free of dynamic API calls.\n const variant = strict\n ? _variant\n : (_variant ??\n (serverToken && typeof _perspective === 'undefined'\n ? await resolveCookieVariant()\n : undefined))\n const useCdn = perspective ? perspective === 'published' : undefined\n const isBuildPhase = process.env['NEXT_PHASE'] === PHASE_PRODUCTION_BUILD\n const cacheMode = useCdn !== false && !isBuildPhase ? 'noStale' : undefined\n const token =\n ((perspective && perspective !== 'published') || stega) && serverToken\n ? serverToken\n : undefined\n\n // 1. Fetch the tags first, with an uncached request, but that does not count towards the Sanity API quota\n const {syncTags} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective,\n variant,\n stega: false,\n resultSourceMap: false,\n returnQuery: false,\n useCdn,\n cacheMode,\n tag: [requestTag, 'fetch-sync-tags'].filter(Boolean).join('.'),\n token,\n })\n\n const cacheTags = [...tags, ...(syncTags?.map((tag) => `${cacheTagPrefix}${tag}`) || [])]\n\n // 2. Then fetch the data, using the fetch cache with specified tags\n const {result, resultSourceMap} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective,\n variant,\n stega,\n next: {revalidate: false, tags: cacheTags},\n useCdn,\n cacheMode,\n tag: requestTag,\n token,\n })\n return {data: result, sourceMap: resultSourceMap || null, tags: cacheTags}\n }\n\n const SanityLive: React.ComponentType<DefinedLiveProps> = async function SanityLive(props) {\n if (strict) {\n validateStrictSanityLiveProps(props)\n }\n const {\n includeDrafts: _includeDrafts,\n requestTag = 'next-loader.live',\n waitFor,\n\n action,\n onError,\n onWelcome,\n onReconnect,\n onRestart,\n onGoAway,\n } = props\n const {projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix} =\n client.config()\n\n const includeDrafts =\n typeof browserToken === 'string' &&\n !!browserToken &&\n (_includeDrafts ?? (await draftMode()).isEnabled)\n const shouldWaitFor = waitFor === 'function' && !includeDrafts ? waitFor : undefined\n\n // Preconnect to the Live Event API origin early, as the Sanity API is almost always on a different origin than the app\n preconnect(client)\n\n return (\n <SanityLiveClientComponent\n config={{\n projectId,\n dataset,\n apiHost: apiHost === defaultApiHost ? undefined : apiHost,\n apiVersion,\n useProjectHostname: useProjectHostname ? undefined : useProjectHostname,\n requestTagPrefix,\n token: includeDrafts ? browserToken : undefined,\n }}\n includeDrafts={includeDrafts ? true : undefined}\n requestTag={requestTag}\n waitFor={shouldWaitFor}\n action={\n action ??\n (shouldWaitFor === 'function' || includeDrafts ? 'refresh' : revalidateSyncTagsAction)\n }\n onError={onError}\n onWelcome={onWelcome}\n onReconnect={onReconnect}\n onRestart={onRestart}\n onGoAway={onGoAway}\n />\n )\n }\n SanityLive.displayName = 'SanityLiveServerComponent'\n\n return {sanityFetch, SanityLive}\n}\n\nasync function resolveCookiePerspective(): Promise<LivePerspective | undefined> {\n return (await draftMode()).isEnabled\n ? await resolvePerspectiveFromCookies({cookies: await cookies()})\n : undefined\n}\n\nasync function resolveCookieVariant(): Promise<string | undefined> {\n return (await draftMode()).isEnabled\n ? await resolveVariantFromCookies({cookies: await cookies()})\n : undefined\n}\n"],"mappings":";;;;;;;;;AAmRA,SAAgB,WAAW,QAA2B;CACpD,MAAM,EAAC,QAAQ,SAAS,aAAa,cAAc,SAAS,UAAS;CAErE,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,mDAAmD;CAGrE,IAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,eAAe,gBAAgB,OAC5E,QAAQ,KACN,6LACF;CAGF,IAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,gBAAgB,iBAAiB,OAC9E,QAAQ,KACN,gXACF;CAGF,MAAM,SAAS,QAAQ,WAAW;EAChC,kBAAkB;EAClB,QAAQ;EACR,aAAa;EACb,OAAO;CACT,CAAC;CACD,MAAM,mBAAmB,OAAO,OAAO,OAAO,CAAC,CAAC,MAAM,cAAc;CAEpE,MAAM,cAAgC,eAAe,YAAY,EAC/D,OACA,SAAS,CAAC,GACV,aAAa,cACb,SAAS,UACT,OAAO,QACP,OAAO,CAAC,GACR,aAAa,uBACZ;EACD,IAAI,QACF,2BAA2B;GAAC,aAAa;GAAc,OAAO;EAAM,CAAC;EAEvE,MAAM,QAAQ,SACV,SACC,WAAW,eAAe,oBAAoB,MAAM,UAAU,EAAA,CAAG,YAAY;EAClF,MAAM,cAAc,SAChB,eACC,iBAAiB,cAAc,MAAM,yBAAyB,IAAI,KAAA;EAIvE,MAAM,UAAU,SACZ,WACC,aACA,eAAe,OAAO,iBAAiB,cACpC,MAAM,qBAAqB,IAC3B,KAAA;EACR,MAAM,SAAS,cAAc,gBAAgB,cAAc,KAAA;EAC3D,MAAM,eAAe,QAAQ,IAAI,kBAAkB;EACnD,MAAM,YAAY,WAAW,SAAS,CAAC,eAAe,YAAY,KAAA;EAClE,MAAM,SACF,eAAe,gBAAgB,eAAgB,UAAU,cACvD,cACA,KAAA;EAGN,MAAM,EAAC,aAAY,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GACzD,gBAAgB;GAChB;GACA;GACA,OAAO;GACP,iBAAiB;GACjB,aAAa;GACb;GACA;GACA,KAAK,CAAC,YAAY,iBAAiB,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG;GAC7D;EACF,CAAC;EAED,MAAM,YAAY,CAAC,GAAG,MAAM,GAAI,UAAU,KAAK,QAAQ,UAAoB,KAAK,KAAK,CAAC,CAAE;EAGxF,MAAM,EAAC,QAAQ,oBAAmB,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GACxE,gBAAgB;GAChB;GACA;GACA;GACA,MAAM;IAAC,YAAY;IAAO,MAAM;GAAS;GACzC;GACA;GACA,KAAK;GACL;EACF,CAAC;EACD,OAAO;GAAC,MAAM;GAAQ,WAAW,mBAAmB;GAAM,MAAM;EAAS;CAC3E;CAEA,MAAMA,eAAoD,eAAeA,aAAW,OAAO;EACzF,IAAI,QACF,8BAA8B,KAAK;EAErC,MAAM,EACJ,eAAe,gBACf,aAAa,oBACb,SAEA,QACA,SACA,WACA,aACA,WACA,aACE;EACJ,MAAM,EAAC,WAAW,SAAS,SAAS,YAAY,oBAAoB,qBAClE,OAAO,OAAO;EAEhB,MAAM,gBACJ,OAAO,iBAAiB,YACxB,CAAC,CAAC,iBACD,mBAAmB,MAAM,UAAU,EAAA,CAAG;EACzC,MAAM,gBAAgB,YAAY,cAAc,CAAC,gBAAgB,UAAU,KAAA;EAG3E,WAAW,MAAM;EAEjB,OACE,oBAACC,YAAD;GACE,QAAQ;IACN;IACA;IACA,SAAS,YAAA,0BAA6B,KAAA,IAAY;IAClD;IACA,oBAAoB,qBAAqB,KAAA,IAAY;IACrD;IACA,OAAO,gBAAgB,eAAe,KAAA;GACxC;GACA,eAAe,gBAAgB,OAAO,KAAA;GAC1B;GACZ,SAAS;GACT,QACE,WACC,kBAAkB,cAAc,gBAAgB,YAAY;GAEtD;GACE;GACE;GACF;GACD;EACX,CAAA;CAEL;CACA,aAAW,cAAc;CAEzB,OAAO;EAAC;EAAa,YAAA;CAAU;AACjC;AAEA,eAAe,2BAAiE;CAC9E,QAAQ,MAAM,UAAU,EAAA,CAAG,YACvB,MAAM,8BAA8B,EAAC,SAAS,MAAM,QAAQ,EAAC,CAAC,IAC9D,KAAA;AACN;AAEA,eAAe,uBAAoD;CACjE,QAAQ,MAAM,UAAU,EAAA,CAAG,YACvB,MAAM,0BAA0B,EAAC,SAAS,MAAM,QAAQ,EAAC,CAAC,IAC1D,KAAA;AACN"}
@@ -11,11 +11,11 @@ import { SyncTag } from "@sanity/client";
11
11
  * import {cookies, draftMode} from 'next/headers'
12
12
  * import {defineQuery} from 'next-sanity'
13
13
  * import {resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live'
14
- * import {sanityFetch, sanityFetchStaticParams} from '#sanity/live'
14
+ * import {cachedSanity, cachedSanityStaticParams} from '#sanity/live'
15
15
  *
16
16
  * export async function generateStaticParams() {
17
17
  * const query = defineQuery(`*[_type == "page" && defined(slug.current)]{"slug": slug.current}`)
18
- * return await sanityFetchStaticParams({query})
18
+ * return await cachedSanityStaticParams({query})
19
19
  * }
20
20
  *
21
21
  * export default async function Page({params}: PageProps<'/[slug]'>) {
@@ -49,10 +49,8 @@ import { SyncTag } from "@sanity/client";
49
49
  * perspective: LivePerspective
50
50
  * stega: boolean
51
51
  * }) {
52
- * 'use cache'
53
- *
54
52
  * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`)
55
- * const {data} = await sanityFetch({query, params: {slug}, perspective, stega})
53
+ * const {data} = await cachedSanity({query, params: {slug}, perspective, stega})
56
54
  *
57
55
  * return <article>...</article>
58
56
  * }
@@ -81,7 +79,7 @@ declare function resolvePerspectiveFromCookies({ cookies: jar }: {
81
79
  * resolveVariantFromCookies,
82
80
  * type LivePerspective,
83
81
  * } from 'next-sanity/live'
84
- * import {sanityFetch} from '#sanity/live'
82
+ * import {cachedSanity} from '#sanity/live'
85
83
  *
86
84
  * export default async function Page({params}: PageProps<'/[slug]'>) {
87
85
  * const {isEnabled: isDraftMode} = await draftMode()
@@ -118,10 +116,8 @@ declare function resolvePerspectiveFromCookies({ cookies: jar }: {
118
116
  * variant: string | undefined
119
117
  * stega: boolean
120
118
  * }) {
121
- * 'use cache'
122
- *
123
119
  * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`)
124
- * const {data} = await sanityFetch({query, params: {slug}, perspective, variant, stega})
120
+ * const {data} = await cachedSanity({query, params: {slug}, perspective, variant, stega})
125
121
  *
126
122
  * return <article>...</article>
127
123
  * }
@@ -1 +1 @@
1
- {"version":3,"file":"parseTags.d.ts","names":[],"sources":["../src/live/shared/resolvePerspectiveFromCookies.ts","../src/live/shared/resolveVariantFromCookies.ts","../src/live/shared/constants.ts","../src/live/shared/parseTags.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAiEsB,gCACpB,SAAS;EAET,SAAS,QAAQ,kBAAkB;IACjC,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCEU,4BACpB,SAAS;EAET,SAAS,QAAQ,kBAAkB;IACjC;cCxES;UCCH;EACR,gBAAgB,iBAAiB;EACjC,mBAAmB;EACnB,eAAe;;;;;;;;;;;;;;;;;;;;;;;iBAwBD,UAAU,sBAAsB"}
1
+ {"version":3,"file":"parseTags.d.ts","names":[],"sources":["../src/live/shared/resolvePerspectiveFromCookies.ts","../src/live/shared/resolveVariantFromCookies.ts","../src/live/shared/constants.ts","../src/live/shared/parseTags.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA+DsB,gCACpB,SAAS;EAET,SAAS,QAAQ,kBAAkB;IACjC,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCEU,4BACpB,SAAS;EAET,SAAS,QAAQ,kBAAkB;IACjC;cCtES;UCCH;EACR,gBAAgB,iBAAiB;EACjC,mBAAmB;EACnB,eAAe;;;;;;;;;;;;;;;;;;;;;;;iBAwBD,UAAU,sBAAsB"}
@@ -26,11 +26,11 @@ function validateStrictFetchOptions(options) {
26
26
  * import {cookies, draftMode} from 'next/headers'
27
27
  * import {defineQuery} from 'next-sanity'
28
28
  * import {resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live'
29
- * import {sanityFetch, sanityFetchStaticParams} from '#sanity/live'
29
+ * import {cachedSanity, cachedSanityStaticParams} from '#sanity/live'
30
30
  *
31
31
  * export async function generateStaticParams() {
32
32
  * const query = defineQuery(`*[_type == "page" && defined(slug.current)]{"slug": slug.current}`)
33
- * return await sanityFetchStaticParams({query})
33
+ * return await cachedSanityStaticParams({query})
34
34
  * }
35
35
  *
36
36
  * export default async function Page({params}: PageProps<'/[slug]'>) {
@@ -64,10 +64,8 @@ function validateStrictFetchOptions(options) {
64
64
  * perspective: LivePerspective
65
65
  * stega: boolean
66
66
  * }) {
67
- * 'use cache'
68
- *
69
67
  * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`)
70
- * const {data} = await sanityFetch({query, params: {slug}, perspective, stega})
68
+ * const {data} = await cachedSanity({query, params: {slug}, perspective, stega})
71
69
  *
72
70
  * return <article>...</article>
73
71
  * }
@@ -96,7 +94,7 @@ async function resolvePerspectiveFromCookies({ cookies: jar }) {
96
94
  * resolveVariantFromCookies,
97
95
  * type LivePerspective,
98
96
  * } from 'next-sanity/live'
99
- * import {sanityFetch} from '#sanity/live'
97
+ * import {cachedSanity} from '#sanity/live'
100
98
  *
101
99
  * export default async function Page({params}: PageProps<'/[slug]'>) {
102
100
  * const {isEnabled: isDraftMode} = await draftMode()
@@ -133,10 +131,8 @@ async function resolvePerspectiveFromCookies({ cookies: jar }) {
133
131
  * variant: string | undefined
134
132
  * stega: boolean
135
133
  * }) {
136
- * 'use cache'
137
- *
138
134
  * const query = defineQuery(`*[_type == "page" && slug.current == $slug][0]`)
139
- * const {data} = await sanityFetch({query, params: {slug}, perspective, variant, stega})
135
+ * const {data} = await cachedSanity({query, params: {slug}, perspective, variant, stega})
140
136
  *
141
137
  * return <article>...</article>
142
138
  * }
@@ -1 +1 @@
1
- {"version":3,"file":"resolveVariantFromCookies.js","names":["preconnect"],"sources":["../src/live/shared/preconnect.ts","../src/live/shared/strictValidation.ts","../src/live/shared/resolvePerspectiveFromCookies.ts","../src/live/shared/resolveVariantFromCookies.ts"],"sourcesContent":["import type {SanityClient} from 'next-sanity'\nimport {preconnect as reactDomPreconnect} from 'react-dom'\n\n/**\n * 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.\n */\nexport function preconnect(client: SanityClient): void {\n const {origin} = new URL(client.getUrl('', false))\n reactDomPreconnect(origin)\n}\n","import {generateHelpUrl} from '@sanity/generate-help-url'\n\nexport function validateStrictSanityLiveProps(props: {includeDrafts?: unknown}): void {\n if (typeof props.includeDrafts !== 'boolean') {\n throw new Error(\n `<SanityLive> requires an explicit \\`includeDrafts\\` prop (true or false) when \\`strict: true\\` is set on \\`defineLive\\`.\\n\\nMore information: ${generateHelpUrl('next-sanity-live-strict')}`,\n {cause: props},\n )\n }\n}\n\nexport function validateStrictFetchOptions(options: {\n perspective?: unknown\n stega?: unknown\n}): void {\n if (typeof options.perspective === 'undefined' || options.perspective === null) {\n throw new Error(\n `sanityFetch() requires an explicit \\`perspective\\` option when \\`strict: true\\` is set on \\`defineLive\\`.\\n\\nMore information: ${generateHelpUrl('next-sanity-fetch-strict')}`,\n {cause: options},\n )\n }\n if (typeof options.stega !== 'boolean') {\n throw new Error(\n `sanityFetch() requires an explicit \\`stega\\` option (true or false) when \\`strict: true\\` is set on \\`defineLive\\`.\\n\\nMore information: ${generateHelpUrl('next-sanity-fetch-strict')}`,\n {cause: options},\n )\n }\n}\n","import {perspectiveCookieName} from '@sanity/preview-url-secret/constants'\nimport type {cookies} from 'next/headers'\n\nimport {sanitizePerspective} from '#live/sanitizePerspective'\nimport type {LivePerspective} from '#live/types'\n\n/**\n * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`),\n * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries.\n * Resolve the perspective once outside the cache boundary and pass it in as a prop / cache key.\n *\n * @example\n * ```tsx\n * import {cookies, draftMode} from 'next/headers'\n * import {defineQuery} from 'next-sanity'\n * import {resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live'\n * import {sanityFetch, sanityFetchStaticParams} from '#sanity/live'\n *\n * export async function generateStaticParams() {\n * const query = defineQuery(`*[_type == \"page\" && defined(slug.current)]{\"slug\": slug.current}`)\n * return await sanityFetchStaticParams({query})\n * }\n *\n * export default async function Page({params}: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * if (isDraftMode) {\n * return (\n * <Suspense>\n * <DynamicPage params={params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await params\n *\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await params\n * const perspective = await resolvePerspectiveFromCookies({cookies: await cookies()})\n *\n * return <CachedPage slug={slug} perspective={perspective} stega />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * stega,\n * }: Awaited<PageProps<'/[slug]'>['params']> & {\n * perspective: LivePerspective\n * stega: boolean\n * }) {\n * 'use cache'\n *\n * const query = defineQuery(`*[_type == \"page\" && slug.current == $slug][0]`)\n * const {data} = await sanityFetch({query, params: {slug}, perspective, stega})\n *\n * return <article>...</article>\n * }\n * ```\n *\n * @public\n */\nexport async function resolvePerspectiveFromCookies({\n cookies: jar,\n}: {\n cookies: Awaited<ReturnType<typeof cookies>>\n}): Promise<LivePerspective> {\n return jar.has(perspectiveCookieName)\n ? sanitizePerspective(jar.get(perspectiveCookieName)?.value, 'drafts')\n : 'drafts'\n}\n","import {variantCookieName} from '@sanity/preview-url-secret/constants'\nimport type {cookies} from 'next/headers'\n\nimport {sanitizeVariant} from '#live/sanitizeVariant'\n\n/**\n * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`),\n * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries.\n * Resolve the variant once outside the cache boundary and pass it in as a prop / cache key.\n *\n * Unlike `resolvePerspectiveFromCookies` there is no fallback value: when no\n * variant cookie is set (or its value is invalid) it resolves to `undefined`,\n * meaning \"no variant selected\" and queries return base content.\n *\n * @example\n * ```tsx\n * import {cookies, draftMode} from 'next/headers'\n * import {defineQuery} from 'next-sanity'\n * import {\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * } from 'next-sanity/live'\n * import {sanityFetch} from '#sanity/live'\n *\n * export default async function Page({params}: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * if (isDraftMode) {\n * return (\n * <Suspense>\n * <DynamicPage params={params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await params\n *\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await params\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: Awaited<PageProps<'/[slug]'>['params']> & {\n * perspective: LivePerspective\n * variant: string | undefined\n * stega: boolean\n * }) {\n * 'use cache'\n *\n * const query = defineQuery(`*[_type == \"page\" && slug.current == $slug][0]`)\n * const {data} = await sanityFetch({query, params: {slug}, perspective, variant, stega})\n *\n * return <article>...</article>\n * }\n * ```\n *\n * @public\n */\nexport async function resolveVariantFromCookies({\n cookies: jar,\n}: {\n cookies: Awaited<ReturnType<typeof cookies>>\n}): Promise<string | undefined> {\n return jar.has(variantCookieName) ? sanitizeVariant(jar.get(variantCookieName)?.value) : undefined\n}\n"],"mappings":";;;;;;;AAMA,SAAgBA,aAAW,QAA4B;CACrD,MAAM,EAAC,WAAU,IAAI,IAAI,OAAO,OAAO,IAAI,KAAK,CAAC;CACjD,WAAmB,MAAM;AAC3B;ACPA,SAAgB,8BAA8B,OAAwC;CACpF,IAAI,OAAO,MAAM,kBAAkB,WACjC,MAAM,IAAI,MACR,iJAAiJ,gBAAgB,yBAAyB,KAC1L,EAAC,OAAO,MAAK,CACf;AAEJ;AAEA,SAAgB,2BAA2B,SAGlC;CACP,IAAI,OAAO,QAAQ,gBAAgB,eAAe,QAAQ,gBAAgB,MACxE,MAAM,IAAI,MACR,kIAAkI,gBAAgB,0BAA0B,KAC5K,EAAC,OAAO,QAAO,CACjB;CAEF,IAAI,OAAO,QAAQ,UAAU,WAC3B,MAAM,IAAI,MACR,4IAA4I,gBAAgB,0BAA0B,KACtL,EAAC,OAAO,QAAO,CACjB;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACsCA,eAAsB,8BAA8B,EAClD,SAAS,OAGkB;CAC3B,OAAO,IAAI,IAAI,qBAAqB,IAChC,oBAAoB,IAAI,IAAI,qBAAqB,CAAC,EAAE,OAAO,QAAQ,IACnE;AACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACFA,eAAsB,0BAA0B,EAC9C,SAAS,OAGqB;CAC9B,OAAO,IAAI,IAAI,iBAAiB,IAAI,gBAAgB,IAAI,IAAI,iBAAiB,CAAC,EAAE,KAAK,IAAI,KAAA;AAC3F"}
1
+ {"version":3,"file":"resolveVariantFromCookies.js","names":["preconnect"],"sources":["../src/live/shared/preconnect.ts","../src/live/shared/strictValidation.ts","../src/live/shared/resolvePerspectiveFromCookies.ts","../src/live/shared/resolveVariantFromCookies.ts"],"sourcesContent":["import type {SanityClient} from 'next-sanity'\nimport {preconnect as reactDomPreconnect} from 'react-dom'\n\n/**\n * 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.\n */\nexport function preconnect(client: SanityClient): void {\n const {origin} = new URL(client.getUrl('', false))\n reactDomPreconnect(origin)\n}\n","import {generateHelpUrl} from '@sanity/generate-help-url'\n\nexport function validateStrictSanityLiveProps(props: {includeDrafts?: unknown}): void {\n if (typeof props.includeDrafts !== 'boolean') {\n throw new Error(\n `<SanityLive> requires an explicit \\`includeDrafts\\` prop (true or false) when \\`strict: true\\` is set on \\`defineLive\\`.\\n\\nMore information: ${generateHelpUrl('next-sanity-live-strict')}`,\n {cause: props},\n )\n }\n}\n\nexport function validateStrictFetchOptions(options: {\n perspective?: unknown\n stega?: unknown\n}): void {\n if (typeof options.perspective === 'undefined' || options.perspective === null) {\n throw new Error(\n `sanityFetch() requires an explicit \\`perspective\\` option when \\`strict: true\\` is set on \\`defineLive\\`.\\n\\nMore information: ${generateHelpUrl('next-sanity-fetch-strict')}`,\n {cause: options},\n )\n }\n if (typeof options.stega !== 'boolean') {\n throw new Error(\n `sanityFetch() requires an explicit \\`stega\\` option (true or false) when \\`strict: true\\` is set on \\`defineLive\\`.\\n\\nMore information: ${generateHelpUrl('next-sanity-fetch-strict')}`,\n {cause: options},\n )\n }\n}\n","import {perspectiveCookieName} from '@sanity/preview-url-secret/constants'\nimport type {cookies} from 'next/headers'\n\nimport {sanitizePerspective} from '#live/sanitizePerspective'\nimport type {LivePerspective} from '#live/types'\n\n/**\n * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`),\n * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries.\n * Resolve the perspective once outside the cache boundary and pass it in as a prop / cache key.\n *\n * @example\n * ```tsx\n * import {cookies, draftMode} from 'next/headers'\n * import {defineQuery} from 'next-sanity'\n * import {resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live'\n * import {cachedSanity, cachedSanityStaticParams} from '#sanity/live'\n *\n * export async function generateStaticParams() {\n * const query = defineQuery(`*[_type == \"page\" && defined(slug.current)]{\"slug\": slug.current}`)\n * return await cachedSanityStaticParams({query})\n * }\n *\n * export default async function Page({params}: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * if (isDraftMode) {\n * return (\n * <Suspense>\n * <DynamicPage params={params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await params\n *\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await params\n * const perspective = await resolvePerspectiveFromCookies({cookies: await cookies()})\n *\n * return <CachedPage slug={slug} perspective={perspective} stega />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * stega,\n * }: Awaited<PageProps<'/[slug]'>['params']> & {\n * perspective: LivePerspective\n * stega: boolean\n * }) {\n * const query = defineQuery(`*[_type == \"page\" && slug.current == $slug][0]`)\n * const {data} = await cachedSanity({query, params: {slug}, perspective, stega})\n *\n * return <article>...</article>\n * }\n * ```\n *\n * @public\n */\nexport async function resolvePerspectiveFromCookies({\n cookies: jar,\n}: {\n cookies: Awaited<ReturnType<typeof cookies>>\n}): Promise<LivePerspective> {\n return jar.has(perspectiveCookieName)\n ? sanitizePerspective(jar.get(perspectiveCookieName)?.value, 'drafts')\n : 'drafts'\n}\n","import {variantCookieName} from '@sanity/preview-url-secret/constants'\nimport type {cookies} from 'next/headers'\n\nimport {sanitizeVariant} from '#live/sanitizeVariant'\n\n/**\n * This helper is intended for use with Next.js Cache Components (`cacheComponents: true`),\n * where `cookies()` and `draftMode()` cannot be called inside `'use cache'` boundaries.\n * Resolve the variant once outside the cache boundary and pass it in as a prop / cache key.\n *\n * Unlike `resolvePerspectiveFromCookies` there is no fallback value: when no\n * variant cookie is set (or its value is invalid) it resolves to `undefined`,\n * meaning \"no variant selected\" and queries return base content.\n *\n * @example\n * ```tsx\n * import {cookies, draftMode} from 'next/headers'\n * import {defineQuery} from 'next-sanity'\n * import {\n * resolvePerspectiveFromCookies,\n * resolveVariantFromCookies,\n * type LivePerspective,\n * } from 'next-sanity/live'\n * import {cachedSanity} from '#sanity/live'\n *\n * export default async function Page({params}: PageProps<'/[slug]'>) {\n * const {isEnabled: isDraftMode} = await draftMode()\n *\n * if (isDraftMode) {\n * return (\n * <Suspense>\n * <DynamicPage params={params} />\n * </Suspense>\n * )\n * }\n *\n * const {slug} = await params\n *\n * return <CachedPage slug={slug} perspective=\"published\" stega={false} />\n * }\n *\n * async function DynamicPage({params}: Pick<PageProps<'/[slug]'>, 'params'>) {\n * const {slug} = await params\n * const jar = await cookies()\n * const perspective = await resolvePerspectiveFromCookies({cookies: jar})\n * const variant = await resolveVariantFromCookies({cookies: jar})\n *\n * return <CachedPage slug={slug} perspective={perspective} variant={variant} stega />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\n * variant,\n * stega,\n * }: Awaited<PageProps<'/[slug]'>['params']> & {\n * perspective: LivePerspective\n * variant: string | undefined\n * stega: boolean\n * }) {\n * const query = defineQuery(`*[_type == \"page\" && slug.current == $slug][0]`)\n * const {data} = await cachedSanity({query, params: {slug}, perspective, variant, stega})\n *\n * return <article>...</article>\n * }\n * ```\n *\n * @public\n */\nexport async function resolveVariantFromCookies({\n cookies: jar,\n}: {\n cookies: Awaited<ReturnType<typeof cookies>>\n}): Promise<string | undefined> {\n return jar.has(variantCookieName) ? sanitizeVariant(jar.get(variantCookieName)?.value) : undefined\n}\n"],"mappings":";;;;;;;AAMA,SAAgBA,aAAW,QAA4B;CACrD,MAAM,EAAC,WAAU,IAAI,IAAI,OAAO,OAAO,IAAI,KAAK,CAAC;CACjD,WAAmB,MAAM;AAC3B;ACPA,SAAgB,8BAA8B,OAAwC;CACpF,IAAI,OAAO,MAAM,kBAAkB,WACjC,MAAM,IAAI,MACR,iJAAiJ,gBAAgB,yBAAyB,KAC1L,EAAC,OAAO,MAAK,CACf;AAEJ;AAEA,SAAgB,2BAA2B,SAGlC;CACP,IAAI,OAAO,QAAQ,gBAAgB,eAAe,QAAQ,gBAAgB,MACxE,MAAM,IAAI,MACR,kIAAkI,gBAAgB,0BAA0B,KAC5K,EAAC,OAAO,QAAO,CACjB;CAEF,IAAI,OAAO,QAAQ,UAAU,WAC3B,MAAM,IAAI,MACR,4IAA4I,gBAAgB,0BAA0B,KACtL,EAAC,OAAO,QAAO,CACjB;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACoCA,eAAsB,8BAA8B,EAClD,SAAS,OAGkB;CAC3B,OAAO,IAAI,IAAI,qBAAqB,IAChC,oBAAoB,IAAI,IAAI,qBAAqB,CAAC,EAAE,OAAO,QAAQ,IACnE;AACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACFA,eAAsB,0BAA0B,EAC9C,SAAS,OAGqB;CAC9B,OAAO,IAAI,IAAI,iBAAiB,IAAI,gBAAgB,IAAI,IAAI,iBAAiB,CAAC,EAAE,KAAK,IAAI,KAAA;AAC3F"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-sanity",
3
- "version": "13.3.0",
3
+ "version": "13.3.2",
4
4
  "description": "Sanity.io toolkit for Next.js",
5
5
  "keywords": [
6
6
  "live",
@@ -59,12 +59,12 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "@portabletext/react": "^7.0.1",
62
- "@sanity/client": "^7.26.0",
62
+ "@sanity/client": "^7.26.2",
63
63
  "@sanity/generate-help-url": "^4.0.0",
64
64
  "@sanity/preview-url-secret": "^4.1.2",
65
- "@sanity/visual-editing": "^5.7.3",
65
+ "@sanity/visual-editing": "^6.0.1",
66
66
  "@sanity/webhook": "^4.0.4",
67
- "groq": "^6.8.0",
67
+ "groq": "^6.9.2",
68
68
  "history": "^5.3.0"
69
69
  },
70
70
  "devDependencies": {
@@ -82,7 +82,7 @@
82
82
  "publint": "^0.3.22",
83
83
  "react": "^19.2.8",
84
84
  "react-dom": "^19.2.8",
85
- "styled-components": "^6.4.4",
85
+ "styled-components": "^6.5.2",
86
86
  "tsdown": "^0.22.14",
87
87
  "typescript": "7.0.2",
88
88
  "vite": "^8.2.0",
@@ -91,7 +91,7 @@
91
91
  "vitest-package-exports": "^1.2.0"
92
92
  },
93
93
  "peerDependencies": {
94
- "@sanity/client": "^7.26.0",
94
+ "@sanity/client": "^7.26.2",
95
95
  "next": "^16.0.0-0",
96
96
  "react": "^19.2.3",
97
97
  "react-dom": "^19.2.3",