next-sanity 13.0.0-cache-components.19 → 13.0.0-cache-components.20
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/RefreshOnInterval.js +14 -0
- package/dist/RefreshOnInterval.js.map +1 -0
- package/dist/SanityLive.js +55 -49
- package/dist/SanityLive.js.map +1 -1
- package/dist/defineLive.d.ts +3 -121
- package/dist/defineLive.d.ts.map +1 -1
- package/dist/hooks/index.js +5 -1
- package/dist/hooks/index.js.map +1 -1
- package/dist/live/client-components/index.d.ts +19 -12
- package/dist/live/client-components/index.d.ts.map +1 -1
- package/dist/live/conditions/default/index.d.ts +3 -10
- package/dist/live/conditions/default/index.d.ts.map +1 -1
- package/dist/live/conditions/default/index.js.map +1 -1
- package/dist/live/conditions/next-js/index.d.ts +3 -2
- package/dist/live/conditions/next-js/index.d.ts.map +1 -1
- package/dist/live/conditions/next-js/index.js +10 -10
- package/dist/live/conditions/next-js/index.js.map +1 -1
- package/dist/live/conditions/react-server/index.d.ts +2 -1
- package/dist/live/conditions/react-server/index.js +11 -11
- package/dist/live/conditions/react-server/index.js.map +1 -1
- package/dist/live/server-actions/index.default.d.ts +9 -5
- package/dist/live/server-actions/index.default.d.ts.map +1 -1
- package/dist/live/server-actions/index.default.js +25 -26
- package/dist/live/server-actions/index.default.js.map +1 -1
- package/dist/live/server-actions/index.next-js.d.ts +9 -5
- package/dist/live/server-actions/index.next-js.d.ts.map +1 -1
- package/dist/live/server-actions/index.next-js.js +14 -25
- package/dist/live/server-actions/index.next-js.js.map +1 -1
- package/dist/types.d.ts +75 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +6 -6
- package/dist/LiveEvents.js +0 -43
- package/dist/LiveEvents.js.map +0 -1
- package/dist/LiveEventsIncludingDrafts.js +0 -46
- package/dist/LiveEventsIncludingDrafts.js.map +0 -1
- package/dist/PresentationComlink.js +0 -36
- package/dist/PresentationComlink.js.map +0 -1
- package/dist/context.js +0 -22
- package/dist/context.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["fetch","Live","SanityLiveClientComponent"],"sources":["../../../../src/live/conditions/next-js/defineLive.tsx"],"sourcesContent":["import type {DefinedFetchType, DefinedLiveProps, LiveOptions} from '#live/types'\n\nimport {DRAFT_SYNC_TAG_PREFIX, PUBLISHED_SYNC_TAG_PREFIX, revalidate} from '#live/constants'\nimport {SanityLive as SanityLiveClientComponent} from 'next-sanity/live/client-components'\nimport {
|
|
1
|
+
{"version":3,"file":"index.js","names":["fetch","Live","SanityLiveClientComponent"],"sources":["../../../../src/live/conditions/next-js/defineLive.tsx"],"sourcesContent":["import type {DefinedFetchType, DefinedLiveProps, LiveOptions} from '#live/types'\n\nimport {DRAFT_SYNC_TAG_PREFIX, PUBLISHED_SYNC_TAG_PREFIX, revalidate} from '#live/constants'\nimport {SanityLive as SanityLiveClientComponent} from 'next-sanity/live/client-components'\nimport {actionRefresh, actionUpdateTags} from 'next-sanity/live/server-actions'\nimport {cacheLife, cacheTag} from 'next/cache'\nimport {preconnect} from 'react-dom'\n\nimport type {DefinedSanityFetchType} from '../react-server/defineLive'\n\nexport function defineLive(config: LiveOptions): {\n sanityFetch: DefinedSanityFetchType\n SanityLive: React.ComponentType<DefinedLiveProps>\n} {\n const {client: _client, serverToken, browserToken} = config\n\n if (!_client) {\n throw new Error('`client` is required for `defineLive` to function')\n }\n\n if (process.env.NODE_ENV !== 'production' && !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 !== 'production' && !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({allowReconfigure: false, useCdn: true})\n const {token: originalToken} = client.config()\n\n const fetch: DefinedFetchType = async function fetch({\n query,\n params = {},\n perspective = 'published',\n stega = false,\n tags: customCacheTags = [],\n requestTag = 'next-loader.fetch.cache-components',\n }) {\n const useCdn = perspective === 'published'\n\n const {result, resultSourceMap, syncTags} = await client.fetch(query, params, {\n filterResponse: false,\n returnQuery: false,\n perspective,\n useCdn,\n stega,\n cacheMode: useCdn ? 'noStale' : undefined,\n tag: requestTag,\n token: perspective === 'published' ? originalToken : serverToken || originalToken, // @TODO can pass undefined instead of config.token here?\n })\n const tags = [\n ...customCacheTags,\n ...(syncTags || []).map(\n (tag) =>\n `${perspective === 'published' ? PUBLISHED_SYNC_TAG_PREFIX : DRAFT_SYNC_TAG_PREFIX}${tag}`,\n ),\n ]\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 Live: React.ComponentType<DefinedLiveProps> = function Live(props) {\n const {\n includeAllDocuments = false,\n action = actionUpdateTags,\n restartAction = actionRefresh,\n reconnectAction = actionRefresh,\n goAwayAction,\n welcomeAction,\n refreshOnMount = false,\n refreshOnFocus = false,\n refreshOnReconnect = false,\n requestTag = 'next-loader.live.cache-components',\n } = props\n\n const shouldIncludeDrafts = typeof browserToken === 'string' && includeAllDocuments\n\n const {projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix} =\n client.config()\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 const {origin} = new URL(client.getUrl('', false))\n preconnect(origin)\n\n return (\n <SanityLiveClientComponent\n config={{\n projectId,\n dataset,\n apiHost,\n apiVersion,\n useProjectHostname,\n requestTagPrefix,\n token: shouldIncludeDrafts ? browserToken : undefined,\n }}\n includeAllDocuments={shouldIncludeDrafts}\n action={action}\n reconnectAction={reconnectAction === false ? undefined : reconnectAction}\n restartAction={restartAction === false ? undefined : restartAction}\n welcomeAction={welcomeAction}\n goAwayAction={goAwayAction}\n requestTag={requestTag}\n refreshOnMount={refreshOnMount}\n refreshOnFocus={refreshOnFocus}\n refreshOnReconnect={refreshOnReconnect}\n />\n )\n }\n Live.displayName = 'SanityLiveServerComponent'\n\n return {\n sanityFetch: fetch,\n SanityLive: Live,\n }\n}\n"],"mappings":";;;;;;;;AAUA,SAAgB,WAAW,QAGzB;CACA,MAAM,EAAC,QAAQ,SAAS,aAAa,iBAAgB;AAErD,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,oDAAoD;AAGtE,KAAI,QAAQ,IAAI,aAAa,gBAAgB,CAAC,eAAe,gBAAgB,MAC3E,SAAQ,KACN,8LACD;AAGH,KAAI,QAAQ,IAAI,aAAa,gBAAgB,CAAC,gBAAgB,iBAAiB,MAC7E,SAAQ,KACN,iXACD;CAGH,MAAM,SAAS,QAAQ,WAAW;EAAC,kBAAkB;EAAO,QAAQ;EAAK,CAAC;CAC1E,MAAM,EAAC,OAAO,kBAAiB,OAAO,QAAQ;CAE9C,MAAM,QAA0B,eAAeA,QAAM,EACnD,OACA,SAAS,EAAE,EACX,cAAc,aACd,QAAQ,OACR,MAAM,kBAAkB,EAAE,EAC1B,aAAa,wCACZ;EACD,MAAM,SAAS,gBAAgB;EAE/B,MAAM,EAAC,QAAQ,iBAAiB,aAAY,MAAM,OAAO,MAAM,OAAO,QAAQ;GAC5E,gBAAgB;GAChB,aAAa;GACb;GACA;GACA;GACA,WAAW,SAAS,YAAY,KAAA;GAChC,KAAK;GACL,OAAO,gBAAgB,cAAc,gBAAgB,eAAe;GACrE,CAAC;EACF,MAAM,OAAO,CACX,GAAG,iBACH,IAAI,YAAY,EAAE,EAAE,KACjB,QACC,GAAG,gBAAgB,cAAc,4BAA4B,wBAAwB,MACxF,CACF;AAID,WAAS,GAAG,KAAK;AAKjB,YAAU,EAAC,YAAW,CAAC;AAEvB,SAAO;GAAC,MAAM;GAAQ,WAAW,mBAAmB;GAAM;GAAK;;CAGjE,MAAM,OAA8C,SAASC,OAAK,OAAO;EACvE,MAAM,EACJ,sBAAsB,OACtB,SAAS,kBACT,gBAAgB,eAChB,kBAAkB,eAClB,cACA,eACA,iBAAiB,OACjB,iBAAiB,OACjB,qBAAqB,OACrB,aAAa,wCACX;EAEJ,MAAM,sBAAsB,OAAO,iBAAiB,YAAY;EAEhE,MAAM,EAAC,WAAW,SAAS,SAAS,YAAY,oBAAoB,qBAClE,OAAO,QAAQ;EAGjB,MAAM,EAAC,WAAU,IAAI,IAAI,OAAO,OAAO,IAAI,MAAM,CAAC;AAClD,aAAW,OAAO;AAElB,SACE,oBAACC,YAAAA;GACC,QAAQ;IACN;IACA;IACA;IACA;IACA;IACA;IACA,OAAO,sBAAsB,eAAe,KAAA;IAC7C;GACD,qBAAqB;GACb;GACR,iBAAiB,oBAAoB,QAAQ,KAAA,IAAY;GACzD,eAAe,kBAAkB,QAAQ,KAAA,IAAY;GACtC;GACD;GACF;GACI;GACA;GACI;IACpB;;AAGN,MAAK,cAAc;AAEnB,QAAO;EACL,aAAa;EACb,YAAY;EACb"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { t as isCorsOriginError } from "../../../isCorsOriginError.js";
|
|
2
|
-
import {
|
|
2
|
+
import { r as PerspectiveType } from "../../../types.js";
|
|
3
|
+
import { a as resolvePerspectiveFromCookies, r as defineLive } from "../../../defineLive.js";
|
|
3
4
|
export { type PerspectiveType as LivePerspective, defineLive, isCorsOriginError, resolvePerspectiveFromCookies };
|
|
@@ -5,7 +5,7 @@ import { perspectiveCookieName } from "@sanity/preview-url-secret/constants";
|
|
|
5
5
|
import { cookies, draftMode } from "next/headers";
|
|
6
6
|
import { jsx } from "react/jsx-runtime";
|
|
7
7
|
import { SanityLive } from "next-sanity/live/client-components";
|
|
8
|
-
import {
|
|
8
|
+
import { actionRefresh, actionUpdateTags } from "next-sanity/live/server-actions";
|
|
9
9
|
import { preconnect } from "react-dom";
|
|
10
10
|
function defineLive(config) {
|
|
11
11
|
const { client: _client, serverToken, browserToken, fetchOptions, stega: stegaEnabled = true } = config;
|
|
@@ -57,9 +57,9 @@ function defineLive(config) {
|
|
|
57
57
|
};
|
|
58
58
|
};
|
|
59
59
|
const SanityLive$1 = async function SanityLive$2(props) {
|
|
60
|
-
const {
|
|
60
|
+
const { includeAllDocuments = (await draftMode()).isEnabled, action = actionUpdateTags, restartAction = actionRefresh, reconnectAction = actionRefresh, goAwayAction, welcomeAction, refreshOnMount, refreshOnFocus, refreshOnReconnect, requestTag = "next-loader.live" } = props;
|
|
61
61
|
const { projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix } = client.config();
|
|
62
|
-
const
|
|
62
|
+
const shouldIncludeAllDocuments = typeof browserToken === "string" && includeAllDocuments;
|
|
63
63
|
const { origin } = new URL(client.getUrl("", false));
|
|
64
64
|
preconnect(origin);
|
|
65
65
|
return /* @__PURE__ */ jsx(SanityLive, {
|
|
@@ -70,18 +70,18 @@ function defineLive(config) {
|
|
|
70
70
|
apiVersion,
|
|
71
71
|
useProjectHostname,
|
|
72
72
|
requestTagPrefix,
|
|
73
|
-
token:
|
|
73
|
+
token: shouldIncludeAllDocuments ? browserToken : void 0
|
|
74
74
|
},
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
includeAllDocuments: shouldIncludeAllDocuments,
|
|
76
|
+
action,
|
|
77
|
+
reconnectAction: reconnectAction === false ? void 0 : reconnectAction,
|
|
78
|
+
restartAction: restartAction === false ? void 0 : restartAction,
|
|
79
|
+
welcomeAction,
|
|
80
|
+
goAwayAction,
|
|
78
81
|
requestTag,
|
|
79
82
|
refreshOnMount,
|
|
80
83
|
refreshOnFocus,
|
|
81
|
-
refreshOnReconnect
|
|
82
|
-
onError,
|
|
83
|
-
onGoAway,
|
|
84
|
-
intervalOnGoAway
|
|
84
|
+
refreshOnReconnect
|
|
85
85
|
});
|
|
86
86
|
};
|
|
87
87
|
SanityLive$1.displayName = "SanityLiveServerComponent";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["sanityFetch","tag","SanityLive","SanityLiveClientComponent"],"sources":["../../../../src/live/conditions/react-server/defineLive.tsx"],"sourcesContent":["import type {DefinedLiveProps, PerspectiveType} from '#live/types'\n\nimport {sanitizePerspective} from '#live/sanitizePerspective'\nimport {\n type ClientPerspective,\n type ClientReturn,\n type ContentSourceMap,\n type QueryParams,\n type SanityClient,\n} from '@sanity/client'\nimport {perspectiveCookieName} from '@sanity/preview-url-secret/constants'\nimport {SanityLive as SanityLiveClientComponent} from 'next-sanity/live/client-components'\nimport {actionLiveEvent, actionLiveEventIncludingDrafts} from 'next-sanity/live/server-actions'\nimport {cookies, draftMode} from 'next/headers'\nimport {preconnect} from 'react-dom'\n\n/**\n * @public\n */\nexport type DefinedSanityFetchType = <const QueryString extends string>(options: {\n query: QueryString\n params?: QueryParams | Promise<QueryParams>\n /**\n * Add custom `next.tags` to the underlying fetch request.\n * @see https://nextjs.org/docs/app/api-reference/functions/fetch#optionsnexttags\n * This can be used in conjunction with custom fallback revalidation strategies, as well as with custom Server Actions that mutate data and want to render with fresh data right away (faster than the Live Event latency).\n * @defaultValue `['sanity']`\n */\n tags?: string[]\n perspective?: Exclude<ClientPerspective, 'raw'>\n stega?: boolean\n /**\n * This request tag is used to identify the request when viewing request logs from your Sanity Content Lake.\n * @see https://www.sanity.io/docs/reference-api-request-tags\n * @defaultValue 'next-loader.fetch'\n */\n requestTag?: string\n}) => Promise<{\n data: ClientReturn<QueryString>\n sourceMap: ContentSourceMap | null\n tags: string[]\n}>\n\n/**\n * @public\n */\nexport interface DefinedSanityLiveProps extends DefinedLiveProps {\n /**\n * @defaultValue `automatically resolved`\n */\n perspective?: PerspectiveType\n}\n\n/**\n * @public\n */\nexport interface DefineSanityLiveOptions {\n /**\n * Required for `sanityFetch` and `SanityLive` to work\n */\n client: SanityClient\n /**\n * Optional. If provided then the token needs to have permissions to query documents with `drafts.` prefixes in order for `perspective: 'drafts'` to work.\n * This token is not shared with the browser.\n */\n serverToken?: string | false\n /**\n * Optional. This token is shared with the browser, and should only have access to query published documents.\n * It is used to setup a `Live Draft Content` EventSource connection, and enables live previewing drafts stand-alone, outside of Presentation Tool.\n */\n browserToken?: string | false\n /**\n * Fetch options used by `sanityFetch`\n */\n fetchOptions?: {\n /**\n * Optional, enables time based revalidation in addition to the EventSource connection.\n * @defaultValue `false`\n */\n revalidate?: number | false\n }\n /**\n * Optional. Include stega encoding when draft mode is enabled.\n * @defaultValue `true`\n */\n stega?: boolean\n}\n\nexport function defineLive(config: DefineSanityLiveOptions): {\n /**\n * @deprecated use `fetch` instead, and define your own `sanityFetch` function with logic for when to toggle `stega` and `perspective`\n */\n sanityFetch: DefinedSanityFetchType\n /**\n * @deprecated use `Live` instead, and define your own `SanityLive` component with logic for when to toggle `perspective`\n */\n SanityLive: React.ComponentType<DefinedSanityLiveProps>\n // fetch: DefinedFetchType\n // Live: React.ComponentType<DefinedLiveProps>\n} {\n const {\n client: _client,\n serverToken,\n browserToken,\n fetchOptions,\n stega: stegaEnabled = true,\n } = config\n\n if (!_client) {\n throw new Error('`client` is required for `defineLive` to function')\n }\n\n if (process.env.NODE_ENV !== 'production' && !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 !== 'production' && !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({allowReconfigure: false, useCdn: false})\n const {token: originalToken} = client.config()\n const studioUrlDefined = typeof client.config().stega.studioUrl !== 'undefined'\n\n const sanityFetch: DefinedSanityFetchType = async function sanityFetch<\n const QueryString extends string,\n >({\n query,\n params = {},\n stega: _stega,\n tags = ['sanity'],\n perspective: _perspective,\n tag,\n requestTag = tag ?? 'next-loader.fetch',\n }: {\n query: QueryString\n params?: QueryParams | Promise<QueryParams>\n stega?: boolean\n tags?: string[]\n perspective?: Exclude<ClientPerspective, 'raw'>\n tag?: string\n requestTag?: string\n }) {\n const stega = _stega ?? (stegaEnabled && studioUrlDefined && (await draftMode()).isEnabled)\n const perspective = _perspective ?? (await resolveCookiePerspective())\n const useCdn = perspective === 'published'\n const revalidate =\n fetchOptions?.revalidate !== undefined\n ? fetchOptions.revalidate\n : process.env.NODE_ENV === 'production'\n ? false\n : undefined\n\n // fetch the tags first, with revalidate to 1s to ensure we get the latest tags, eventually\n const {syncTags} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective: perspective as ClientPerspective,\n stega: false,\n returnQuery: false,\n next: {revalidate, tags: [...tags, 'sanity:fetch-sync-tags']},\n useCdn,\n cacheMode: useCdn ? 'noStale' : undefined,\n tag: [requestTag, 'fetch-sync-tags'].filter(Boolean).join('.'),\n })\n\n const cacheTags = [...tags, ...(syncTags?.map((tag) => `sanity:${tag}`) || [])]\n\n const {result, resultSourceMap} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective: perspective as ClientPerspective,\n stega,\n token: perspective !== 'published' && serverToken ? serverToken : originalToken,\n next: {revalidate, tags: cacheTags},\n useCdn,\n cacheMode: useCdn ? 'noStale' : undefined,\n tag: requestTag,\n })\n return {data: result, sourceMap: resultSourceMap || null, tags: cacheTags}\n }\n\n const SanityLive: React.ComponentType<DefinedSanityLiveProps> = async function SanityLive(props) {\n const {\n includeDrafts = (await draftMode()).isEnabled,\n onLiveEvent: onChange = actionLiveEvent,\n onLiveEventIncludingDrafts: onChangeIncludingDrafts = actionLiveEventIncludingDrafts,\n refreshOnMount,\n refreshOnFocus,\n refreshOnReconnect,\n requestTag = 'next-loader.live',\n onError,\n onGoAway,\n intervalOnGoAway,\n perspective: _perspective,\n } = props\n const {projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix} =\n client.config()\n const shouldIncludeDrafts = typeof browserToken === 'string' && includeDrafts\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 const {origin} = new URL(client.getUrl('', false))\n preconnect(origin)\n\n return (\n <SanityLiveClientComponent\n config={{\n projectId,\n dataset,\n apiHost,\n apiVersion,\n useProjectHostname,\n requestTagPrefix,\n token: shouldIncludeDrafts ? browserToken : undefined,\n }}\n includeDrafts={shouldIncludeDrafts}\n onLiveEvent={onChange}\n onLiveEventIncludingDrafts={onChangeIncludingDrafts}\n requestTag={requestTag}\n refreshOnMount={refreshOnMount}\n refreshOnFocus={refreshOnFocus}\n refreshOnReconnect={refreshOnReconnect}\n onError={onError}\n onGoAway={onGoAway}\n intervalOnGoAway={intervalOnGoAway}\n />\n )\n }\n SanityLive.displayName = 'SanityLiveServerComponent'\n\n return {\n sanityFetch,\n SanityLive,\n }\n}\n\nasync function resolveCookiePerspective(): Promise<Exclude<ClientPerspective, 'raw'>> {\n return (await draftMode()).isEnabled\n ? (await cookies()).has(perspectiveCookieName)\n ? sanitizePerspective((await cookies()).get(perspectiveCookieName)?.value, 'drafts')\n : 'drafts'\n : 'published'\n}\n"],"mappings":";;;;;;;;;AAwFA,SAAgB,WAAW,QAWzB;CACA,MAAM,EACJ,QAAQ,SACR,aACA,cACA,cACA,OAAO,eAAe,SACpB;AAEJ,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,oDAAoD;AAGtE,KAAI,QAAQ,IAAI,aAAa,gBAAgB,CAAC,eAAe,gBAAgB,MAC3E,SAAQ,KACN,8LACD;AAGH,KAAI,QAAQ,IAAI,aAAa,gBAAgB,CAAC,gBAAgB,iBAAiB,MAC7E,SAAQ,KACN,iXACD;CAGH,MAAM,SAAS,QAAQ,WAAW;EAAC,kBAAkB;EAAO,QAAQ;EAAM,CAAC;CAC3E,MAAM,EAAC,OAAO,kBAAiB,OAAO,QAAQ;CAC9C,MAAM,mBAAmB,OAAO,OAAO,QAAQ,CAAC,MAAM,cAAc;CAEpE,MAAM,cAAsC,eAAeA,cAEzD,EACA,OACA,SAAS,EAAE,EACX,OAAO,QACP,OAAO,CAAC,SAAS,EACjB,aAAa,cACb,KACA,aAAa,OAAO,uBASnB;EACD,MAAM,QAAQ,WAAW,gBAAgB,qBAAqB,MAAM,WAAW,EAAE;EACjF,MAAM,cAAc,gBAAiB,MAAM,0BAA0B;EACrE,MAAM,SAAS,gBAAgB;EAC/B,MAAM,aACJ,cAAc,eAAe,KAAA,IACzB,aAAa,aACb,QAAQ,IAAI,aAAa,eACvB,QACA,KAAA;EAGR,MAAM,EAAC,aAAY,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GACzD,gBAAgB;GACH;GACb,OAAO;GACP,aAAa;GACb,MAAM;IAAC;IAAY,MAAM,CAAC,GAAG,MAAM,yBAAyB;IAAC;GAC7D;GACA,WAAW,SAAS,YAAY,KAAA;GAChC,KAAK,CAAC,YAAY,kBAAkB,CAAC,OAAO,QAAQ,CAAC,KAAK,IAAI;GAC/D,CAAC;EAEF,MAAM,YAAY,CAAC,GAAG,MAAM,GAAI,UAAU,KAAK,UAAQ,UAAUC,QAAM,IAAI,EAAE,CAAE;EAE/E,MAAM,EAAC,QAAQ,oBAAmB,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GACxE,gBAAgB;GACH;GACb;GACA,OAAO,gBAAgB,eAAe,cAAc,cAAc;GAClE,MAAM;IAAC;IAAY,MAAM;IAAU;GACnC;GACA,WAAW,SAAS,YAAY,KAAA;GAChC,KAAK;GACN,CAAC;AACF,SAAO;GAAC,MAAM;GAAQ,WAAW,mBAAmB;GAAM,MAAM;GAAU;;CAG5E,MAAMC,eAA0D,eAAeA,aAAW,OAAO;EAC/F,MAAM,EACJ,iBAAiB,MAAM,WAAW,EAAE,WACpC,aAAa,WAAW,iBACxB,4BAA4B,0BAA0B,gCACtD,gBACA,gBACA,oBACA,aAAa,oBACb,SACA,UACA,kBACA,aAAa,iBACX;EACJ,MAAM,EAAC,WAAW,SAAS,SAAS,YAAY,oBAAoB,qBAClE,OAAO,QAAQ;EACjB,MAAM,sBAAsB,OAAO,iBAAiB,YAAY;EAGhE,MAAM,EAAC,WAAU,IAAI,IAAI,OAAO,OAAO,IAAI,MAAM,CAAC;AAClD,aAAW,OAAO;AAElB,SACE,oBAACC,YAAAA;GACC,QAAQ;IACN;IACA;IACA;IACA;IACA;IACA;IACA,OAAO,sBAAsB,eAAe,KAAA;IAC7C;GACD,eAAe;GACf,aAAa;GACb,4BAA4B;GAChB;GACI;GACA;GACI;GACX;GACC;GACQ;IAClB;;AAGN,cAAW,cAAc;AAEzB,QAAO;EACL;EACA,YAAA;EACD;;AAGH,eAAe,2BAAuE;AACpF,SAAQ,MAAM,WAAW,EAAE,aACtB,MAAM,SAAS,EAAE,IAAI,sBAAsB,GAC1C,qBAAqB,MAAM,SAAS,EAAE,IAAI,sBAAsB,EAAE,OAAO,SAAS,GAClF,WACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["sanityFetch","tag","SanityLive","SanityLiveClientComponent"],"sources":["../../../../src/live/conditions/react-server/defineLive.tsx"],"sourcesContent":["import type {DefinedLiveProps} from '#live/types'\n\nimport {sanitizePerspective} from '#live/sanitizePerspective'\nimport {\n type ClientPerspective,\n type ClientReturn,\n type ContentSourceMap,\n type QueryParams,\n type SanityClient,\n} from '@sanity/client'\nimport {perspectiveCookieName} from '@sanity/preview-url-secret/constants'\nimport {SanityLive as SanityLiveClientComponent} from 'next-sanity/live/client-components'\nimport {actionRefresh, actionUpdateTags} from 'next-sanity/live/server-actions'\nimport {cookies, draftMode} from 'next/headers'\nimport {preconnect} from 'react-dom'\n\n/**\n * @public\n */\nexport type DefinedSanityFetchType = <const QueryString extends string>(options: {\n query: QueryString\n params?: QueryParams | Promise<QueryParams>\n /**\n * Add custom `next.tags` to the underlying fetch request.\n * @see https://nextjs.org/docs/app/api-reference/functions/fetch#optionsnexttags\n * This can be used in conjunction with custom fallback revalidation strategies, as well as with custom Server Actions that mutate data and want to render with fresh data right away (faster than the Live Event latency).\n * @defaultValue `['sanity']`\n */\n tags?: string[]\n perspective?: Exclude<ClientPerspective, 'raw'>\n stega?: boolean\n /**\n * This request tag is used to identify the request when viewing request logs from your Sanity Content Lake.\n * @see https://www.sanity.io/docs/reference-api-request-tags\n * @defaultValue 'next-loader.fetch'\n */\n requestTag?: string\n}) => Promise<{\n data: ClientReturn<QueryString>\n sourceMap: ContentSourceMap | null\n tags: string[]\n}>\n\n/**\n * @public\n */\nexport interface DefineSanityLiveOptions {\n /**\n * Required for `sanityFetch` and `SanityLive` to work\n */\n client: SanityClient\n /**\n * Optional. If provided then the token needs to have permissions to query documents with `drafts.` prefixes in order for `perspective: 'drafts'` to work.\n * This token is not shared with the browser.\n */\n serverToken?: string | false\n /**\n * Optional. This token is shared with the browser, and should only have access to query published documents.\n * It is used to setup a `Live Draft Content` EventSource connection, and enables live previewing drafts stand-alone, outside of Presentation Tool.\n */\n browserToken?: string | false\n /**\n * Fetch options used by `sanityFetch`\n */\n fetchOptions?: {\n /**\n * Optional, enables time based revalidation in addition to the EventSource connection.\n * @defaultValue `false`\n */\n revalidate?: number | false\n }\n /**\n * Optional. Include stega encoding when draft mode is enabled.\n * @defaultValue `true`\n */\n stega?: boolean\n}\n\nexport function defineLive(config: DefineSanityLiveOptions): {\n /**\n * @deprecated use `fetch` instead, and define your own `sanityFetch` function with logic for when to toggle `stega` and `perspective`\n */\n sanityFetch: DefinedSanityFetchType\n /**\n * @deprecated use `Live` instead, and define your own `SanityLive` component with logic for when to toggle `perspective`\n */\n SanityLive: React.ComponentType<DefinedLiveProps>\n // fetch: DefinedFetchType\n // Live: React.ComponentType<DefinedLiveProps>\n} {\n const {\n client: _client,\n serverToken,\n browserToken,\n fetchOptions,\n stega: stegaEnabled = true,\n } = config\n\n if (!_client) {\n throw new Error('`client` is required for `defineLive` to function')\n }\n\n if (process.env.NODE_ENV !== 'production' && !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 !== 'production' && !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({allowReconfigure: false, useCdn: false})\n const {token: originalToken} = client.config()\n const studioUrlDefined = typeof client.config().stega.studioUrl !== 'undefined'\n\n const sanityFetch: DefinedSanityFetchType = async function sanityFetch<\n const QueryString extends string,\n >({\n query,\n params = {},\n stega: _stega,\n tags = ['sanity'],\n perspective: _perspective,\n tag,\n requestTag = tag ?? 'next-loader.fetch',\n }: {\n query: QueryString\n params?: QueryParams | Promise<QueryParams>\n stega?: boolean\n tags?: string[]\n perspective?: Exclude<ClientPerspective, 'raw'>\n tag?: string\n requestTag?: string\n }) {\n const stega = _stega ?? (stegaEnabled && studioUrlDefined && (await draftMode()).isEnabled)\n const perspective = _perspective ?? (await resolveCookiePerspective())\n const useCdn = perspective === 'published'\n const revalidate =\n fetchOptions?.revalidate !== undefined\n ? fetchOptions.revalidate\n : process.env.NODE_ENV === 'production'\n ? false\n : undefined\n\n // fetch the tags first, with revalidate to 1s to ensure we get the latest tags, eventually\n const {syncTags} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective: perspective as ClientPerspective,\n stega: false,\n returnQuery: false,\n next: {revalidate, tags: [...tags, 'sanity:fetch-sync-tags']},\n useCdn,\n cacheMode: useCdn ? 'noStale' : undefined,\n tag: [requestTag, 'fetch-sync-tags'].filter(Boolean).join('.'),\n })\n\n const cacheTags = [...tags, ...(syncTags?.map((tag) => `sanity:${tag}`) || [])]\n\n const {result, resultSourceMap} = await client.fetch(query, await params, {\n filterResponse: false,\n perspective: perspective as ClientPerspective,\n stega,\n token: perspective !== 'published' && serverToken ? serverToken : originalToken,\n next: {revalidate, tags: cacheTags},\n useCdn,\n cacheMode: useCdn ? 'noStale' : undefined,\n tag: requestTag,\n })\n return {data: result, sourceMap: resultSourceMap || null, tags: cacheTags}\n }\n\n const SanityLive: React.ComponentType<DefinedLiveProps> = async function SanityLive(props) {\n const {\n includeAllDocuments = (await draftMode()).isEnabled,\n action = actionUpdateTags,\n restartAction = actionRefresh,\n reconnectAction = actionRefresh,\n goAwayAction,\n welcomeAction,\n refreshOnMount,\n refreshOnFocus,\n refreshOnReconnect,\n requestTag = 'next-loader.live',\n } = props\n const {projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix} =\n client.config()\n const shouldIncludeAllDocuments = typeof browserToken === 'string' && includeAllDocuments\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 const {origin} = new URL(client.getUrl('', false))\n preconnect(origin)\n\n return (\n <SanityLiveClientComponent\n config={{\n projectId,\n dataset,\n apiHost,\n apiVersion,\n useProjectHostname,\n requestTagPrefix,\n token: shouldIncludeAllDocuments ? browserToken : undefined,\n }}\n includeAllDocuments={shouldIncludeAllDocuments}\n action={action}\n reconnectAction={reconnectAction === false ? undefined : reconnectAction}\n restartAction={restartAction === false ? undefined : restartAction}\n welcomeAction={welcomeAction}\n goAwayAction={goAwayAction}\n requestTag={requestTag}\n refreshOnMount={refreshOnMount}\n refreshOnFocus={refreshOnFocus}\n refreshOnReconnect={refreshOnReconnect}\n />\n )\n }\n SanityLive.displayName = 'SanityLiveServerComponent'\n\n return {\n sanityFetch,\n SanityLive,\n }\n}\n\nasync function resolveCookiePerspective(): Promise<Exclude<ClientPerspective, 'raw'>> {\n return (await draftMode()).isEnabled\n ? (await cookies()).has(perspectiveCookieName)\n ? sanitizePerspective((await cookies()).get(perspectiveCookieName)?.value, 'drafts')\n : 'drafts'\n : 'published'\n}\n"],"mappings":";;;;;;;;;AA8EA,SAAgB,WAAW,QAWzB;CACA,MAAM,EACJ,QAAQ,SACR,aACA,cACA,cACA,OAAO,eAAe,SACpB;AAEJ,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,oDAAoD;AAGtE,KAAI,QAAQ,IAAI,aAAa,gBAAgB,CAAC,eAAe,gBAAgB,MAC3E,SAAQ,KACN,8LACD;AAGH,KAAI,QAAQ,IAAI,aAAa,gBAAgB,CAAC,gBAAgB,iBAAiB,MAC7E,SAAQ,KACN,iXACD;CAGH,MAAM,SAAS,QAAQ,WAAW;EAAC,kBAAkB;EAAO,QAAQ;EAAM,CAAC;CAC3E,MAAM,EAAC,OAAO,kBAAiB,OAAO,QAAQ;CAC9C,MAAM,mBAAmB,OAAO,OAAO,QAAQ,CAAC,MAAM,cAAc;CAEpE,MAAM,cAAsC,eAAeA,cAEzD,EACA,OACA,SAAS,EAAE,EACX,OAAO,QACP,OAAO,CAAC,SAAS,EACjB,aAAa,cACb,KACA,aAAa,OAAO,uBASnB;EACD,MAAM,QAAQ,WAAW,gBAAgB,qBAAqB,MAAM,WAAW,EAAE;EACjF,MAAM,cAAc,gBAAiB,MAAM,0BAA0B;EACrE,MAAM,SAAS,gBAAgB;EAC/B,MAAM,aACJ,cAAc,eAAe,KAAA,IACzB,aAAa,aACb,QAAQ,IAAI,aAAa,eACvB,QACA,KAAA;EAGR,MAAM,EAAC,aAAY,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GACzD,gBAAgB;GACH;GACb,OAAO;GACP,aAAa;GACb,MAAM;IAAC;IAAY,MAAM,CAAC,GAAG,MAAM,yBAAyB;IAAC;GAC7D;GACA,WAAW,SAAS,YAAY,KAAA;GAChC,KAAK,CAAC,YAAY,kBAAkB,CAAC,OAAO,QAAQ,CAAC,KAAK,IAAI;GAC/D,CAAC;EAEF,MAAM,YAAY,CAAC,GAAG,MAAM,GAAI,UAAU,KAAK,UAAQ,UAAUC,QAAM,IAAI,EAAE,CAAE;EAE/E,MAAM,EAAC,QAAQ,oBAAmB,MAAM,OAAO,MAAM,OAAO,MAAM,QAAQ;GACxE,gBAAgB;GACH;GACb;GACA,OAAO,gBAAgB,eAAe,cAAc,cAAc;GAClE,MAAM;IAAC;IAAY,MAAM;IAAU;GACnC;GACA,WAAW,SAAS,YAAY,KAAA;GAChC,KAAK;GACN,CAAC;AACF,SAAO;GAAC,MAAM;GAAQ,WAAW,mBAAmB;GAAM,MAAM;GAAU;;CAG5E,MAAMC,eAAoD,eAAeA,aAAW,OAAO;EACzF,MAAM,EACJ,uBAAuB,MAAM,WAAW,EAAE,WAC1C,SAAS,kBACT,gBAAgB,eAChB,kBAAkB,eAClB,cACA,eACA,gBACA,gBACA,oBACA,aAAa,uBACX;EACJ,MAAM,EAAC,WAAW,SAAS,SAAS,YAAY,oBAAoB,qBAClE,OAAO,QAAQ;EACjB,MAAM,4BAA4B,OAAO,iBAAiB,YAAY;EAGtE,MAAM,EAAC,WAAU,IAAI,IAAI,OAAO,OAAO,IAAI,MAAM,CAAC;AAClD,aAAW,OAAO;AAElB,SACE,oBAACC,YAAAA;GACC,QAAQ;IACN;IACA;IACA;IACA;IACA;IACA;IACA,OAAO,4BAA4B,eAAe,KAAA;IACnD;GACD,qBAAqB;GACb;GACR,iBAAiB,oBAAoB,QAAQ,KAAA,IAAY;GACzD,eAAe,kBAAkB,QAAQ,KAAA,IAAY;GACtC;GACD;GACF;GACI;GACA;GACI;IACpB;;AAGN,cAAW,cAAc;AAEzB,QAAO;EACL;EACA,YAAA;EACD;;AAGH,eAAe,2BAAuE;AACpF,SAAQ,MAAM,WAAW,EAAE,aACtB,MAAM,SAAS,EAAE,IAAI,sBAAsB,GAC1C,qBAAqB,MAAM,SAAS,EAAE,IAAI,sBAAsB,EAAE,OAAO,SAAS,GAClF,WACF"}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
import { i as SanityLiveActionContext } from "../../types.js";
|
|
2
|
+
import { LiveEvent } from "@sanity/client";
|
|
1
3
|
/**
|
|
2
|
-
* Used by `<SanityLive
|
|
4
|
+
* Used by `<SanityLive action={actionRevalidateTags} />`
|
|
3
5
|
*/
|
|
4
|
-
declare function
|
|
6
|
+
declare function actionUpdateTags(event: Extract<LiveEvent, {
|
|
7
|
+
type: "message";
|
|
8
|
+
}>, context: SanityLiveActionContext): Promise<void>;
|
|
5
9
|
/**
|
|
6
|
-
* Used by `<SanityLive
|
|
10
|
+
* Used by `<SanityLive reconnectAction={actionRefresh} restartAction={actionRefresh} />`
|
|
7
11
|
*/
|
|
8
|
-
declare function
|
|
9
|
-
export {
|
|
12
|
+
declare function actionRefresh(): Promise<void>;
|
|
13
|
+
export { actionRefresh, actionUpdateTags };
|
|
10
14
|
//# sourceMappingURL=index.default.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.default.d.ts","names":[],"sources":["../../../src/live/server-actions/index.default.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.default.d.ts","names":[],"sources":["../../../src/live/server-actions/index.default.ts"],"sourcesContent":[],"mappings":";;;AAYA;;AACS,iBADa,gBAAA,CACb,KAAA,EAAA,OAAA,CAAQ,SAAR,EAAA;EACE,IAAA,EAAA,SAAA;CACR,CAAA,EAAA,OAAA,EADQ,uBACR,CAAA,EAAA,OAAA,CAAA,IAAA,CAAA;;AAsCH;;iBAAsB,aAAA,CAAA,GAAiB"}
|
|
@@ -1,35 +1,34 @@
|
|
|
1
1
|
"use server";
|
|
2
2
|
import { n as PUBLISHED_SYNC_TAG_PREFIX, t as DRAFT_SYNC_TAG_PREFIX } from "../../constants.js";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { draftMode } from "next/headers";
|
|
4
|
+
import { refresh, revalidateTag, updateTag } from "next/cache";
|
|
5
|
+
async function actionUpdateTags(event, context) {
|
|
6
|
+
if (!Array.isArray(event.tags)) {
|
|
7
|
+
console.warn(`<SanityLive ${context.includeAllDocuments ? "includeAllDocuments " : ""}/> action called with non-array tags`, event);
|
|
7
8
|
return;
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
if (context.includeAllDocuments) {
|
|
11
|
+
if (!(await draftMode()).isEnabled) {
|
|
12
|
+
console.warn("<SanityLive includeAllDocuments /> action called in non-draft mode, ignoring", {
|
|
13
|
+
event,
|
|
14
|
+
context
|
|
15
|
+
});
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const tags = event.tags.map((tag) => `${DRAFT_SYNC_TAG_PREFIX}${tag}`);
|
|
19
|
+
for (const tag of tags) updateTag(tag);
|
|
20
|
+
console.log(`<SanityLive includeAllDocuments /> updated tags: ${tags.join(", ")}`);
|
|
21
|
+
} else {
|
|
22
|
+
revalidateTag("sanity:fetch-sync-tags", "max");
|
|
23
|
+
console.log(`<SanityLive /> revalidated tag: "sanity:fetch-sync-tags" with cache profile "max"`);
|
|
24
|
+
const tags = event.tags.map((tag) => `${PUBLISHED_SYNC_TAG_PREFIX}${tag}`);
|
|
25
|
+
for (const tag of tags) updateTag(tag);
|
|
26
|
+
console.log(`<SanityLive /> updated tags: ${tags.join(", ")}`);
|
|
18
27
|
}
|
|
19
28
|
}
|
|
20
|
-
async function
|
|
21
|
-
|
|
22
|
-
console.warn("<SanityLive /> `expireTags` called with non-array tags", _tags);
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
const tags = _tags.filter((tag) => typeof tag === "string" && tag.startsWith(DRAFT_SYNC_TAG_PREFIX));
|
|
26
|
-
if (!tags.length) {
|
|
27
|
-
console.warn("<SanityLive /> `expireTags` called with no valid tags", _tags);
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
for (const tag of tags) updateTag(tag);
|
|
31
|
-
console.log(`<SanityLive /> updated tags: ${tags.join(", ")}`);
|
|
29
|
+
async function actionRefresh() {
|
|
30
|
+
refresh();
|
|
32
31
|
}
|
|
33
|
-
export {
|
|
32
|
+
export { actionRefresh, actionUpdateTags };
|
|
34
33
|
|
|
35
34
|
//# sourceMappingURL=index.default.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.default.js","names":[],"sources":["../../../src/live/server-actions/index.default.ts"],"sourcesContent":["'use server'\n\nimport {DRAFT_SYNC_TAG_PREFIX, PUBLISHED_SYNC_TAG_PREFIX} from '#live/constants'\nimport {revalidateTag, updateTag} from 'next/cache'\n\n/**\n * Used by `<SanityLive
|
|
1
|
+
{"version":3,"file":"index.default.js","names":[],"sources":["../../../src/live/server-actions/index.default.ts"],"sourcesContent":["'use server'\n\nimport type {SanityLiveActionContext} from '#live/types'\nimport type {LiveEvent} from '@sanity/client'\n\nimport {DRAFT_SYNC_TAG_PREFIX, PUBLISHED_SYNC_TAG_PREFIX} from '#live/constants'\nimport {refresh, revalidateTag, updateTag} from 'next/cache'\nimport {draftMode} from 'next/headers'\n\n/**\n * Used by `<SanityLive action={actionRevalidateTags} />`\n */\nexport async function actionUpdateTags(\n event: Extract<LiveEvent, {type: 'message'}>,\n context: SanityLiveActionContext,\n): Promise<void> {\n if (!Array.isArray(event.tags)) {\n console.warn(\n `<SanityLive ${context.includeAllDocuments ? 'includeAllDocuments ' : ''}/> action called with non-array tags`,\n event,\n )\n return undefined\n }\n if (context.includeAllDocuments) {\n if (!(await draftMode()).isEnabled) {\n console.warn('<SanityLive includeAllDocuments /> action called in non-draft mode, ignoring', {\n event,\n context,\n })\n return undefined\n }\n const tags = event.tags.map((tag) => `${DRAFT_SYNC_TAG_PREFIX}${tag}`)\n for (const tag of tags) {\n updateTag(tag)\n }\n // oxlint-disable-next-line no-console\n console.log(`<SanityLive includeAllDocuments /> updated tags: ${tags.join(', ')}`)\n } else {\n revalidateTag('sanity:fetch-sync-tags', 'max')\n // oxlint-disable-next-line no-console\n console.log(`<SanityLive /> revalidated tag: \"sanity:fetch-sync-tags\" with cache profile \"max\"`)\n const tags = event.tags.map((tag) => `${PUBLISHED_SYNC_TAG_PREFIX}${tag}`)\n for (const tag of tags) {\n updateTag(tag)\n }\n // oxlint-disable-next-line no-console\n console.log(`<SanityLive /> updated tags: ${tags.join(', ')}`)\n }\n}\n\n/**\n * Used by `<SanityLive reconnectAction={actionRefresh} restartAction={actionRefresh} />`\n */\nexport async function actionRefresh(): Promise<void> {\n refresh()\n}\n"],"mappings":";;;;AAYA,eAAsB,iBACpB,OACA,SACe;AACf,KAAI,CAAC,MAAM,QAAQ,MAAM,KAAK,EAAE;AAC9B,UAAQ,KACN,eAAe,QAAQ,sBAAsB,yBAAyB,GAAG,uCACzE,MACD;AACD;;AAEF,KAAI,QAAQ,qBAAqB;AAC/B,MAAI,EAAE,MAAM,WAAW,EAAE,WAAW;AAClC,WAAQ,KAAK,gFAAgF;IAC3F;IACA;IACD,CAAC;AACF;;EAEF,MAAM,OAAO,MAAM,KAAK,KAAK,QAAQ,GAAG,wBAAwB,MAAM;AACtE,OAAK,MAAM,OAAO,KAChB,WAAU,IAAI;AAGhB,UAAQ,IAAI,oDAAoD,KAAK,KAAK,KAAK,GAAG;QAC7E;AACL,gBAAc,0BAA0B,MAAM;AAE9C,UAAQ,IAAI,oFAAoF;EAChG,MAAM,OAAO,MAAM,KAAK,KAAK,QAAQ,GAAG,4BAA4B,MAAM;AAC1E,OAAK,MAAM,OAAO,KAChB,WAAU,IAAI;AAGhB,UAAQ,IAAI,gCAAgC,KAAK,KAAK,KAAK,GAAG;;;AAOlE,eAAsB,gBAA+B;AACnD,UAAS"}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
import { i as SanityLiveActionContext } from "../../types.js";
|
|
2
|
+
import { LiveEvent } from "@sanity/client";
|
|
1
3
|
/**
|
|
2
|
-
* Used by `<SanityLive
|
|
4
|
+
* Used by `<SanityLive action={actionRevalidateTags} />`
|
|
3
5
|
*/
|
|
4
|
-
declare function
|
|
6
|
+
declare function actionUpdateTags(event: Extract<LiveEvent, {
|
|
7
|
+
type: "message";
|
|
8
|
+
}>, context: SanityLiveActionContext): Promise<void>;
|
|
5
9
|
/**
|
|
6
|
-
* Used by `<SanityLive
|
|
10
|
+
* Used by `<SanityLive reconnectAction={actionRefresh} restartAction={actionRefresh} />`
|
|
7
11
|
*/
|
|
8
|
-
declare function
|
|
9
|
-
export {
|
|
12
|
+
declare function actionRefresh(): Promise<void>;
|
|
13
|
+
export { actionRefresh, actionUpdateTags };
|
|
10
14
|
//# sourceMappingURL=index.next-js.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.next-js.d.ts","names":[],"sources":["../../../src/live/server-actions/index.next-js.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.next-js.d.ts","names":[],"sources":["../../../src/live/server-actions/index.next-js.ts"],"sourcesContent":[],"mappings":";;;AAYA;;AACS,iBADa,gBAAA,CACb,KAAA,EAAA,OAAA,CAAQ,SAAR,EAAA;EACE,IAAA,EAAA,SAAA;CACR,CAAA,EAAA,OAAA,EADQ,uBACR,CAAA,EAAA,OAAA,CAAA,IAAA,CAAA;;AAiCH;;iBAAsB,aAAA,CAAA,GAAiB"}
|
|
@@ -2,37 +2,26 @@
|
|
|
2
2
|
import { n as PUBLISHED_SYNC_TAG_PREFIX, t as DRAFT_SYNC_TAG_PREFIX } from "../../constants.js";
|
|
3
3
|
import { draftMode } from "next/headers";
|
|
4
4
|
import { refresh, updateTag } from "next/cache";
|
|
5
|
-
async function
|
|
6
|
-
if (!Array.isArray(
|
|
7
|
-
console.warn(
|
|
5
|
+
async function actionUpdateTags(event, context) {
|
|
6
|
+
if (!Array.isArray(event.tags)) {
|
|
7
|
+
console.warn(`<SanityLive ${context.includeAllDocuments ? "includeAllDocuments " : ""}/> action called with non-array tags`, event);
|
|
8
8
|
return;
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
if ((await draftMode()).isEnabled) {
|
|
11
|
+
console.warn(`<SanityLive ${context.includeAllDocuments ? "includeAllDocuments " : ""}/> action called in draft mode, cache is bypassed in draft mode so the refresh() function is called instead of updateTag()`, {
|
|
12
|
+
event,
|
|
13
|
+
context
|
|
14
|
+
});
|
|
15
|
+
refresh();
|
|
13
16
|
return;
|
|
14
17
|
}
|
|
18
|
+
const tags = event.tags.map((tag) => `${context.includeAllDocuments ? PUBLISHED_SYNC_TAG_PREFIX : DRAFT_SYNC_TAG_PREFIX}${tag}`);
|
|
15
19
|
for (const tag of tags) updateTag(tag);
|
|
16
|
-
console.log(`<SanityLive /> updated tags: ${tags.join(", ")}`);
|
|
20
|
+
console.log(`<SanityLive ${context.includeAllDocuments ? "includeAllDocuments " : ""}/> updated tags: ${tags.join(", ")}`);
|
|
17
21
|
}
|
|
18
|
-
async function
|
|
19
|
-
|
|
20
|
-
if (isEnabled) refresh();
|
|
21
|
-
else return expireDraftTags(tags);
|
|
22
|
+
async function actionRefresh() {
|
|
23
|
+
refresh();
|
|
22
24
|
}
|
|
23
|
-
|
|
24
|
-
if (!Array.isArray(_tags)) {
|
|
25
|
-
console.warn("<SanityLive /> `expireTags` called with non-array tags", _tags);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
const tags = _tags.filter((tag) => typeof tag === "string" && tag.startsWith(DRAFT_SYNC_TAG_PREFIX));
|
|
29
|
-
if (!tags.length) {
|
|
30
|
-
console.warn("<SanityLive /> `expireTags` called with no valid tags", _tags);
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
for (const tag of tags) updateTag(tag);
|
|
34
|
-
console.log(`<SanityLive /> updated tags: ${tags.join(", ")}`);
|
|
35
|
-
}
|
|
36
|
-
export { actionLiveEvent, actionLiveEventIncludingDrafts };
|
|
25
|
+
export { actionRefresh, actionUpdateTags };
|
|
37
26
|
|
|
38
27
|
//# sourceMappingURL=index.next-js.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.next-js.js","names":[],"sources":["../../../src/live/server-actions/index.next-js.ts"],"sourcesContent":["'use server'\n\nimport {DRAFT_SYNC_TAG_PREFIX, PUBLISHED_SYNC_TAG_PREFIX} from '#live/constants'\nimport {refresh, updateTag} from 'next/cache'\nimport {draftMode} from 'next/headers'\n\n/**\n * Used by `<SanityLive
|
|
1
|
+
{"version":3,"file":"index.next-js.js","names":[],"sources":["../../../src/live/server-actions/index.next-js.ts"],"sourcesContent":["'use server'\n\nimport type {SanityLiveActionContext} from '#live/types'\nimport type {LiveEvent} from '@sanity/client'\n\nimport {DRAFT_SYNC_TAG_PREFIX, PUBLISHED_SYNC_TAG_PREFIX} from '#live/constants'\nimport {refresh, updateTag} from 'next/cache'\nimport {draftMode} from 'next/headers'\n\n/**\n * Used by `<SanityLive action={actionRevalidateTags} />`\n */\nexport async function actionUpdateTags(\n event: Extract<LiveEvent, {type: 'message'}>,\n context: SanityLiveActionContext,\n): Promise<void> {\n if (!Array.isArray(event.tags)) {\n console.warn(\n `<SanityLive ${context.includeAllDocuments ? 'includeAllDocuments ' : ''}/> action called with non-array tags`,\n event,\n )\n return undefined\n }\n if ((await draftMode()).isEnabled) {\n console.warn(\n `<SanityLive ${context.includeAllDocuments ? 'includeAllDocuments ' : ''}/> action called in draft mode, cache is bypassed in draft mode so the refresh() function is called instead of updateTag()`,\n {event, context},\n )\n refresh()\n return undefined\n }\n\n const tags = event.tags.map(\n (tag) =>\n `${context.includeAllDocuments ? PUBLISHED_SYNC_TAG_PREFIX : DRAFT_SYNC_TAG_PREFIX}${tag}`,\n )\n for (const tag of tags) {\n updateTag(tag)\n }\n // oxlint-disable-next-line no-console\n console.log(\n `<SanityLive ${context.includeAllDocuments ? 'includeAllDocuments ' : ''}/> updated tags: ${tags.join(', ')}`,\n )\n}\n\n/**\n * Used by `<SanityLive reconnectAction={actionRefresh} restartAction={actionRefresh} />`\n */\nexport async function actionRefresh(): Promise<void> {\n refresh()\n}\n"],"mappings":";;;;AAYA,eAAsB,iBACpB,OACA,SACe;AACf,KAAI,CAAC,MAAM,QAAQ,MAAM,KAAK,EAAE;AAC9B,UAAQ,KACN,eAAe,QAAQ,sBAAsB,yBAAyB,GAAG,uCACzE,MACD;AACD;;AAEF,MAAK,MAAM,WAAW,EAAE,WAAW;AACjC,UAAQ,KACN,eAAe,QAAQ,sBAAsB,yBAAyB,GAAG,6HACzE;GAAC;GAAO;GAAQ,CACjB;AACD,WAAS;AACT;;CAGF,MAAM,OAAO,MAAM,KAAK,KACrB,QACC,GAAG,QAAQ,sBAAsB,4BAA4B,wBAAwB,MACxF;AACD,MAAK,MAAM,OAAO,KAChB,WAAU,IAAI;AAGhB,SAAQ,IACN,eAAe,QAAQ,sBAAsB,yBAAyB,GAAG,mBAAmB,KAAK,KAAK,KAAK,GAC5G;;AAMH,eAAsB,gBAA+B;AACnD,UAAS"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ClientPerspective, LiveEvent, SanityClient } from "next-sanity";
|
|
2
|
+
/**
|
|
3
|
+
* Perspectives supported by Sanity Live.
|
|
4
|
+
* Using the legacy `'raw'` perspective is not supported and leads to undefined behavior.
|
|
5
|
+
*/
|
|
6
|
+
type PerspectiveType = Exclude<ClientPerspective, "raw">;
|
|
7
|
+
/**
|
|
8
|
+
* TODO: docs
|
|
9
|
+
*/
|
|
10
|
+
interface SanityLiveActionContext {
|
|
11
|
+
includeAllDocuments: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface DefinedLiveProps {
|
|
14
|
+
/**
|
|
15
|
+
* TODO: docs, settings this to `true` enables live events for draft content and requires `browserToken` to be set.
|
|
16
|
+
*/
|
|
17
|
+
includeAllDocuments?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* TODO: docs
|
|
20
|
+
*/
|
|
21
|
+
action?: (event: Extract<LiveEvent, {
|
|
22
|
+
type: "message";
|
|
23
|
+
}>, context: SanityLiveActionContext) => Promise<void>;
|
|
24
|
+
welcomeAction?: ((event: Extract<LiveEvent, {
|
|
25
|
+
type: "welcome";
|
|
26
|
+
}>, context: SanityLiveActionContext) => Promise<void>) | false;
|
|
27
|
+
goAwayAction?: ((event: Extract<LiveEvent, {
|
|
28
|
+
type: "goaway";
|
|
29
|
+
}>, context: SanityLiveActionContext) => Promise<number | false>) | false;
|
|
30
|
+
reconnectAction?: ((event: Extract<LiveEvent, {
|
|
31
|
+
type: "reconnect";
|
|
32
|
+
}>, context: SanityLiveActionContext) => Promise<void>) | false;
|
|
33
|
+
restartAction?: ((event: Extract<LiveEvent, {
|
|
34
|
+
type: "restart";
|
|
35
|
+
}>, context: SanityLiveActionContext) => Promise<void>) | false;
|
|
36
|
+
/**
|
|
37
|
+
* Automatic refresh of RSC when the component <SanityLive /> is mounted.
|
|
38
|
+
* @defaultValue `false`
|
|
39
|
+
*/
|
|
40
|
+
refreshOnMount?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Automatically refresh when window gets focused
|
|
43
|
+
* @defaultValue `false`
|
|
44
|
+
*/
|
|
45
|
+
refreshOnFocus?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Automatically refresh when the browser regains a network connection (via navigator.onLine)
|
|
48
|
+
* @defaultValue `false`
|
|
49
|
+
*/
|
|
50
|
+
refreshOnReconnect?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* This request tag is used to identify the request when viewing request logs from your Sanity Content Lake.
|
|
53
|
+
* @see https://www.sanity.io/docs/reference-api-request-tags
|
|
54
|
+
* @defaultValue 'next-loader.live'
|
|
55
|
+
*/
|
|
56
|
+
requestTag?: string;
|
|
57
|
+
}
|
|
58
|
+
interface LiveOptions {
|
|
59
|
+
/**
|
|
60
|
+
* Required for `fetch()` and `<Live>` to work
|
|
61
|
+
*/
|
|
62
|
+
client: SanityClient;
|
|
63
|
+
/**
|
|
64
|
+
* Optional. If provided then the token needs to have permissions to query documents with `drafts.` prefixes in order for `perspective: 'drafts'` to work.
|
|
65
|
+
* This token is never shared with the browser, unless you reuse it in `browserToken`..
|
|
66
|
+
*/
|
|
67
|
+
serverToken?: string | false;
|
|
68
|
+
/**
|
|
69
|
+
* Optional. This token is shared with the browser when `<Live>` is given a `perspective` prop other than `"published"`, and should only have access to query published documents.
|
|
70
|
+
* It is used to setup a `Live Draft Content` EventSource connection, and enables live previewing drafts stand-alone, outside of Presentation Tool.
|
|
71
|
+
*/
|
|
72
|
+
browserToken?: string | false;
|
|
73
|
+
}
|
|
74
|
+
export { SanityLiveActionContext as i, LiveOptions as n, PerspectiveType as r, DefinedLiveProps as t };
|
|
75
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/live/shared/types.ts"],"sourcesContent":[],"mappings":";;AAcA;AAqCA;AASA;AASmB,KAvDP,eAAA,GAAkB,OAuDX,CAvDmB,iBAuDnB,EAAA,KAAA,CAAA;;;;UAlBF,uBAAA;;;UASA,gBAAA;;;;;;;;mBASN,QAAQ;;eACN,4BACN;2BAGQ,QAAQ;;eACN,4BACN;0BAII,QAAQ;;eACN,4BACN;6BAII,QAAQ;;eACN,4BACN;2BAII,QAAQ;;eACN,4BACN;;;;;;;;;;;;;;;;;;;;;;;UA2BM,WAAA;;;;UAIP"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-sanity",
|
|
3
|
-
"version": "13.0.0-cache-components.
|
|
3
|
+
"version": "13.0.0-cache-components.20",
|
|
4
4
|
"description": "Sanity.io toolkit for Next.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"live",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"@sanity/preview-url-secret": "^4.0.2",
|
|
69
69
|
"@sanity/visual-editing": "^5.1.0",
|
|
70
70
|
"dequal": "^2.0.3",
|
|
71
|
-
"groq": "^5.
|
|
71
|
+
"groq": "^5.5.0",
|
|
72
72
|
"history": "^5.3.0"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
@@ -76,17 +76,17 @@
|
|
|
76
76
|
"@sanity/tsconfig": "^2.1.0",
|
|
77
77
|
"@sanity/webhook": "4.0.4",
|
|
78
78
|
"@types/js-yaml": "^4.0.9",
|
|
79
|
-
"@types/node": "^24.10.
|
|
79
|
+
"@types/node": "^24.10.9",
|
|
80
80
|
"@types/react": "^19.2.8",
|
|
81
81
|
"@types/react-dom": "^19.2.3",
|
|
82
82
|
"@vitejs/plugin-react": "^5.1.2",
|
|
83
83
|
"@vitest/coverage-v8": "^4.0.17",
|
|
84
84
|
"js-yaml": "^4.1.1",
|
|
85
|
-
"next": "16.1.1-canary.
|
|
85
|
+
"next": "16.1.1-canary.35",
|
|
86
86
|
"publint": "^0.3.16",
|
|
87
87
|
"react": "^19.2.3",
|
|
88
88
|
"react-dom": "^19.2.3",
|
|
89
|
-
"styled-components": "^6.3.
|
|
89
|
+
"styled-components": "^6.3.8",
|
|
90
90
|
"tsdown": "0.19.0",
|
|
91
91
|
"typescript": "5.9.3",
|
|
92
92
|
"vitest": "^4.0.17",
|
|
@@ -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.5.0",
|
|
101
101
|
"styled-components": "^6.1"
|
|
102
102
|
},
|
|
103
103
|
"browserslist": "extends @sanity/browserslist-config",
|
package/dist/LiveEvents.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { n as PUBLISHED_SYNC_TAG_PREFIX } from "./constants.js";
|
|
2
|
-
import { useRouter } from "next/navigation";
|
|
3
|
-
import { useEffect, useEffectEvent, useState } from "react";
|
|
4
|
-
function LiveEvents(props) {
|
|
5
|
-
const { client, onEvent, intervalOnGoAway = 3e4, requestTag, onError, onGoAway } = props;
|
|
6
|
-
const [longPollingInterval, setLongPollingInterval] = useState(false);
|
|
7
|
-
const router = useRouter();
|
|
8
|
-
const handleLiveEvent = useEffectEvent((event) => {
|
|
9
|
-
if (process.env.NODE_ENV !== "production" && event.type === "welcome") {
|
|
10
|
-
console.info("<SanityLive> is connected and listening for live events to published content");
|
|
11
|
-
setLongPollingInterval(false);
|
|
12
|
-
} else if (event.type === "message") onEvent(event.tags.map((tag) => `${PUBLISHED_SYNC_TAG_PREFIX}${tag}`)).then((result) => {
|
|
13
|
-
if (result === "refresh") router.refresh();
|
|
14
|
-
});
|
|
15
|
-
else if (event.type === "restart" || event.type === "reconnect") router.refresh();
|
|
16
|
-
else if (event.type === "goaway") {
|
|
17
|
-
onGoAway(event, intervalOnGoAway);
|
|
18
|
-
setLongPollingInterval(intervalOnGoAway);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
const subscription = client.live.events({ tag: requestTag }).subscribe({
|
|
23
|
-
next: handleLiveEvent,
|
|
24
|
-
error: onError
|
|
25
|
-
});
|
|
26
|
-
return () => subscription.unsubscribe();
|
|
27
|
-
}, [
|
|
28
|
-
client.live,
|
|
29
|
-
onError,
|
|
30
|
-
requestTag
|
|
31
|
-
]);
|
|
32
|
-
useEffect(() => {
|
|
33
|
-
if (!longPollingInterval) return;
|
|
34
|
-
const interval = setInterval(() => router.refresh(), longPollingInterval);
|
|
35
|
-
return () => clearInterval(interval);
|
|
36
|
-
}, [longPollingInterval, router]);
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
LiveEvents.displayName = "LiveEvents";
|
|
40
|
-
var LiveEvents_default = LiveEvents;
|
|
41
|
-
export { LiveEvents_default as default };
|
|
42
|
-
|
|
43
|
-
//# sourceMappingURL=LiveEvents.js.map
|
package/dist/LiveEvents.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"LiveEvents.js","names":[],"sources":["../src/live/client-components/LiveEvents.tsx"],"sourcesContent":["import type {LiveEvent, LiveEventGoAway, SanityClient, SyncTag} from '@sanity/client'\n\nimport {PUBLISHED_SYNC_TAG_PREFIX} from '#live/constants'\nimport {useRouter} from 'next/navigation'\nimport {useEffect, useState, useEffectEvent} from 'react'\n\ninterface LiveEventsProps {\n client: SanityClient\n onEvent: (tags: string[]) => Promise<void | 'refresh'>\n requestTag: string\n onError: (error: unknown) => void\n intervalOnGoAway: number | false\n onGoAway: (event: LiveEventGoAway, intervalOnGoAway: number | false) => void\n}\n\nfunction LiveEvents(props: LiveEventsProps): null {\n const {client, onEvent, intervalOnGoAway = 30_000, requestTag, onError, onGoAway} = props\n\n const [longPollingInterval, setLongPollingInterval] = useState<number | false>(false)\n\n const router = useRouter()\n const handleLiveEvent = useEffectEvent((event: LiveEvent) => {\n if (process.env.NODE_ENV !== 'production' && event.type === 'welcome') {\n // oxlint-disable-next-line no-console\n console.info('<SanityLive> is connected and listening for live events to published content')\n // Disable long polling when welcome event is received, this is a no-op if long polling is already disabled\n setLongPollingInterval(false)\n } else if (event.type === 'message') {\n void onEvent(\n event.tags.map((tag: SyncTag) => `${PUBLISHED_SYNC_TAG_PREFIX}${tag}` as const),\n ).then((result) => {\n if (result === 'refresh') router.refresh()\n })\n } else if (event.type === 'restart' || event.type === 'reconnect') {\n router.refresh()\n } else if (event.type === 'goaway') {\n onGoAway(event, intervalOnGoAway)\n setLongPollingInterval(intervalOnGoAway)\n }\n })\n useEffect(() => {\n const subscription = client.live.events({tag: requestTag}).subscribe({\n next: handleLiveEvent,\n error: onError,\n })\n return () => subscription.unsubscribe()\n }, [client.live, onError, requestTag])\n\n /**\n * Handle switching to long polling when needed\n */\n useEffect(() => {\n if (!longPollingInterval) return\n const interval = setInterval(() => router.refresh(), longPollingInterval)\n return () => clearInterval(interval)\n }, [longPollingInterval, router])\n\n return null\n}\n\nLiveEvents.displayName = 'LiveEvents'\n\nexport default LiveEvents\n"],"mappings":";;;AAeA,SAAS,WAAW,OAA8B;CAChD,MAAM,EAAC,QAAQ,SAAS,mBAAmB,KAAQ,YAAY,SAAS,aAAY;CAEpF,MAAM,CAAC,qBAAqB,0BAA0B,SAAyB,MAAM;CAErF,MAAM,SAAS,WAAW;CAC1B,MAAM,kBAAkB,gBAAgB,UAAqB;AAC3D,MAAI,QAAQ,IAAI,aAAa,gBAAgB,MAAM,SAAS,WAAW;AAErE,WAAQ,KAAK,+EAA+E;AAE5F,0BAAuB,MAAM;aACpB,MAAM,SAAS,UACnB,SACH,MAAM,KAAK,KAAK,QAAiB,GAAG,4BAA4B,MAAe,CAChF,CAAC,MAAM,WAAW;AACjB,OAAI,WAAW,UAAW,QAAO,SAAS;IAC1C;WACO,MAAM,SAAS,aAAa,MAAM,SAAS,YACpD,QAAO,SAAS;WACP,MAAM,SAAS,UAAU;AAClC,YAAS,OAAO,iBAAiB;AACjC,0BAAuB,iBAAiB;;GAE1C;AACF,iBAAgB;EACd,MAAM,eAAe,OAAO,KAAK,OAAO,EAAC,KAAK,YAAW,CAAC,CAAC,UAAU;GACnE,MAAM;GACN,OAAO;GACR,CAAC;AACF,eAAa,aAAa,aAAa;IACtC;EAAC,OAAO;EAAM;EAAS;EAAW,CAAC;AAKtC,iBAAgB;AACd,MAAI,CAAC,oBAAqB;EAC1B,MAAM,WAAW,kBAAkB,OAAO,SAAS,EAAE,oBAAoB;AACzE,eAAa,cAAc,SAAS;IACnC,CAAC,qBAAqB,OAAO,CAAC;AAEjC,QAAO;;AAGT,WAAW,cAAc;AAEzB,IAAA,qBAAe"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { t as DRAFT_SYNC_TAG_PREFIX } from "./constants.js";
|
|
2
|
-
import { useRouter } from "next/navigation";
|
|
3
|
-
import { useEffect, useEffectEvent, useState } from "react";
|
|
4
|
-
function LiveEventsIncludingDrafts(props) {
|
|
5
|
-
const { client, intervalOnGoAway, onError, onEvent, onGoAway, requestTag } = props;
|
|
6
|
-
const [longPollingInterval, setLongPollingInterval] = useState(false);
|
|
7
|
-
const router = useRouter();
|
|
8
|
-
const handleLiveEvent = useEffectEvent((event) => {
|
|
9
|
-
if (process.env.NODE_ENV !== "production" && event.type === "welcome") {
|
|
10
|
-
console.info("<SanityLive> is connected and listening for live events to all content including drafts and version documents in content releases");
|
|
11
|
-
setLongPollingInterval(false);
|
|
12
|
-
} else if (event.type === "message") onEvent(event.tags.map((tag) => `${DRAFT_SYNC_TAG_PREFIX}${tag}`)).then((result) => {
|
|
13
|
-
if (result === "refresh") router.refresh();
|
|
14
|
-
});
|
|
15
|
-
else if (event.type === "restart" || event.type === "reconnect") router.refresh();
|
|
16
|
-
else if (event.type === "goaway") {
|
|
17
|
-
onGoAway(event, intervalOnGoAway);
|
|
18
|
-
setLongPollingInterval(intervalOnGoAway);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
const subscription = client.live.events({
|
|
23
|
-
includeDrafts: true,
|
|
24
|
-
tag: requestTag
|
|
25
|
-
}).subscribe({
|
|
26
|
-
next: handleLiveEvent,
|
|
27
|
-
error: onError
|
|
28
|
-
});
|
|
29
|
-
return () => subscription.unsubscribe();
|
|
30
|
-
}, [
|
|
31
|
-
client.live,
|
|
32
|
-
onError,
|
|
33
|
-
requestTag
|
|
34
|
-
]);
|
|
35
|
-
useEffect(() => {
|
|
36
|
-
if (!longPollingInterval) return;
|
|
37
|
-
const interval = setInterval(() => router.refresh(), longPollingInterval);
|
|
38
|
-
return () => clearInterval(interval);
|
|
39
|
-
}, [longPollingInterval, router]);
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
LiveEventsIncludingDrafts.displayName = "LiveEventsIncludingDrafts";
|
|
43
|
-
var LiveEventsIncludingDrafts_default = LiveEventsIncludingDrafts;
|
|
44
|
-
export { LiveEventsIncludingDrafts_default as default };
|
|
45
|
-
|
|
46
|
-
//# sourceMappingURL=LiveEventsIncludingDrafts.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"LiveEventsIncludingDrafts.js","names":[],"sources":["../src/live/client-components/LiveEventsIncludingDrafts.tsx"],"sourcesContent":["import type {LiveEvent, LiveEventGoAway, SanityClient, SyncTag} from '@sanity/client'\n\nimport {DRAFT_SYNC_TAG_PREFIX} from '#live/constants'\nimport {useRouter} from 'next/navigation'\nimport {useEffect, useState, useEffectEvent} from 'react'\n\ninterface LiveEventsIncludingDraftsProps {\n client: SanityClient\n onEvent: (tags: string[]) => Promise<void | 'refresh'>\n requestTag: string\n onError: (error: unknown) => void\n intervalOnGoAway: number | false\n onGoAway: (event: LiveEventGoAway, intervalOnGoAway: number | false) => void\n}\n\nfunction LiveEventsIncludingDrafts(props: LiveEventsIncludingDraftsProps): null {\n const {client, intervalOnGoAway, onError, onEvent, onGoAway, requestTag} = props\n\n const [longPollingInterval, setLongPollingInterval] = useState<number | false>(false)\n\n const router = useRouter()\n const handleLiveEvent = useEffectEvent((event: LiveEvent) => {\n if (process.env.NODE_ENV !== 'production' && event.type === 'welcome') {\n // oxlint-disable-next-line no-console\n console.info(\n '<SanityLive> is connected and listening for live events to all content including drafts and version documents in content releases',\n )\n // Disable long polling when welcome event is received, this is a no-op if long polling is already disabled\n setLongPollingInterval(false)\n } else if (event.type === 'message') {\n void onEvent(\n event.tags.map((tag: SyncTag) => `${DRAFT_SYNC_TAG_PREFIX}${tag}` as const),\n ).then((result) => {\n if (result === 'refresh') router.refresh()\n })\n } else if (event.type === 'restart' || event.type === 'reconnect') {\n router.refresh()\n } else if (event.type === 'goaway') {\n onGoAway(event, intervalOnGoAway)\n setLongPollingInterval(intervalOnGoAway)\n }\n })\n useEffect(() => {\n const subscription = client.live.events({includeDrafts: true, tag: requestTag}).subscribe({\n next: handleLiveEvent,\n error: onError,\n })\n return () => subscription.unsubscribe()\n }, [client.live, onError, requestTag])\n\n useEffect(() => {\n if (!longPollingInterval) return\n const interval = setInterval(() => router.refresh(), longPollingInterval)\n return () => clearInterval(interval)\n }, [longPollingInterval, router])\n\n return null\n}\n\nLiveEventsIncludingDrafts.displayName = 'LiveEventsIncludingDrafts'\n\nexport default LiveEventsIncludingDrafts\n"],"mappings":";;;AAeA,SAAS,0BAA0B,OAA6C;CAC9E,MAAM,EAAC,QAAQ,kBAAkB,SAAS,SAAS,UAAU,eAAc;CAE3E,MAAM,CAAC,qBAAqB,0BAA0B,SAAyB,MAAM;CAErF,MAAM,SAAS,WAAW;CAC1B,MAAM,kBAAkB,gBAAgB,UAAqB;AAC3D,MAAI,QAAQ,IAAI,aAAa,gBAAgB,MAAM,SAAS,WAAW;AAErE,WAAQ,KACN,oIACD;AAED,0BAAuB,MAAM;aACpB,MAAM,SAAS,UACnB,SACH,MAAM,KAAK,KAAK,QAAiB,GAAG,wBAAwB,MAAe,CAC5E,CAAC,MAAM,WAAW;AACjB,OAAI,WAAW,UAAW,QAAO,SAAS;IAC1C;WACO,MAAM,SAAS,aAAa,MAAM,SAAS,YACpD,QAAO,SAAS;WACP,MAAM,SAAS,UAAU;AAClC,YAAS,OAAO,iBAAiB;AACjC,0BAAuB,iBAAiB;;GAE1C;AACF,iBAAgB;EACd,MAAM,eAAe,OAAO,KAAK,OAAO;GAAC,eAAe;GAAM,KAAK;GAAW,CAAC,CAAC,UAAU;GACxF,MAAM;GACN,OAAO;GACR,CAAC;AACF,eAAa,aAAa,aAAa;IACtC;EAAC,OAAO;EAAM;EAAS;EAAW,CAAC;AAEtC,iBAAgB;AACd,MAAI,CAAC,oBAAqB;EAC1B,MAAM,WAAW,kBAAkB,OAAO,SAAS,EAAE,oBAAoB;AACzE,eAAa,cAAc,SAAS;IACnC,CAAC,qBAAqB,OAAO,CAAC;AAEjC,QAAO;;AAGT,0BAA0B,cAAc;AAExC,IAAA,oCAAe"}
|