next-sanity 13.0.0-cache-components.62 → 13.0.0
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/README.md +1 -1
- package/dist/constants.js +1 -5
- package/dist/constants.js.map +1 -1
- package/dist/live/cache-life.d.ts +3 -12
- package/dist/live/cache-life.d.ts.map +1 -1
- package/dist/live/cache-life.js +3 -12
- package/dist/live/cache-life.js.map +1 -1
- package/dist/live/conditions/next-js/index.d.ts.map +1 -1
- package/dist/live/conditions/next-js/index.js +6 -4
- package/dist/live/conditions/next-js/index.js.map +1 -1
- package/dist/parseTags.d.ts.map +1 -1
- package/dist/resolvePerspectiveFromCookies.js +1 -1
- package/dist/resolvePerspectiveFromCookies.js.map +1 -1
- package/dist/types.d.ts +52 -13
- package/dist/types.d.ts.map +1 -1
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -102,7 +102,7 @@ MIT-licensed. See [LICENSE][LICENSE].
|
|
|
102
102
|
[migrate-v9-to-v10]: https://github.com/sanity-io/next-sanity/blob/main/packages/next-sanity/MIGRATE-v9-to-v10.md
|
|
103
103
|
[migrate-v10-to-v11]: https://github.com/sanity-io/next-sanity/blob/main/packages/next-sanity/MIGRATE-v10-to-v11.md
|
|
104
104
|
[migrate-v11-to-v12]: https://github.com/sanity-io/next-sanity/blob/main/packages/next-sanity/MIGRATE-v11-to-v12.md
|
|
105
|
-
[migrate-v12-to-v13]: https://github.com/sanity-io/next-sanity/blob/main/packages/next-sanity/
|
|
105
|
+
[migrate-v12-to-v13]: https://github.com/sanity-io/next-sanity/blob/main/packages/next-sanity/MIGRATE-v12-to-v13.md
|
|
106
106
|
[next-docs]: https://nextjs.org/docs
|
|
107
107
|
[sanity]: https://www.sanity.io?utm_source=github&utm_medium=readme&utm_campaign=next-sanity
|
|
108
108
|
[sanity-next-clean-starter]: https://www.sanity.io/templates/nextjs-sanity-clean
|
package/dist/constants.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sanity Live handles on-demand revalidation, so the default 15min time based revalidation is too short
|
|
3
|
-
*/
|
|
4
|
-
const revalidate = 31536e3;
|
|
5
1
|
const cacheTagPrefix = "sanity:";
|
|
6
|
-
export {
|
|
2
|
+
export { cacheTagPrefix as t };
|
|
7
3
|
|
|
8
4
|
//# sourceMappingURL=constants.js.map
|
package/dist/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","names":[],"sources":["../src/live/shared/constants.ts"],"sourcesContent":["import type {CacheTagPrefix} from '#live/types'\n\n
|
|
1
|
+
{"version":3,"file":"constants.js","names":[],"sources":["../src/live/shared/constants.ts"],"sourcesContent":["import type {CacheTagPrefix} from '#live/types'\n\n// @TODO make this configurable\nexport const cacheTagPrefix = 'sanity:' satisfies CacheTagPrefix\n"],"mappings":"AAGA,MAAa,iBAAiB"}
|
|
@@ -4,30 +4,21 @@
|
|
|
4
4
|
* // next.config.ts
|
|
5
5
|
*
|
|
6
6
|
* import type {NextConfig} from 'next'
|
|
7
|
-
* import {sanity} from 'next-sanity/cache-life'
|
|
7
|
+
* import {sanity} from 'next-sanity/live/cache-life'
|
|
8
8
|
*
|
|
9
9
|
* const nextConfig: NextConfig = {
|
|
10
10
|
* cacheComponents: true,
|
|
11
11
|
* cacheLife: {
|
|
12
|
-
* sanity
|
|
12
|
+
* default: sanity,
|
|
13
13
|
* }
|
|
14
14
|
* }
|
|
15
15
|
*
|
|
16
16
|
* export default nextConfig
|
|
17
17
|
* ```
|
|
18
|
-
*
|
|
19
|
-
* ```ts
|
|
20
|
-
*
|
|
21
|
-
* async function sanityFetch() {
|
|
22
|
-
* 'use cache'
|
|
23
|
-
* cacheLife('sanity')
|
|
24
|
-
* const {data} = await fetch({query, params})
|
|
25
|
-
* return data
|
|
26
|
-
* }
|
|
27
18
|
*/
|
|
28
19
|
declare const sanity: {
|
|
29
20
|
/**
|
|
30
|
-
* Sanity Live handles on-demand revalidation, so the default 15min time
|
|
21
|
+
* Sanity Live handles on-demand revalidation, so the default 15min time-based revalidation is too short
|
|
31
22
|
*/
|
|
32
23
|
readonly revalidate: 31_536_000;
|
|
33
24
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-life.d.ts","names":[],"sources":["../../src/live/cache-life.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"cache-life.d.ts","names":[],"sources":["../../src/live/cache-life.ts"],"mappings":"AAkBA;;;;;;;;;;;;;;;;;;AAAA,cAAa,MAAA;;;;WAIX,UAAA;AAAA"}
|
package/dist/live/cache-life.js
CHANGED
|
@@ -4,30 +4,21 @@
|
|
|
4
4
|
* // next.config.ts
|
|
5
5
|
*
|
|
6
6
|
* import type {NextConfig} from 'next'
|
|
7
|
-
* import {sanity} from 'next-sanity/cache-life'
|
|
7
|
+
* import {sanity} from 'next-sanity/live/cache-life'
|
|
8
8
|
*
|
|
9
9
|
* const nextConfig: NextConfig = {
|
|
10
10
|
* cacheComponents: true,
|
|
11
11
|
* cacheLife: {
|
|
12
|
-
* sanity
|
|
12
|
+
* default: sanity,
|
|
13
13
|
* }
|
|
14
14
|
* }
|
|
15
15
|
*
|
|
16
16
|
* export default nextConfig
|
|
17
17
|
* ```
|
|
18
|
-
*
|
|
19
|
-
* ```ts
|
|
20
|
-
*
|
|
21
|
-
* async function sanityFetch() {
|
|
22
|
-
* 'use cache'
|
|
23
|
-
* cacheLife('sanity')
|
|
24
|
-
* const {data} = await fetch({query, params})
|
|
25
|
-
* return data
|
|
26
|
-
* }
|
|
27
18
|
*/
|
|
28
19
|
const sanity = {
|
|
29
20
|
/**
|
|
30
|
-
* Sanity Live handles on-demand revalidation, so the default 15min time
|
|
21
|
+
* Sanity Live handles on-demand revalidation, so the default 15min time-based revalidation is too short
|
|
31
22
|
*/
|
|
32
23
|
revalidate: 31536e3 };
|
|
33
24
|
export { sanity };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-life.js","names":[],"sources":["../../src/live/cache-life.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"cache-life.js","names":[],"sources":["../../src/live/cache-life.ts"],"sourcesContent":["/**\n * For usage with `cacheComponents: true`, and `defineLive`:\n * ```ts\n * // next.config.ts\n *\n * import type {NextConfig} from 'next'\n * import {sanity} from 'next-sanity/live/cache-life'\n *\n * const nextConfig: NextConfig = {\n * cacheComponents: true,\n * cacheLife: {\n * default: sanity,\n * }\n * }\n *\n * export default nextConfig\n * ```\n */\nexport const sanity = {\n /**\n * Sanity Live handles on-demand revalidation, so the default 15min time-based revalidation is too short\n */\n revalidate: 31_536_000, // 365 days\n} as const satisfies {\n /**\n * This cache may be stale on clients for ... seconds before checking with the server.\n */\n stale?: number\n /**\n * If the server receives a new request after ... seconds, start revalidating new values in the background.\n */\n revalidate?: number\n /**\n * If this entry has no traffic for ... seconds it will expire. The next request will recompute it.\n */\n expire?: number\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkBA,MAAa,SAAS;;;;AAIpB,YAAY,SACb"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../../../src/live/conditions/next-js/defineLive.tsx"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../../../src/live/conditions/next-js/defineLive.tsx"],"mappings":";;;;;;AAsKA;;;;;;;;;;;;;;;;;;;;AAqFA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBArFgB,UAAA,CAAW,MAAA,EAAQ,iBAAA;EAAqB,MAAA;AAAA;EACtD,WAAA,EAAa,sBAAA;EACb,UAAA,EAAY,KAAA,CAAM,aAAA,CAAc,sBAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmFlB,UAAA,CAAW,MAAA,EAAQ,iBAAA;EAAqB,MAAA;AAAA;EACtD,WAAA,EAAa,gBAAA;EACb,UAAA,EAAY,KAAA,CAAM,aAAA,CAAc,gBAAA;AAAA"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { t as isCorsOriginError } from "../../../isCorsOriginError.js";
|
|
2
|
-
import {
|
|
2
|
+
import { t as cacheTagPrefix } from "../../../constants.js";
|
|
3
3
|
import { i as preconnect, n as validateStrictFetchOptions, r as validateStrictSanityLiveProps, t as resolvePerspectiveFromCookies } from "../../../resolvePerspectiveFromCookies.js";
|
|
4
4
|
import { t as parseTags } from "../../../parseTags.js";
|
|
5
5
|
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
import { sanity } from "next-sanity/live/cache-life";
|
|
6
7
|
import { SanityLive } from "next-sanity/live/client-components";
|
|
7
8
|
import { revalidateSyncTagsAction } from "next-sanity/live/server-actions";
|
|
8
9
|
import { cacheLife, cacheTag } from "next/cache";
|
|
@@ -39,14 +40,15 @@ function defineLive(config) {
|
|
|
39
40
|
});
|
|
40
41
|
const tags = [...customCacheTags, ...(syncTags || []).map((tag) => `${cacheTagPrefix}${tag}`)];
|
|
41
42
|
/**
|
|
42
|
-
* The tags used here, are expired later on in the `
|
|
43
|
+
* The tags used here, are expired later on in the `action` Server Action given to `<SanityLive />` with the `revalidateTag` function from `next/cache`,
|
|
44
|
+
* or by a route handler that userland sets up.
|
|
43
45
|
*/
|
|
44
46
|
cacheTag(...tags);
|
|
45
47
|
/**
|
|
46
|
-
* Sanity Live handles on-demand revalidation, so the default 15min time
|
|
48
|
+
* Sanity Live handles on-demand revalidation, so the default 15min time-based revalidation is too short,
|
|
47
49
|
* userland can still set a shorter revalidate time by calling `cacheLife` themselves.
|
|
48
50
|
*/
|
|
49
|
-
cacheLife(
|
|
51
|
+
cacheLife(sanity);
|
|
50
52
|
return {
|
|
51
53
|
data: result,
|
|
52
54
|
sourceMap: resultSourceMap || null,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["SanityLive","SanityLiveClientComponent"],"sources":["../../../../src/live/conditions/next-js/defineLive.tsx"],"sourcesContent":["import {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, revalidate} 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 * @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 * 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 * 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 * return {perspective: perspective ?? 'drafts', 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, stega} = await getDynamicFetchOptions()\n *\n * return <CachedPage slug={slug} perspective={perspective} stega={stega} />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\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 * 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 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 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 `expireTags` Server Action with the `expireTag` function from `next/cache`\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({revalidate})\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,\n apiVersion,\n useProjectHostname,\n requestTagPrefix,\n token: includeDrafts ? browserToken : undefined,\n }}\n includeDrafts={includeDrafts ? true : undefined}\n requestTag={requestTag}\n waitFor={shouldWaitFor}\n action={action ?? (shouldWaitFor === 'function' ? 'refresh' : revalidateSyncTagsAction)}\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,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MAAM;EAEtC,MAAM,EACJ,eAAe,iBAAiB,OAChC,aAAa,qCACb,SAEA,QACA,SACA,WACA,aACA,WACA,aACE;EACJ,MAAM,EAAC,WAAW,SAAS,SAAS,YAAY,oBAAoB,qBAClE,OAAO,QAAQ;EAEjB,MAAM,gBAAgB,OAAO,iBAAiB,YAAY,CAAC,CAAC,gBAAgB;EAC5E,MAAM,gBAAgB,YAAY,cAAc,CAAC,gBAAgB,UAAU,KAAA;AAG3E,aAAW,OAAO;AAElB,SACE,oBAACC,YAAD;GACE,QAAQ;IACN;IACA;IACA;IACA;IACA;IACA;IACA,OAAO,gBAAgB,eAAe,KAAA;IACvC;GACD,eAAe,gBAAgB,OAAO,KAAA;GAC1B;GACZ,SAAS;GACT,QAAQ,WAAW,kBAAkB,aAAa,YAAY;GACrD;GACE;GACE;GACF;GACD;GACV,CAAA;;AAGN,cAAW,cAAc;AAEzB,QAAO;EAAC;EAAa,YAAA;EAAW"}
|
|
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} 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 * @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 * 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 * 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 * return {perspective: perspective ?? 'drafts', 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, stega} = await getDynamicFetchOptions()\n *\n * return <CachedPage slug={slug} perspective={perspective} stega={stega} />\n * }\n *\n * async function CachedPage({\n * slug,\n * perspective,\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 * 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 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 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,\n apiVersion,\n useProjectHostname,\n requestTagPrefix,\n token: includeDrafts ? browserToken : undefined,\n }}\n includeDrafts={includeDrafts ? true : undefined}\n requestTag={requestTag}\n waitFor={shouldWaitFor}\n action={action ?? (shouldWaitFor === 'function' ? 'refresh' : revalidateSyncTagsAction)}\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":";;;;;;;;;;AA+PA,SAAgB,WAAW,QAA2B;CACpD,MAAM,EAAC,QAAQ,SAAS,aAAa,cAAc,SAAS,UAAS;AAErE,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,oDAAoD;AAGtE,KAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,eAAe,gBAAgB,MAC5E,SAAQ,KACN,8LACD;AAGH,KAAI,QAAQ,IAAI,aAAa,iBAAiB,CAAC,gBAAgB,iBAAiB,MAC9E,SAAQ,KACN,iXACD;CAGH,MAAM,SAAS,QAAQ,WAAW;EAChC,kBAAkB;EAClB,QAAQ;EACR,aAAa;EACb,OAAO;EACR,CAAC;CAEF,MAAM,cAAgC,eAAe,YAAY,EAC/D,OACA,SAAS,EAAE,EACX,aACA,OACA,MAAM,kBAAkB,EAAE,EAC1B,aAAa,wCACZ;AACD,MAAI,OACF,4BAA2B;GAAC;GAAa;GAAM,CAAC;EAGlD,MAAM,SAAS,cAAc,gB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@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseTags.d.ts","names":[],"sources":["../src/live/shared/resolvePerspectiveFromCookies.ts","../src/live/shared/constants.ts","../src/live/shared/parseTags.ts"],"mappings":";;;;;AAiEA;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"parseTags.d.ts","names":[],"sources":["../src/live/shared/resolvePerspectiveFromCookies.ts","../src/live/shared/constants.ts","../src/live/shared/parseTags.ts"],"mappings":";;;;;AAiEA;;;;;;;;;;;;;;;;;;;;;;AC9DA;;;;;;ACD6B;;;;;;;;;;;;;;;;;;AA6B7B;;;;;;;;;;;iBFkCsB,6BAAA,CAAA;EACpB,OAAA,EAAS;AAAA;EAET,OAAA,EAAS,OAAA,CAAQ,UAAA,QAAkB,OAAA;AAAA,IACjC,OAAA,CAAQ,eAAA;AAAA,cClEC,cAAA;AAAA,UCCH,UAAA;EACR,IAAA,YAAgB,cAAA,GAAiB,OAAA;EACjC,iBAAA,EAAmB,OAAA;EACnB,MAAA,SAAe,cAAA;AAAA;;;;;;;;;;;;;;;;;;;;ADJjB;;iBC4BgB,SAAA,CAAU,UAAA,YAAsB,UAAA"}
|
|
@@ -3,7 +3,7 @@ import { perspectiveCookieName } from "@sanity/preview-url-secret/constants";
|
|
|
3
3
|
import { preconnect } from "react-dom";
|
|
4
4
|
import { generateHelpUrl } from "@sanity/generate-help-url";
|
|
5
5
|
/**
|
|
6
|
-
* Uses the React DOM `preconnect` function to preconnect to the Live Event API origin early,
|
|
6
|
+
* 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.
|
|
7
7
|
*/
|
|
8
8
|
function preconnect$1(client) {
|
|
9
9
|
const { origin } = new URL(client.getUrl("", false));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolvePerspectiveFromCookies.js","names":["preconnect"],"sources":["../src/live/shared/preconnect.ts","../src/live/shared/strictValidation.ts","../src/live/shared/resolvePerspectiveFromCookies.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,
|
|
1
|
+
{"version":3,"file":"resolvePerspectiveFromCookies.js","names":["preconnect"],"sources":["../src/live/shared/preconnect.ts","../src/live/shared/strictValidation.ts","../src/live/shared/resolvePerspectiveFromCookies.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"],"mappings":";;;;;;;AAMA,SAAgBA,aAAW,QAA4B;CACrD,MAAM,EAAC,WAAU,IAAI,IAAI,OAAO,OAAO,IAAI,MAAM,CAAC;AAClD,YAAmB,OAAO;;ACN5B,SAAgB,8BAA8B,OAAwC;AACpF,KAAI,OAAO,MAAM,kBAAkB,UACjC,OAAM,IAAI,MACR,iJAAiJ,gBAAgB,0BAA0B,IAC3L,EAAC,OAAO,OAAM,CACf;;AAIL,SAAgB,2BAA2B,SAGlC;AACP,KAAI,OAAO,QAAQ,gBAAgB,eAAe,QAAQ,gBAAgB,KACxE,OAAM,IAAI,MACR,kIAAkI,gBAAgB,2BAA2B,IAC7K,EAAC,OAAO,SAAQ,CACjB;AAEH,KAAI,OAAO,QAAQ,UAAU,UAC3B,OAAM,IAAI,MACR,4IAA4I,gBAAgB,2BAA2B,IACvL,EAAC,OAAO,SAAQ,CACjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACwCL,eAAsB,8BAA8B,EAClD,SAAS,OAGkB;AAC3B,QAAO,IAAI,IAAI,sBAAsB,GACjC,oBAAoB,IAAI,IAAI,sBAAsB,EAAE,OAAO,SAAS,GACpE"}
|
package/dist/types.d.ts
CHANGED
|
@@ -23,29 +23,59 @@ type DefinedFetchType = <const QueryString extends string>(options: {
|
|
|
23
23
|
/**
|
|
24
24
|
* Content perspective used for the fetch.
|
|
25
25
|
*
|
|
26
|
-
* @
|
|
26
|
+
* @remarks
|
|
27
|
+
* Requires `serverToken` to be configured in `defineLive()`
|
|
28
|
+
*
|
|
29
|
+
* @defaultValue
|
|
30
|
+
* The default is `'published'` unless
|
|
31
|
+
* - `Cache Components` are disabled
|
|
32
|
+
* - `defineLive()` was given a `serverToken`
|
|
33
|
+
* - `defineLive()` is not set to `strict: true`
|
|
34
|
+
* - `draftMode()` is enabled
|
|
35
|
+
*
|
|
36
|
+
* If all of the above conditions are met, then the default value will be resolved from attempting to read the `'sanity-preview-perspective'` cookie and fall back to `'drafts'` if not set
|
|
27
37
|
*/
|
|
28
38
|
perspective?: LivePerspective;
|
|
29
39
|
/**
|
|
30
40
|
* Enables stega encoding of the data. This is typically only used in draft
|
|
31
41
|
* mode with `perspective: 'drafts'` and `@sanity/visual-editing`.
|
|
32
42
|
*
|
|
33
|
-
* @
|
|
43
|
+
* @remarks
|
|
44
|
+
* Requires `serverToken` to be configured in `defineLive()`
|
|
45
|
+
*
|
|
46
|
+
* @defaultValue
|
|
47
|
+
* The default is `false` unless
|
|
48
|
+
* - `Cache Components` are disabled
|
|
49
|
+
* - `defineLive()` was given a `serverToken`
|
|
50
|
+
* - `defineLive()` is not set to `strict: true`
|
|
51
|
+
* - `defineLive()` was given a `client` that defines `stega.studioUrl`
|
|
52
|
+
* - `draftMode()` is enabled
|
|
53
|
+
*
|
|
54
|
+
* If all of the above conditions are met, then the default value will be `true`
|
|
34
55
|
*/
|
|
35
56
|
stega?: boolean;
|
|
36
57
|
/**
|
|
37
58
|
* Additional cache tags to associate with this fetch.
|
|
38
59
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* The default behavior will always add cache tags automatically for the query based on the `syncTags` response returned by Content Lake.
|
|
62
|
+
* You only need to define custom tags if you also mutate content in a server action and need to implement read-your-own-write UI.
|
|
63
|
+
* @see https://nextjs.org/docs/app/api-reference/functions/updateTag#server-action-with-read-your-own-writes
|
|
64
|
+
*
|
|
65
|
+
* When `cacheComponents: false` your custom tags are appended to the underlying `next.tags` array on the `fetch` request and are subject to the tag length and max tag items limits of Next.js.
|
|
66
|
+
* @see https://nextjs.org/docs/app/api-reference/functions/fetch#optionsnexttags
|
|
67
|
+
* When `cacheComponents: true` your custom tags are appended to the underlying `cacheTag()` call and are subject to the tag length and max tag items limits of Next.js.
|
|
68
|
+
* @see https://nextjs.org/docs/app/api-reference/functions/cacheTag#good-to-know
|
|
42
69
|
*/
|
|
43
70
|
tags?: string[];
|
|
44
71
|
/**
|
|
45
72
|
* Request tag used to identify the request in Sanity Content Lake logs.
|
|
46
73
|
*
|
|
47
74
|
* @see https://www.sanity.io/docs/reference-api-request-tags
|
|
48
|
-
*
|
|
75
|
+
*
|
|
76
|
+
* @defaultValue
|
|
77
|
+
* If `cacheComponents: true` then the default value is `'next-loader.fetch.cache-components'`
|
|
78
|
+
* otherwise it's `'next-loader.fetch'`
|
|
49
79
|
*/
|
|
50
80
|
requestTag?: string;
|
|
51
81
|
}) => Promise<{
|
|
@@ -59,20 +89,29 @@ type DefinedFetchType = <const QueryString extends string>(options: {
|
|
|
59
89
|
*/
|
|
60
90
|
interface DefinedLiveProps {
|
|
61
91
|
/**
|
|
62
|
-
* Include draft and content release version events in the live connection.
|
|
92
|
+
* Include draft and content release version events in the live connection. Otherwise only events for published content are included.
|
|
63
93
|
*
|
|
64
|
-
*
|
|
65
|
-
* configured in `defineLive()`
|
|
94
|
+
* @remarks
|
|
95
|
+
* Requires `browserToken` to be configured in `defineLive()`
|
|
66
96
|
*
|
|
67
|
-
* @defaultValue
|
|
97
|
+
* @defaultValue
|
|
98
|
+
* The default is `false` unless
|
|
99
|
+
* - `Cache Components` are disabled
|
|
100
|
+
* - `defineLive()` was given a `browserToken`
|
|
101
|
+
* - `defineLive()` is not set to `strict: true`
|
|
102
|
+
* - `draftMode()` is enabled
|
|
103
|
+
*
|
|
104
|
+
* If all of the above conditions are met, then the default value will be `true`
|
|
68
105
|
*/
|
|
69
106
|
includeDrafts?: boolean;
|
|
70
107
|
/**
|
|
71
|
-
* Request tag used to identify the live EventSource request in Sanity Content
|
|
72
|
-
* Lake logs.
|
|
108
|
+
* Request tag used to identify the live EventSource request in Sanity Content Lake logs.
|
|
73
109
|
*
|
|
74
110
|
* @see https://www.sanity.io/docs/reference-api-request-tags
|
|
75
|
-
*
|
|
111
|
+
*
|
|
112
|
+
* @defaultValue
|
|
113
|
+
* If `cacheComponents: true` then the default value is `'next-loader.live.cache-components'`
|
|
114
|
+
* otherwise it's `'next-loader.live'`
|
|
76
115
|
*/
|
|
77
116
|
requestTag?: string;
|
|
78
117
|
/**
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/live/shared/types.ts"],"mappings":";;AAcA;;;KAAY,eAAA,GAAkB,OAAA,CAAQ,iBAAA;;AAStC;;;;;;KAAY,gBAAA,sCAAsD,OAAA;;;;EAIhE,KAAA,EAAO,WAAA;;;;EAIP,MAAA,GAAS,WAAA,GAAc,OAAA,CAAQ,WAAA
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/live/shared/types.ts"],"mappings":";;AAcA;;;KAAY,eAAA,GAAkB,OAAA,CAAQ,iBAAA;;AAStC;;;;;;KAAY,gBAAA,sCAAsD,OAAA;;;;EAIhE,KAAA,EAAO,WAAA;;;;EAIP,MAAA,GAAS,WAAA,GAAc,OAAA,CAAQ,WAAA;;;;;;;;;;;;;;;;EAgB/B,WAAA,GAAc,eAAA;;;;;;AAqDhB;;;;;;;;;;;;EAnCE,KAAA;;;;;;;;;;;;;;EAcA,IAAA;EAqFW;AAGb;;;;;;;;EA9EE,UAAA;AAAA,MACI,OAAA;EACJ,IAAA,EAAM,YAAA,CAAa,WAAA;EACnB,SAAA,EAAW,gBAAA;EACX,IAAA;AAAA;;;;;UAOe,gBAAA;EAyGf;AAOF;;;;;;;;;;;;;;EAhGE,aAAA;;;;;;;;;;EAUA,UAAA;;;;;EAKA,OAAA;;;;;;;EAOA,MAAA,GAAS,gBAAA;EAuFM;;;;AAcjB;EA/FE,OAAA,GAAU,iBAAA;;;;AAyHZ;EApHE,SAAA,GAAY,mBAAA;;;;AA6Hd;EAxHE,WAAA,GAAc,qBAAA;;;;;EAKd,SAAA,GAAY,mBAAA;;;AA2Hd;;EAtHE,QAAA,GAAW,kBAAA;AAAA;AAAA,UAGI,iBAAA;;;;EAIf,MAAA,EAAQ,YAAA;;;;;;;EAOR,WAAA;;;AAiHF;;;;;EAzGE,YAAA;;;;;;;;;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
5
5
|
"keywords": [
|
|
6
6
|
"live",
|
|
@@ -64,22 +64,22 @@
|
|
|
64
64
|
"@sanity/preview-url-secret": "^4.0.6",
|
|
65
65
|
"@sanity/visual-editing": "^5.3.6",
|
|
66
66
|
"@sanity/webhook": "^4.0.4",
|
|
67
|
-
"groq": "^5.
|
|
67
|
+
"groq": "^5.26.0",
|
|
68
68
|
"history": "^5.3.0"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"@sanity/tsconfig": "^2.1.0",
|
|
72
72
|
"@types/js-yaml": "^4.0.9",
|
|
73
73
|
"@types/node": "^24.12.4",
|
|
74
|
-
"@types/react": "^19.2.
|
|
74
|
+
"@types/react": "^19.2.15",
|
|
75
75
|
"@types/react-dom": "^19.2.3",
|
|
76
76
|
"@vercel/stega": "^1.1.0",
|
|
77
77
|
"@vitejs/plugin-react": "^6.0.2",
|
|
78
|
-
"@vitest/browser-playwright": "^4.1.
|
|
79
|
-
"@vitest/coverage-v8": "^4.1.
|
|
78
|
+
"@vitest/browser-playwright": "^4.1.7",
|
|
79
|
+
"@vitest/coverage-v8": "^4.1.7",
|
|
80
80
|
"js-yaml": "^4.1.1",
|
|
81
81
|
"msw": "^2.14.6",
|
|
82
|
-
"next": "16.3.0-canary.
|
|
82
|
+
"next": "16.3.0-canary.24",
|
|
83
83
|
"publint": "^0.3.21",
|
|
84
84
|
"react": "^19.2.6",
|
|
85
85
|
"react-dom": "^19.2.6",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"typedoc": "^0.28.19",
|
|
89
89
|
"typescript": "5.9.3",
|
|
90
90
|
"vite": "^8.0.13",
|
|
91
|
-
"vitest": "^4.1.
|
|
91
|
+
"vitest": "^4.1.7",
|
|
92
92
|
"vitest-browser-react": "^2.2.0",
|
|
93
93
|
"vitest-package-exports": "^1.2.0"
|
|
94
94
|
},
|
|
@@ -97,7 +97,7 @@
|
|
|
97
97
|
"next": "^16.0.0-0",
|
|
98
98
|
"react": "^19.2.3",
|
|
99
99
|
"react-dom": "^19.2.3",
|
|
100
|
-
"sanity": "^5.
|
|
100
|
+
"sanity": "^5.26.0",
|
|
101
101
|
"styled-components": "^6.1"
|
|
102
102
|
},
|
|
103
103
|
"engines": {
|