next-sanity 13.0.0-cache-components.35 → 13.0.0-cache-components.37
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/NextStudio.js +17 -0
- package/dist/NextStudio.js.map +1 -1
- package/dist/NextStudioNoScript.js +2 -0
- package/dist/NextStudioNoScript.js.map +1 -1
- package/dist/RefreshOnFocus.js +2 -2
- package/dist/RefreshOnFocus.js.map +1 -1
- package/dist/RefreshOnInterval.js +2 -2
- package/dist/RefreshOnInterval.js.map +1 -1
- package/dist/RefreshOnMount.js +9 -3
- package/dist/RefreshOnMount.js.map +1 -1
- package/dist/RefreshOnReconnect.js +2 -2
- package/dist/RefreshOnReconnect.js.map +1 -1
- package/dist/SanityLive.js +13 -9
- package/dist/SanityLive.js.map +1 -1
- package/dist/VisualEditing.js +41 -0
- package/dist/VisualEditing.js.map +1 -1
- package/dist/constants.js +3 -0
- package/dist/constants.js.map +1 -1
- package/dist/draft-mode/index.js +17 -0
- package/dist/draft-mode/index.js.map +1 -1
- package/dist/image/index.js +6 -0
- package/dist/image/index.js.map +1 -1
- package/dist/isCorsOriginError.js +1 -0
- package/dist/isCorsOriginError.js.map +1 -1
- package/dist/live/cache-life.js +32 -1
- package/dist/live/cache-life.js.map +1 -1
- package/dist/live/client-components/index.d.ts.map +1 -1
- package/dist/live/client-components/index.js +3 -0
- package/dist/live/client-components/index.js.map +1 -1
- package/dist/live/conditions/default/index.js +4 -0
- package/dist/live/conditions/default/index.js.map +1 -1
- package/dist/live/conditions/next-js/index.js +7 -0
- package/dist/live/conditions/next-js/index.js.map +1 -1
- package/dist/live/conditions/react-server/index.js.map +1 -1
- package/dist/live/server-actions/index.default.js +6 -0
- package/dist/live/server-actions/index.default.js.map +1 -1
- package/dist/live/server-actions/index.next-js.js +6 -0
- package/dist/live/server-actions/index.next-js.js.map +1 -1
- package/dist/parseTags.js +21 -0
- package/dist/parseTags.js.map +1 -1
- package/dist/resolvePerspectiveFromCookies.js +4 -0
- package/dist/resolvePerspectiveFromCookies.js.map +1 -1
- package/dist/studio/client-component/index.js +7 -0
- package/dist/studio/client-component/index.js.map +1 -1
- package/dist/studio/index.js +40 -0
- package/dist/studio/index.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/visual-editing/index.js +3 -0
- package/dist/visual-editing/index.js.map +1 -1
- package/dist/visual-editing/server-actions/index.js +6 -0
- package/dist/visual-editing/server-actions/index.js.map +1 -1
- package/dist/webhook/index.js +5 -0
- package/dist/webhook/index.js.map +1 -1
- package/package.json +18 -17
package/dist/NextStudio.js
CHANGED
|
@@ -3,6 +3,7 @@ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
|
3
3
|
import { useMemo, useSyncExternalStore } from "react";
|
|
4
4
|
import { Studio } from "sanity";
|
|
5
5
|
import { createHashHistory } from "history";
|
|
6
|
+
/** @internal */
|
|
6
7
|
function createHashHistoryForStudio() {
|
|
7
8
|
const history = createHashHistory();
|
|
8
9
|
return {
|
|
@@ -40,10 +41,26 @@ function createHashHistoryForStudio() {
|
|
|
40
41
|
}
|
|
41
42
|
};
|
|
42
43
|
}
|
|
44
|
+
/** @internal */
|
|
43
45
|
function useIsMounted() {
|
|
44
46
|
return useSyncExternalStore(emptySubscribe, () => true, () => false);
|
|
45
47
|
}
|
|
46
48
|
const emptySubscribe = () => () => {};
|
|
49
|
+
/**
|
|
50
|
+
* Override how the Studio renders by passing children.
|
|
51
|
+
* This is useful for advanced use cases where you're using StudioProvider and StudioLayout instead of Studio:
|
|
52
|
+
* ```
|
|
53
|
+
* import {StudioProvider, StudioLayout} from 'sanity'
|
|
54
|
+
* import {NextStudio} from 'next-sanity/studio'
|
|
55
|
+
* <NextStudio config={config}>
|
|
56
|
+
* <StudioProvider config={config}>
|
|
57
|
+
* <CustomComponentThatUsesContextFromStudioProvider />
|
|
58
|
+
* <StudioLayout />
|
|
59
|
+
* </StudioProvider>
|
|
60
|
+
* </NextStudio>
|
|
61
|
+
* ```
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
47
64
|
function NextStudioComponent({ children, config, unstable__noScript = true, scheme, history, ...props }) {
|
|
48
65
|
const isMounted = useIsMounted();
|
|
49
66
|
const unstableHistory = useMemo(() => {
|
package/dist/NextStudio.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NextStudio.js","names":[],"sources":["../src/studio/client-component/createHashHistoryForStudio.ts","../src/studio/client-component/useIsMounted.ts","../src/studio/client-component/NextStudio.tsx"],"sourcesContent":["// oxlint-disable unbound-method\nimport {createHashHistory, type History, type Listener} from 'history'\n\n/** @internal */\nexport function createHashHistoryForStudio(): History {\n const history = createHashHistory()\n return {\n get action() {\n return history.action\n },\n get location() {\n return history.location\n },\n get createHref() {\n return history.createHref\n },\n get push() {\n return history.push\n },\n get replace() {\n return history.replace\n },\n get go() {\n return history.go\n },\n get back() {\n return history.back\n },\n get forward() {\n return history.forward\n },\n get block() {\n return history.block\n },\n // Overriding listen to workaround a problem where native history provides history.listen(location => void), but the npm package is history.listen(({action, location}) => void)\n listen(listener: Listener) {\n // return history.listen(({ action, location }) => {\n return history.listen(({location}) => {\n // console.debug('history.listen', action, location)\n // @ts-expect-error -- working around a bug? in studio\n listener(location)\n })\n },\n }\n}\n","import {useSyncExternalStore} from 'react'\n\n/** @internal */\nexport function useIsMounted(): boolean {\n return useSyncExternalStore(\n emptySubscribe,\n () => true,\n () => false,\n )\n}\nconst emptySubscribe = () => () => {}\n","import {useMemo} from 'react'\nimport {Studio, type StudioProps} from 'sanity'\n\nimport {NextStudioLayout} from '../NextStudioLayout'\nimport {NextStudioNoScript} from '../NextStudioNoScript'\nimport {createHashHistoryForStudio} from './createHashHistoryForStudio'\nimport {useIsMounted} from './useIsMounted'\n\n/** @public */\nexport interface NextStudioProps extends StudioProps {\n children?: React.ReactNode\n /**\n * Render the <noscript> tag\n * @defaultValue true\n * @alpha\n */\n unstable__noScript?: boolean\n /**\n * The 'hash' option is new feature that is not yet stable for production, but is available for testing and its API won't change in a breaking way.\n * If 'hash' doesn't work for you, or if you want to use a memory based history, you can use the `unstable_history` prop instead.\n * @alpha\n * @defaultValue 'browser'\n */\n history?: 'browser' | 'hash'\n}\n/**\n * Override how the Studio renders by passing children.\n * This is useful for advanced use cases where you're using StudioProvider and StudioLayout instead of Studio:\n * ```\n * import {StudioProvider, StudioLayout} from 'sanity'\n * import {NextStudio} from 'next-sanity/studio'\n * <NextStudio config={config}>\n * <StudioProvider config={config}>\n * <CustomComponentThatUsesContextFromStudioProvider />\n * <StudioLayout />\n * </StudioProvider>\n * </NextStudio>\n * ```\n * @public\n */\nexport default function NextStudioComponent({\n children,\n config,\n unstable__noScript = true,\n scheme,\n history,\n ...props\n}: NextStudioProps): React.JSX.Element {\n const isMounted = useIsMounted()\n const unstableHistory = useMemo<typeof props.unstable_history>(() => {\n if (props.unstable_history && history) {\n throw new Error('Cannot use both `unstable_history` and `history` props at the same time')\n }\n\n if (isMounted && history === 'hash') {\n return createHashHistoryForStudio()\n }\n return props.unstable_history\n }, [history, isMounted, props.unstable_history])\n\n return (\n <>\n {unstable__noScript && <NextStudioNoScript />}\n <NextStudioLayout>\n {history === 'hash' && !isMounted\n ? null\n : children || (\n <Studio\n config={config}\n scheme={scheme}\n unstable_globalStyles\n {...props}\n unstable_history={unstableHistory}\n />\n )}\n </NextStudioLayout>\n </>\n )\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"NextStudio.js","names":[],"sources":["../src/studio/client-component/createHashHistoryForStudio.ts","../src/studio/client-component/useIsMounted.ts","../src/studio/client-component/NextStudio.tsx"],"sourcesContent":["// oxlint-disable unbound-method\nimport {createHashHistory, type History, type Listener} from 'history'\n\n/** @internal */\nexport function createHashHistoryForStudio(): History {\n const history = createHashHistory()\n return {\n get action() {\n return history.action\n },\n get location() {\n return history.location\n },\n get createHref() {\n return history.createHref\n },\n get push() {\n return history.push\n },\n get replace() {\n return history.replace\n },\n get go() {\n return history.go\n },\n get back() {\n return history.back\n },\n get forward() {\n return history.forward\n },\n get block() {\n return history.block\n },\n // Overriding listen to workaround a problem where native history provides history.listen(location => void), but the npm package is history.listen(({action, location}) => void)\n listen(listener: Listener) {\n // return history.listen(({ action, location }) => {\n return history.listen(({location}) => {\n // console.debug('history.listen', action, location)\n // @ts-expect-error -- working around a bug? in studio\n listener(location)\n })\n },\n }\n}\n","import {useSyncExternalStore} from 'react'\n\n/** @internal */\nexport function useIsMounted(): boolean {\n return useSyncExternalStore(\n emptySubscribe,\n () => true,\n () => false,\n )\n}\nconst emptySubscribe = () => () => {}\n","import {useMemo} from 'react'\nimport {Studio, type StudioProps} from 'sanity'\n\nimport {NextStudioLayout} from '../NextStudioLayout'\nimport {NextStudioNoScript} from '../NextStudioNoScript'\nimport {createHashHistoryForStudio} from './createHashHistoryForStudio'\nimport {useIsMounted} from './useIsMounted'\n\n/** @public */\nexport interface NextStudioProps extends StudioProps {\n children?: React.ReactNode\n /**\n * Render the <noscript> tag\n * @defaultValue true\n * @alpha\n */\n unstable__noScript?: boolean\n /**\n * The 'hash' option is new feature that is not yet stable for production, but is available for testing and its API won't change in a breaking way.\n * If 'hash' doesn't work for you, or if you want to use a memory based history, you can use the `unstable_history` prop instead.\n * @alpha\n * @defaultValue 'browser'\n */\n history?: 'browser' | 'hash'\n}\n/**\n * Override how the Studio renders by passing children.\n * This is useful for advanced use cases where you're using StudioProvider and StudioLayout instead of Studio:\n * ```\n * import {StudioProvider, StudioLayout} from 'sanity'\n * import {NextStudio} from 'next-sanity/studio'\n * <NextStudio config={config}>\n * <StudioProvider config={config}>\n * <CustomComponentThatUsesContextFromStudioProvider />\n * <StudioLayout />\n * </StudioProvider>\n * </NextStudio>\n * ```\n * @public\n */\nexport default function NextStudioComponent({\n children,\n config,\n unstable__noScript = true,\n scheme,\n history,\n ...props\n}: NextStudioProps): React.JSX.Element {\n const isMounted = useIsMounted()\n const unstableHistory = useMemo<typeof props.unstable_history>(() => {\n if (props.unstable_history && history) {\n throw new Error('Cannot use both `unstable_history` and `history` props at the same time')\n }\n\n if (isMounted && history === 'hash') {\n return createHashHistoryForStudio()\n }\n return props.unstable_history\n }, [history, isMounted, props.unstable_history])\n\n return (\n <>\n {unstable__noScript && <NextStudioNoScript />}\n <NextStudioLayout>\n {history === 'hash' && !isMounted\n ? null\n : children || (\n <Studio\n config={config}\n scheme={scheme}\n unstable_globalStyles\n {...props}\n unstable_history={unstableHistory}\n />\n )}\n </NextStudioLayout>\n </>\n )\n}\n"],"mappings":";;;;;;AAIA,SAAgB,6BAAsC;CACpD,MAAM,UAAU,mBAAmB;AACnC,QAAO;EACL,IAAI,SAAS;AACX,UAAO,QAAQ;;EAEjB,IAAI,WAAW;AACb,UAAO,QAAQ;;EAEjB,IAAI,aAAa;AACf,UAAO,QAAQ;;EAEjB,IAAI,OAAO;AACT,UAAO,QAAQ;;EAEjB,IAAI,UAAU;AACZ,UAAO,QAAQ;;EAEjB,IAAI,KAAK;AACP,UAAO,QAAQ;;EAEjB,IAAI,OAAO;AACT,UAAO,QAAQ;;EAEjB,IAAI,UAAU;AACZ,UAAO,QAAQ;;EAEjB,IAAI,QAAQ;AACV,UAAO,QAAQ;;EAGjB,OAAO,UAAoB;AAEzB,UAAO,QAAQ,QAAQ,EAAC,eAAc;AAGpC,aAAS,SAAS;KAClB;;EAEL;;;ACxCH,SAAgB,eAAwB;AACtC,QAAO,qBACL,sBACM,YACA,MACP;;AAEH,MAAM,6BAA6B;;;;;;;;;;;;;;;;AC8BnC,SAAwB,oBAAoB,EAC1C,UACA,QACA,qBAAqB,MACrB,QACA,SACA,GAAG,SACkC;CACrC,MAAM,YAAY,cAAc;CAChC,MAAM,kBAAkB,cAA6C;AACnE,MAAI,MAAM,oBAAoB,QAC5B,OAAM,IAAI,MAAM,0EAA0E;AAG5F,MAAI,aAAa,YAAY,OAC3B,QAAO,4BAA4B;AAErC,SAAO,MAAM;IACZ;EAAC;EAAS;EAAW,MAAM;EAAiB,CAAC;AAEhD,QACE,qBAAA,UAAA,EAAA,UAAA,CACG,sBAAsB,oBAAC,oBAAD,EAAsB,CAAA,EAC7C,oBAAC,kBAAD,EAAA,UACG,YAAY,UAAU,CAAC,YACpB,OACA,YACE,oBAAC,QAAD;EACU;EACA;EACR,uBAAA;EACA,GAAI;EACJ,kBAAkB;EAClB,CAAA,EAES,CAAA,CAClB,EAAA,CAAA"}
|
|
@@ -6,6 +6,7 @@ const style = {
|
|
|
6
6
|
WebkitFontSmoothing: "antialiased",
|
|
7
7
|
overflow: "auto"
|
|
8
8
|
};
|
|
9
|
+
/** @public */
|
|
9
10
|
const NextStudioLayout = ({ children }) => {
|
|
10
11
|
return /* @__PURE__ */ jsx("div", {
|
|
11
12
|
id: "sanity",
|
|
@@ -33,6 +34,7 @@ const styles = {
|
|
|
33
34
|
fontFamily: "helvetica, arial, sans-serif"
|
|
34
35
|
}
|
|
35
36
|
};
|
|
37
|
+
/** @internal */
|
|
36
38
|
const NextStudioNoScript = () => /* @__PURE__ */ jsx("noscript", { children: /* @__PURE__ */ jsx("div", {
|
|
37
39
|
style: styles.outer,
|
|
38
40
|
children: /* @__PURE__ */ jsxs("div", {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NextStudioNoScript.js","names":[],"sources":["../src/studio/NextStudioLayout.tsx","../src/studio/NextStudioNoScript.tsx"],"sourcesContent":["/** @public */\nexport interface NextStudioLayoutProps {\n children: React.ReactNode\n}\n\nconst style = {\n height: '100vh',\n maxHeight: '100dvh',\n overscrollBehavior: 'none',\n WebkitFontSmoothing: 'antialiased',\n overflow: 'auto',\n} satisfies React.CSSProperties\n\n/** @public */\nexport const NextStudioLayout = ({children}: NextStudioLayoutProps): React.JSX.Element => {\n return (\n <div id=\"sanity\" data-ui=\"NextStudioLayout\" style={style}>\n {children}\n </div>\n )\n}\n","const styles: Record<'outer' | 'inner', React.CSSProperties> = {\n outer: {\n position: 'absolute',\n top: 0,\n right: 0,\n left: 0,\n bottom: 0,\n background: '#fff',\n zIndex: 1,\n },\n inner: {\n position: 'absolute',\n top: '50%',\n left: '50%',\n transform: 'translate(-50%, -50%)',\n textAlign: 'center',\n fontFamily: 'helvetica, arial, sans-serif',\n },\n}\n\n/** @internal */\nexport const NextStudioNoScript = (): React.JSX.Element => (\n <noscript>\n <div style={styles.outer}>\n <div style={styles.inner}>\n <h1>JavaScript disabled</h1>\n <p>\n Please <a href=\"https://www.enable-javascript.com/\">enable JavaScript</a> in your browser\n and reload the page to proceed.\n </p>\n </div>\n </div>\n </noscript>\n)\n"],"mappings":";AAKA,MAAM,QAAQ;CACZ,QAAQ;CACR,WAAW;CACX,oBAAoB;CACpB,qBAAqB;CACrB,UAAU;CACX
|
|
1
|
+
{"version":3,"file":"NextStudioNoScript.js","names":[],"sources":["../src/studio/NextStudioLayout.tsx","../src/studio/NextStudioNoScript.tsx"],"sourcesContent":["/** @public */\nexport interface NextStudioLayoutProps {\n children: React.ReactNode\n}\n\nconst style = {\n height: '100vh',\n maxHeight: '100dvh',\n overscrollBehavior: 'none',\n WebkitFontSmoothing: 'antialiased',\n overflow: 'auto',\n} satisfies React.CSSProperties\n\n/** @public */\nexport const NextStudioLayout = ({children}: NextStudioLayoutProps): React.JSX.Element => {\n return (\n <div id=\"sanity\" data-ui=\"NextStudioLayout\" style={style}>\n {children}\n </div>\n )\n}\n","const styles: Record<'outer' | 'inner', React.CSSProperties> = {\n outer: {\n position: 'absolute',\n top: 0,\n right: 0,\n left: 0,\n bottom: 0,\n background: '#fff',\n zIndex: 1,\n },\n inner: {\n position: 'absolute',\n top: '50%',\n left: '50%',\n transform: 'translate(-50%, -50%)',\n textAlign: 'center',\n fontFamily: 'helvetica, arial, sans-serif',\n },\n}\n\n/** @internal */\nexport const NextStudioNoScript = (): React.JSX.Element => (\n <noscript>\n <div style={styles.outer}>\n <div style={styles.inner}>\n <h1>JavaScript disabled</h1>\n <p>\n Please <a href=\"https://www.enable-javascript.com/\">enable JavaScript</a> in your browser\n and reload the page to proceed.\n </p>\n </div>\n </div>\n </noscript>\n)\n"],"mappings":";AAKA,MAAM,QAAQ;CACZ,QAAQ;CACR,WAAW;CACX,oBAAoB;CACpB,qBAAqB;CACrB,UAAU;CACX;;AAGD,MAAa,oBAAoB,EAAC,eAAwD;AACxF,QACE,oBAAC,OAAD;EAAK,IAAG;EAAS,WAAQ;EAA0B;EAChD;EACG,CAAA;;AClBV,MAAM,SAAyD;CAC7D,OAAO;EACL,UAAU;EACV,KAAK;EACL,OAAO;EACP,MAAM;EACN,QAAQ;EACR,YAAY;EACZ,QAAQ;EACT;CACD,OAAO;EACL,UAAU;EACV,KAAK;EACL,MAAM;EACN,WAAW;EACX,WAAW;EACX,YAAY;EACb;CACF;;AAGD,MAAa,2BACX,oBAAC,YAAD,EAAA,UACE,oBAAC,OAAD;CAAK,OAAO,OAAO;WACjB,qBAAC,OAAD;EAAK,OAAO,OAAO;YAAnB,CACE,oBAAC,MAAD,EAAA,UAAI,uBAAwB,CAAA,EAC5B,qBAAC,KAAD,EAAA,UAAA;GAAG;GACM,oBAAC,KAAD;IAAG,MAAK;cAAqC;IAAqB,CAAA;;GAEvE,EAAA,CAAA,CACA;;CACF,CAAA,EACG,CAAA"}
|
package/dist/RefreshOnFocus.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useRouter } from "next/navigation";
|
|
2
|
-
import { useEffect } from "react";
|
|
2
|
+
import { startTransition, useEffect } from "react";
|
|
3
3
|
const focusThrottleInterval = 5e3;
|
|
4
4
|
function RefreshOnFocus() {
|
|
5
5
|
const router = useRouter();
|
|
@@ -9,7 +9,7 @@ function RefreshOnFocus() {
|
|
|
9
9
|
const callback = () => {
|
|
10
10
|
const now = Date.now();
|
|
11
11
|
if (now > nextFocusRevalidatedAt && document.visibilityState !== "hidden") {
|
|
12
|
-
router.refresh();
|
|
12
|
+
startTransition(() => router.refresh());
|
|
13
13
|
nextFocusRevalidatedAt = now + focusThrottleInterval;
|
|
14
14
|
}
|
|
15
15
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RefreshOnFocus.js","names":[],"sources":["../src/live/client-components/RefreshOnFocus.tsx"],"sourcesContent":["import {useRouter} from 'next/navigation'\nimport {useEffect} from 'react'\n\nconst focusThrottleInterval = 5_000\n\nexport default function RefreshOnFocus(): null {\n const router = useRouter()\n\n useEffect(() => {\n const controller = new AbortController()\n let nextFocusRevalidatedAt = 0\n const callback = () => {\n const now = Date.now()\n if (now > nextFocusRevalidatedAt && document.visibilityState !== 'hidden') {\n router.refresh()\n nextFocusRevalidatedAt = now + focusThrottleInterval\n }\n }\n const {signal} = controller\n document.addEventListener('visibilitychange', callback, {passive: true, signal})\n window.addEventListener('focus', callback, {passive: true, signal})\n return () => controller.abort()\n }, [router])\n\n return null\n}\nRefreshOnFocus.displayName = 'RefreshOnFocus'\n"],"mappings":";;AAGA,MAAM,wBAAwB;AAE9B,SAAwB,iBAAuB;CAC7C,MAAM,SAAS,WAAW;AAE1B,iBAAgB;EACd,MAAM,aAAa,IAAI,iBAAiB;EACxC,IAAI,yBAAyB;EAC7B,MAAM,iBAAiB;GACrB,MAAM,MAAM,KAAK,KAAK;AACtB,OAAI,MAAM,0BAA0B,SAAS,oBAAoB,UAAU;AACzE,
|
|
1
|
+
{"version":3,"file":"RefreshOnFocus.js","names":[],"sources":["../src/live/client-components/RefreshOnFocus.tsx"],"sourcesContent":["import {useRouter} from 'next/navigation'\nimport {startTransition, useEffect} from 'react'\n\nconst focusThrottleInterval = 5_000\n\nexport default function RefreshOnFocus(): null {\n const router = useRouter()\n\n useEffect(() => {\n const controller = new AbortController()\n let nextFocusRevalidatedAt = 0\n const callback = () => {\n const now = Date.now()\n if (now > nextFocusRevalidatedAt && document.visibilityState !== 'hidden') {\n startTransition(() => router.refresh())\n nextFocusRevalidatedAt = now + focusThrottleInterval\n }\n }\n const {signal} = controller\n document.addEventListener('visibilitychange', callback, {passive: true, signal})\n window.addEventListener('focus', callback, {passive: true, signal})\n return () => controller.abort()\n }, [router])\n\n return null\n}\nRefreshOnFocus.displayName = 'RefreshOnFocus'\n"],"mappings":";;AAGA,MAAM,wBAAwB;AAE9B,SAAwB,iBAAuB;CAC7C,MAAM,SAAS,WAAW;AAE1B,iBAAgB;EACd,MAAM,aAAa,IAAI,iBAAiB;EACxC,IAAI,yBAAyB;EAC7B,MAAM,iBAAiB;GACrB,MAAM,MAAM,KAAK,KAAK;AACtB,OAAI,MAAM,0BAA0B,SAAS,oBAAoB,UAAU;AACzE,0BAAsB,OAAO,SAAS,CAAC;AACvC,6BAAyB,MAAM;;;EAGnC,MAAM,EAAC,WAAU;AACjB,WAAS,iBAAiB,oBAAoB,UAAU;GAAC,SAAS;GAAM;GAAO,CAAC;AAChF,SAAO,iBAAiB,SAAS,UAAU;GAAC,SAAS;GAAM;GAAO,CAAC;AACnE,eAAa,WAAW,OAAO;IAC9B,CAAC,OAAO,CAAC;AAEZ,QAAO;;AAET,eAAe,cAAc"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useRouter } from "next/navigation";
|
|
2
|
-
import { useEffect } from "react";
|
|
2
|
+
import { startTransition, useEffect } from "react";
|
|
3
3
|
function RefreshOnInterval(props) {
|
|
4
4
|
const router = useRouter();
|
|
5
5
|
useEffect(() => {
|
|
6
|
-
const interval = setInterval(() => router.refresh(), props.interval);
|
|
6
|
+
const interval = setInterval(() => startTransition(() => router.refresh()), props.interval);
|
|
7
7
|
return () => clearInterval(interval);
|
|
8
8
|
}, [router, props.interval]);
|
|
9
9
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RefreshOnInterval.js","names":[],"sources":["../src/live/client-components/RefreshOnInterval.tsx"],"sourcesContent":["import {useRouter} from 'next/navigation'\nimport {useEffect} from 'react'\n\nexport default function RefreshOnInterval(props: {interval: number}): null {\n const router = useRouter()\n\n useEffect(() => {\n const interval = setInterval(() => router.refresh(), props.interval)\n return () => clearInterval(interval)\n }, [router, props.interval])\n\n return null\n}\nRefreshOnInterval.displayName = 'RefreshOnInterval'\n"],"mappings":";;AAGA,SAAwB,kBAAkB,OAAiC;CACzE,MAAM,SAAS,WAAW;AAE1B,iBAAgB;EACd,MAAM,WAAW,kBAAkB,OAAO,SAAS,EAAE,MAAM,SAAS;
|
|
1
|
+
{"version":3,"file":"RefreshOnInterval.js","names":[],"sources":["../src/live/client-components/RefreshOnInterval.tsx"],"sourcesContent":["import {useRouter} from 'next/navigation'\nimport {startTransition, useEffect} from 'react'\n\nexport default function RefreshOnInterval(props: {interval: number}): null {\n const router = useRouter()\n\n useEffect(() => {\n const interval = setInterval(() => startTransition(() => router.refresh()), props.interval)\n return () => clearInterval(interval)\n }, [router, props.interval])\n\n return null\n}\nRefreshOnInterval.displayName = 'RefreshOnInterval'\n"],"mappings":";;AAGA,SAAwB,kBAAkB,OAAiC;CACzE,MAAM,SAAS,WAAW;AAE1B,iBAAgB;EACd,MAAM,WAAW,kBAAkB,sBAAsB,OAAO,SAAS,CAAC,EAAE,MAAM,SAAS;AAC3F,eAAa,cAAc,SAAS;IACnC,CAAC,QAAQ,MAAM,SAAS,CAAC;AAE5B,QAAO;;AAET,kBAAkB,cAAc"}
|
package/dist/RefreshOnMount.js
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { useRouter } from "next/navigation";
|
|
2
2
|
import { startTransition, useEffect, useReducer } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* Handles refreshing the page when the page is mounted,
|
|
5
|
+
* in case the content changes at a high enough frequency that by
|
|
6
|
+
* the time the page started streaming, and the <SanityLive> component sets
|
|
7
|
+
* up the EventSource connection, content might have changed.
|
|
8
|
+
*/
|
|
3
9
|
function RefreshOnMount() {
|
|
4
10
|
const router = useRouter();
|
|
5
11
|
const [mounted, mount] = useReducer(() => true, false);
|
|
6
12
|
useEffect(() => {
|
|
7
|
-
if (!mounted) {
|
|
8
|
-
|
|
13
|
+
if (!mounted) startTransition(() => {
|
|
14
|
+
mount();
|
|
9
15
|
router.refresh();
|
|
10
|
-
}
|
|
16
|
+
});
|
|
11
17
|
}, [mounted, router]);
|
|
12
18
|
return null;
|
|
13
19
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RefreshOnMount.js","names":[],"sources":["../src/live/client-components/RefreshOnMount.tsx"],"sourcesContent":["/**\n * Handles refreshing the page when the page is mounted,\n * in case the content changes at a high enough frequency that by\n * the time the page started streaming, and the <SanityLive> component sets\n * up the EventSource connection, content might have changed.\n */\n\nimport {useRouter} from 'next/navigation'\nimport {useEffect, useReducer, startTransition} from 'react'\n\nexport default function RefreshOnMount(): null {\n const router = useRouter()\n const [mounted, mount] = useReducer(() => true, false)\n\n useEffect(() => {\n if (!mounted) {\n startTransition(() => mount()
|
|
1
|
+
{"version":3,"file":"RefreshOnMount.js","names":[],"sources":["../src/live/client-components/RefreshOnMount.tsx"],"sourcesContent":["/**\n * Handles refreshing the page when the page is mounted,\n * in case the content changes at a high enough frequency that by\n * the time the page started streaming, and the <SanityLive> component sets\n * up the EventSource connection, content might have changed.\n */\n\nimport {useRouter} from 'next/navigation'\nimport {useEffect, useReducer, startTransition} from 'react'\n\nexport default function RefreshOnMount(): null {\n const router = useRouter()\n const [mounted, mount] = useReducer(() => true, false)\n\n useEffect(() => {\n if (!mounted) {\n startTransition(() => {\n mount()\n router.refresh()\n })\n }\n }, [mounted, router])\n\n return null\n}\nRefreshOnMount.displayName = 'RefreshOnMount'\n"],"mappings":";;;;;;;;AAUA,SAAwB,iBAAuB;CAC7C,MAAM,SAAS,WAAW;CAC1B,MAAM,CAAC,SAAS,SAAS,iBAAiB,MAAM,MAAM;AAEtD,iBAAgB;AACd,MAAI,CAAC,QACH,uBAAsB;AACpB,UAAO;AACP,UAAO,SAAS;IAChB;IAEH,CAAC,SAAS,OAAO,CAAC;AAErB,QAAO;;AAET,eAAe,cAAc"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { useRouter } from "next/navigation";
|
|
2
|
-
import { useEffect } from "react";
|
|
2
|
+
import { startTransition, useEffect } from "react";
|
|
3
3
|
function RefreshOnReconnect() {
|
|
4
4
|
const router = useRouter();
|
|
5
5
|
useEffect(() => {
|
|
6
6
|
const controller = new AbortController();
|
|
7
7
|
const { signal } = controller;
|
|
8
|
-
window.addEventListener("online", () => router.refresh(), {
|
|
8
|
+
window.addEventListener("online", () => startTransition(() => router.refresh()), {
|
|
9
9
|
passive: true,
|
|
10
10
|
signal
|
|
11
11
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RefreshOnReconnect.js","names":[],"sources":["../src/live/client-components/RefreshOnReconnect.tsx"],"sourcesContent":["import {useRouter} from 'next/navigation'\nimport {useEffect} from 'react'\n\nexport default function RefreshOnReconnect(): null {\n const router = useRouter()\n\n useEffect(() => {\n const controller = new AbortController()\n const {signal} = controller\n window.addEventListener('online', () => router.refresh(), {passive: true
|
|
1
|
+
{"version":3,"file":"RefreshOnReconnect.js","names":[],"sources":["../src/live/client-components/RefreshOnReconnect.tsx"],"sourcesContent":["import {useRouter} from 'next/navigation'\nimport {startTransition, useEffect} from 'react'\n\nexport default function RefreshOnReconnect(): null {\n const router = useRouter()\n\n useEffect(() => {\n const controller = new AbortController()\n const {signal} = controller\n window.addEventListener('online', () => startTransition(() => router.refresh()), {\n passive: true,\n signal,\n })\n return () => controller.abort()\n }, [router])\n\n return null\n}\nRefreshOnReconnect.displayName = 'RefreshOnReconnect'\n"],"mappings":";;AAGA,SAAwB,qBAA2B;CACjD,MAAM,SAAS,WAAW;AAE1B,iBAAgB;EACd,MAAM,aAAa,IAAI,iBAAiB;EACxC,MAAM,EAAC,WAAU;AACjB,SAAO,iBAAiB,gBAAgB,sBAAsB,OAAO,SAAS,CAAC,EAAE;GAC/E,SAAS;GACT;GACD,CAAC;AACF,eAAa,WAAW,OAAO;IAC9B,CAAC,OAAO,CAAC;AAEZ,QAAO;;AAET,mBAAmB,cAAc"}
|
package/dist/SanityLive.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { t as isCorsOriginError } from "./isCorsOriginError.js";
|
|
2
2
|
import { t as cacheTagPrefixes } from "./constants.js";
|
|
3
|
+
import { useRouter } from "next/navigation";
|
|
3
4
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
5
|
import { createClient } from "@sanity/client";
|
|
5
6
|
import dynamic from "next/dynamic";
|
|
6
|
-
import { useEffect, useEffectEvent, useMemo, useState } from "react";
|
|
7
|
+
import { startTransition, useEffect, useEffectEvent, useMemo, useState } from "react";
|
|
7
8
|
const RefreshOnFocus = dynamic(() => import("./RefreshOnFocus.js"));
|
|
8
9
|
const RefreshOnMount = dynamic(() => import("./RefreshOnMount.js"));
|
|
9
10
|
const RefreshOnInterval = dynamic(() => import("./RefreshOnInterval.js"));
|
|
@@ -38,25 +39,28 @@ function SanityLive(props) {
|
|
|
38
39
|
if (onError) onError(error, actionContext);
|
|
39
40
|
else setError(isCorsOriginError(error) ? new Error(`Sanity Live is unable to connect to the Sanity API as the current origin - ${window.origin} - is not in the list of allowed CORS origins for this Sanity Project.${error.addOriginUrl ? ` Add it here: ${error.addOriginUrl}` : ""}`, { cause: error }) : error);
|
|
40
41
|
});
|
|
42
|
+
const router = useRouter();
|
|
41
43
|
const handleLiveEvent = useEffectEvent((event) => {
|
|
42
44
|
switch (event.type) {
|
|
43
45
|
case "welcome":
|
|
44
|
-
setRefreshOnInterval(false);
|
|
45
|
-
if (onWelcome) onWelcome(event, actionContext);
|
|
46
|
+
startTransition(() => setRefreshOnInterval(false));
|
|
47
|
+
if (onWelcome) startTransition(() => onWelcome(event, actionContext));
|
|
46
48
|
break;
|
|
47
49
|
case "message":
|
|
48
|
-
action(event.tags.map((tag) => `${includeDrafts ? cacheTagPrefixes.drafts : cacheTagPrefixes.published}${tag}`))
|
|
50
|
+
startTransition(() => action(event.tags.map((tag) => `${includeDrafts ? cacheTagPrefixes.drafts : cacheTagPrefixes.published}${tag}`)).then((result) => {
|
|
51
|
+
if (result === "refresh") startTransition(() => router.refresh());
|
|
52
|
+
}));
|
|
49
53
|
break;
|
|
50
54
|
case "restart":
|
|
51
|
-
setRefreshOnInterval(false);
|
|
52
|
-
if (onRestart) onRestart(event, actionContext);
|
|
55
|
+
startTransition(() => setRefreshOnInterval(false));
|
|
56
|
+
if (onRestart) startTransition(() => onRestart(event, actionContext));
|
|
53
57
|
break;
|
|
54
58
|
case "reconnect":
|
|
55
|
-
setRefreshOnInterval(false);
|
|
56
|
-
if (onReconnect) onReconnect(event, actionContext);
|
|
59
|
+
startTransition(() => setRefreshOnInterval(false));
|
|
60
|
+
if (onReconnect) startTransition(() => onReconnect(event, actionContext));
|
|
57
61
|
break;
|
|
58
62
|
case "goaway":
|
|
59
|
-
if (onGoAway) onGoAway(event, actionContext, (interval) => setRefreshOnInterval(interval));
|
|
63
|
+
if (onGoAway) startTransition(() => onGoAway(event, actionContext, (interval) => startTransition(() => setRefreshOnInterval(interval))));
|
|
60
64
|
else if (!onGoAway) handleError(new Error(`Sanity Live connection closed, automatic revalidation is disabled, the server gave this reason: ${event.reason}`, { cause: event }));
|
|
61
65
|
break;
|
|
62
66
|
default:
|
package/dist/SanityLive.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SanityLive.js","names":[],"sources":["../src/live/client-components/SanityLive.tsx"],"sourcesContent":["import {createClient, type InitializedClientConfig, type LiveEvent} from '@sanity/client'\nimport dynamic from 'next/dynamic'\nimport {useEffect, useEffectEvent, useMemo, useState} from 'react'\n\nimport {cacheTagPrefixes} from '#live/constants'\nimport {isCorsOriginError} from '#live/isCorsOriginError'\nimport type {\n SanityLiveAction,\n SanityLiveActionContext,\n SanityLiveOnError,\n SanityLiveOnGoaway,\n SanityLiveOnReconnect,\n SanityLiveOnRestart,\n SanityLiveOnWelcome,\n} from '#live/types'\n\nconst RefreshOnFocus = dynamic(() => import('./RefreshOnFocus'))\nconst RefreshOnMount = dynamic(() => import('./RefreshOnMount'))\nconst RefreshOnInterval = dynamic(() => import('./RefreshOnInterval'))\nconst RefreshOnReconnect = dynamic(() => import('./RefreshOnReconnect'))\n\ninterface SanityClientConfig extends Pick<\n InitializedClientConfig,\n | 'projectId'\n | 'dataset'\n | 'apiHost'\n | 'apiVersion'\n | 'useProjectHostname'\n | 'token'\n | 'requestTagPrefix'\n> {}\n\nexport interface SanityLiveProps {\n config: SanityClientConfig\n includeDrafts?: boolean\n requestTag: string\n\n action: SanityLiveAction\n onError: SanityLiveOnError | false | undefined\n onWelcome: SanityLiveOnWelcome | false | undefined\n onReconnect: SanityLiveOnReconnect | false | undefined\n onRestart: SanityLiveOnRestart | false | undefined\n onGoAway: SanityLiveOnGoaway | false | undefined\n\n refreshOnMount?: boolean\n refreshOnFocus?: boolean\n refreshOnReconnect?: boolean\n}\n\nfunction SanityLive(props: SanityLiveProps): React.JSX.Element | null {\n const {\n config,\n includeDrafts = false,\n action,\n onError,\n onWelcome = handleWelcome,\n onReconnect,\n onRestart,\n onGoAway = handleGoaway,\n refreshOnMount = false,\n refreshOnFocus = false,\n refreshOnReconnect = true,\n requestTag,\n } = props\n const {projectId, dataset, apiHost, apiVersion, useProjectHostname, token, requestTagPrefix} =\n config\n const actionContext = {includeDrafts} satisfies SanityLiveActionContext\n\n const client = useMemo(\n () =>\n createClient({\n projectId,\n dataset,\n apiHost,\n apiVersion,\n useProjectHostname,\n ignoreBrowserTokenWarning: true,\n token,\n useCdn: false,\n requestTagPrefix,\n }),\n [apiHost, apiVersion, dataset, projectId, requestTagPrefix, token, useProjectHostname],\n )\n\n const [refreshOnInterval, setRefreshOnInterval] = useState<number | false>(false)\n\n const [error, setError] = useState<unknown>(null)\n if (error) {\n // Throw during render to bubble up to the nearest <ErrorBoundary>, if `onError` is provided we won't rethrow\n throw error\n }\n const handleError = useEffectEvent((error: unknown) => {\n if (onError) {\n void onError(error, actionContext)\n } else {\n setError(\n isCorsOriginError(error)\n ? new Error(\n `Sanity Live is unable to connect to the Sanity API as the current origin - ${window.origin} - is not in the list of allowed CORS origins for this Sanity Project.${error.addOriginUrl ? ` Add it here: ${error.addOriginUrl}` : ''}`,\n {cause: error},\n )\n : error,\n )\n }\n })\n\n const handleLiveEvent = useEffectEvent((event: LiveEvent) => {\n switch (event.type) {\n case 'welcome': {\n // Disable long polling when welcome event is received, this is a no-op if long polling is already disabled\n setRefreshOnInterval(false)\n\n if (onWelcome) {\n
|
|
1
|
+
{"version":3,"file":"SanityLive.js","names":[],"sources":["../src/live/client-components/SanityLive.tsx"],"sourcesContent":["import {createClient, type InitializedClientConfig, type LiveEvent} from '@sanity/client'\nimport dynamic from 'next/dynamic'\nimport {useRouter} from 'next/navigation'\nimport {startTransition, useEffect, useEffectEvent, useMemo, useState} from 'react'\n\nimport {cacheTagPrefixes} from '#live/constants'\nimport {isCorsOriginError} from '#live/isCorsOriginError'\nimport type {\n SanityLiveAction,\n SanityLiveActionContext,\n SanityLiveOnError,\n SanityLiveOnGoaway,\n SanityLiveOnReconnect,\n SanityLiveOnRestart,\n SanityLiveOnWelcome,\n} from '#live/types'\n\nconst RefreshOnFocus = dynamic(() => import('./RefreshOnFocus'))\nconst RefreshOnMount = dynamic(() => import('./RefreshOnMount'))\nconst RefreshOnInterval = dynamic(() => import('./RefreshOnInterval'))\nconst RefreshOnReconnect = dynamic(() => import('./RefreshOnReconnect'))\n\ninterface SanityClientConfig extends Pick<\n InitializedClientConfig,\n | 'projectId'\n | 'dataset'\n | 'apiHost'\n | 'apiVersion'\n | 'useProjectHostname'\n | 'token'\n | 'requestTagPrefix'\n> {}\n\nexport interface SanityLiveProps {\n config: SanityClientConfig\n includeDrafts?: boolean\n requestTag: string\n\n action: SanityLiveAction\n onError: SanityLiveOnError | false | undefined\n onWelcome: SanityLiveOnWelcome | false | undefined\n onReconnect: SanityLiveOnReconnect | false | undefined\n onRestart: SanityLiveOnRestart | false | undefined\n onGoAway: SanityLiveOnGoaway | false | undefined\n\n refreshOnMount?: boolean\n refreshOnFocus?: boolean\n refreshOnReconnect?: boolean\n}\n\nfunction SanityLive(props: SanityLiveProps): React.JSX.Element | null {\n const {\n config,\n includeDrafts = false,\n action,\n onError,\n onWelcome = handleWelcome,\n onReconnect,\n onRestart,\n onGoAway = handleGoaway,\n refreshOnMount = false,\n refreshOnFocus = false,\n refreshOnReconnect = true,\n requestTag,\n } = props\n const {projectId, dataset, apiHost, apiVersion, useProjectHostname, token, requestTagPrefix} =\n config\n const actionContext = {includeDrafts} satisfies SanityLiveActionContext\n\n const client = useMemo(\n () =>\n createClient({\n projectId,\n dataset,\n apiHost,\n apiVersion,\n useProjectHostname,\n ignoreBrowserTokenWarning: true,\n token,\n useCdn: false,\n requestTagPrefix,\n }),\n [apiHost, apiVersion, dataset, projectId, requestTagPrefix, token, useProjectHostname],\n )\n\n const [refreshOnInterval, setRefreshOnInterval] = useState<number | false>(false)\n\n const [error, setError] = useState<unknown>(null)\n if (error) {\n // Throw during render to bubble up to the nearest <ErrorBoundary>, if `onError` is provided we won't rethrow\n throw error\n }\n const handleError = useEffectEvent((error: unknown) => {\n if (onError) {\n void onError(error, actionContext)\n } else {\n setError(\n isCorsOriginError(error)\n ? new Error(\n `Sanity Live is unable to connect to the Sanity API as the current origin - ${window.origin} - is not in the list of allowed CORS origins for this Sanity Project.${error.addOriginUrl ? ` Add it here: ${error.addOriginUrl}` : ''}`,\n {cause: error},\n )\n : error,\n )\n }\n })\n\n const router = useRouter()\n const handleLiveEvent = useEffectEvent((event: LiveEvent) => {\n switch (event.type) {\n case 'welcome': {\n // Disable long polling when welcome event is received, this is a no-op if long polling is already disabled\n startTransition(() => setRefreshOnInterval(false))\n\n if (onWelcome) {\n startTransition(() => onWelcome(event, actionContext))\n }\n break\n }\n case 'message': {\n startTransition(() =>\n action(\n event.tags.map(\n (tag) =>\n `${includeDrafts ? cacheTagPrefixes.drafts : cacheTagPrefixes.published}${tag}`,\n ),\n ).then((result) => {\n if (result === 'refresh') {\n startTransition(() => router.refresh())\n }\n }),\n )\n break\n }\n case 'restart': {\n // Disable long polling when restart event is received, this is a no-op if long polling is already disabled\n startTransition(() => setRefreshOnInterval(false))\n\n if (onRestart) {\n startTransition(() => onRestart(event, actionContext))\n }\n break\n }\n case 'reconnect': {\n // Disable long polling when reconnect event is received, this is a no-op if long polling is already disabled\n startTransition(() => setRefreshOnInterval(false))\n\n if (onReconnect) {\n startTransition(() => onReconnect(event, actionContext))\n }\n break\n }\n case 'goaway': {\n if (onGoAway) {\n startTransition(() =>\n onGoAway(event, actionContext, (interval) =>\n startTransition(() => setRefreshOnInterval(interval)),\n ),\n )\n } else if (!onGoAway) {\n handleError(\n new Error(\n `Sanity Live connection closed, automatic revalidation is disabled, the server gave this reason: ${event.reason}`,\n {cause: event},\n ),\n )\n }\n break\n }\n default:\n handleError(new Error(`Unknown live event type`, {cause: event}))\n break\n }\n })\n useEffect(() => {\n const subscription = client.live\n .events({includeDrafts, tag: requestTag})\n .subscribe({next: handleLiveEvent, error: handleError})\n return () => subscription.unsubscribe()\n }, [client.live, requestTag, includeDrafts])\n\n return (\n <>\n {refreshOnFocus && <RefreshOnFocus />}\n {refreshOnInterval && Number.isFinite(refreshOnInterval) && refreshOnInterval > 0 && (\n <RefreshOnInterval interval={refreshOnInterval} />\n )}\n {refreshOnMount && <RefreshOnMount />}\n {refreshOnReconnect && <RefreshOnReconnect />}\n </>\n )\n}\n\nSanityLive.displayName = 'SanityLiveClientComponent'\n\nexport default SanityLive\n\nconst handleWelcome: SanityLiveOnWelcome = (_, {includeDrafts}) => {\n // oxlint-disable-next-line no-console\n console.info(\n `<SanityLive${includeDrafts ? ' includeDrafts' : ''}> is connected and listening for live events to ${includeDrafts ? 'all content including drafts and version documents in content releases' : 'published content'}`,\n )\n}\n\nconst handleGoaway: SanityLiveOnGoaway = (event, {includeDrafts}, setLongPollingInterval) => {\n const interval = 30_000\n console.warn(\n `<SanityLive${includeDrafts ? ' includeDrafts' : ''}> connection is closed after receiving a 'goaway' event, the server gave this reason:`,\n event.reason,\n `Content will now be refreshed every ${interval / 1_000} seconds`,\n )\n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B,2BAA2B;CAC3F,MAAM,WAAW;AACjB,SAAQ,KACN,cAAc,gBAAgB,mBAAmB,GAAG,wFACpD,MAAM,QACN,uCAAuC,WAAW,IAAM,UACzD;AACD,wBAAuB,SAAS"}
|
package/dist/VisualEditing.js
CHANGED
|
@@ -3,11 +3,25 @@ import { VisualEditing as VisualEditing$1 } from "@sanity/visual-editing/react";
|
|
|
3
3
|
import { jsx } from "react/jsx-runtime";
|
|
4
4
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
5
5
|
import { actionRefresh } from "next-sanity/visual-editing/server-actions";
|
|
6
|
+
/**
|
|
7
|
+
* From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/path-has-prefix.ts#L10-L17
|
|
8
|
+
* Checks if a given path starts with a given prefix. It ensures it matches
|
|
9
|
+
* exactly without containing extra chars. e.g. prefix /docs should replace
|
|
10
|
+
* for /docs, /docs/, /docs/a but not /docsss
|
|
11
|
+
* @param path The path to check.
|
|
12
|
+
* @param prefix The prefix to check against.
|
|
13
|
+
*/
|
|
6
14
|
function pathHasPrefix(path, prefix) {
|
|
7
15
|
if (typeof path !== "string") return false;
|
|
8
16
|
const { pathname } = parsePath(path);
|
|
9
17
|
return pathname === prefix || pathname.startsWith(`${prefix}/`);
|
|
10
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/parse-path.ts#L6-L22
|
|
21
|
+
* Given a path this function will find the pathname, query and hash and return
|
|
22
|
+
* them. This is useful to parse full paths on the client side.
|
|
23
|
+
* @param path A path to parse e.g. /foo/bar?id=1#hash
|
|
24
|
+
*/
|
|
11
25
|
function parsePath(path) {
|
|
12
26
|
const hashIndex = path.indexOf("#");
|
|
13
27
|
const queryIndex = path.indexOf("?");
|
|
@@ -23,18 +37,37 @@ function parsePath(path) {
|
|
|
23
37
|
hash: ""
|
|
24
38
|
};
|
|
25
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/add-path-prefix.ts#L3C1-L14C2
|
|
42
|
+
* Adds the provided prefix to the given path. It first ensures that the path
|
|
43
|
+
* is indeed starting with a slash.
|
|
44
|
+
*/
|
|
26
45
|
function addPathPrefix(path, prefix) {
|
|
27
46
|
if (!path.startsWith("/") || !prefix) return path;
|
|
28
47
|
if (path === "/" && prefix) return prefix;
|
|
29
48
|
const { pathname, query, hash } = parsePath(path);
|
|
30
49
|
return `${prefix}${pathname}${query}${hash}`;
|
|
31
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/remove-path-prefix.ts#L3-L39
|
|
53
|
+
* Given a path and a prefix it will remove the prefix when it exists in the
|
|
54
|
+
* given path. It ensures it matches exactly without containing extra chars
|
|
55
|
+
* and if the prefix is not there it will be noop.
|
|
56
|
+
*
|
|
57
|
+
* @param path The path to remove the prefix from.
|
|
58
|
+
* @param prefix The prefix to be removed.
|
|
59
|
+
*/
|
|
32
60
|
function removePathPrefix(path, prefix) {
|
|
33
61
|
if (!pathHasPrefix(path, prefix)) return path;
|
|
34
62
|
const withoutPrefix = path.slice(prefix.length);
|
|
35
63
|
if (withoutPrefix.startsWith("/")) return withoutPrefix;
|
|
36
64
|
return `/${withoutPrefix}`;
|
|
37
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* From: https://github.com/vercel/next.js/blob/dfe7fc03e2268e7cb765dce6a89e02c831c922d5/packages/next/src/client/normalize-trailing-slash.ts#L16
|
|
68
|
+
* Normalizes the trailing slash of a path according to the `trailingSlash` option
|
|
69
|
+
* in `next.config.js`.
|
|
70
|
+
*/
|
|
38
71
|
const normalizePathTrailingSlash = (path, trailingSlash) => {
|
|
39
72
|
const { pathname, query, hash } = parsePath(path);
|
|
40
73
|
if (trailingSlash) {
|
|
@@ -43,6 +76,14 @@ const normalizePathTrailingSlash = (path, trailingSlash) => {
|
|
|
43
76
|
}
|
|
44
77
|
return `${removeTrailingSlash(pathname)}${query}${hash}`;
|
|
45
78
|
};
|
|
79
|
+
/**
|
|
80
|
+
* From: https://github.com/vercel/next.js/blob/dfe7fc03e2268e7cb765dce6a89e02c831c922d5/packages/next/src/shared/lib/router/utils/remove-trailing-slash.ts#L8
|
|
81
|
+
* Removes the trailing slash for a given route or page path. Preserves the
|
|
82
|
+
* root page. Examples:
|
|
83
|
+
* - `/foo/bar/` -> `/foo/bar`
|
|
84
|
+
* - `/foo/bar` -> `/foo/bar`
|
|
85
|
+
* - `/` -> `/`
|
|
86
|
+
*/
|
|
46
87
|
function removeTrailingSlash(route) {
|
|
47
88
|
return route.replace(/\/$/, "") || "/";
|
|
48
89
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VisualEditing.js","names":["VisualEditingComponent"],"sources":["../src/visual-editing/client-component/utils.ts","../src/visual-editing/client-component/VisualEditing.tsx"],"sourcesContent":["/**\n * From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/path-has-prefix.ts#L10-L17\n * Checks if a given path starts with a given prefix. It ensures it matches\n * exactly without containing extra chars. e.g. prefix /docs should replace\n * for /docs, /docs/, /docs/a but not /docsss\n * @param path The path to check.\n * @param prefix The prefix to check against.\n */\nfunction pathHasPrefix(path: string, prefix: string): boolean {\n if (typeof path !== 'string') {\n return false\n }\n\n const {pathname} = parsePath(path)\n return pathname === prefix || pathname.startsWith(`${prefix}/`)\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/parse-path.ts#L6-L22\n * Given a path this function will find the pathname, query and hash and return\n * them. This is useful to parse full paths on the client side.\n * @param path A path to parse e.g. /foo/bar?id=1#hash\n */\nfunction parsePath(path: string): {\n pathname: string\n query: string\n hash: string\n} {\n const hashIndex = path.indexOf('#')\n const queryIndex = path.indexOf('?')\n const hasQuery = queryIndex > -1 && (hashIndex < 0 || queryIndex < hashIndex)\n\n if (hasQuery || hashIndex > -1) {\n return {\n pathname: path.substring(0, hasQuery ? queryIndex : hashIndex),\n query: hasQuery ? path.substring(queryIndex, hashIndex > -1 ? hashIndex : undefined) : '',\n hash: hashIndex > -1 ? path.slice(hashIndex) : '',\n }\n }\n\n return {pathname: path, query: '', hash: ''}\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/add-path-prefix.ts#L3C1-L14C2\n * Adds the provided prefix to the given path. It first ensures that the path\n * is indeed starting with a slash.\n */\nexport function addPathPrefix(path: string, prefix?: string): string {\n if (!path.startsWith('/') || !prefix) {\n return path\n }\n // If the path is exactly '/' then return just the prefix\n if (path === '/' && prefix) {\n return prefix\n }\n\n const {pathname, query, hash} = parsePath(path)\n return `${prefix}${pathname}${query}${hash}`\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/remove-path-prefix.ts#L3-L39\n * Given a path and a prefix it will remove the prefix when it exists in the\n * given path. It ensures it matches exactly without containing extra chars\n * and if the prefix is not there it will be noop.\n *\n * @param path The path to remove the prefix from.\n * @param prefix The prefix to be removed.\n */\nexport function removePathPrefix(path: string, prefix: string): string {\n // If the path doesn't start with the prefix we can return it as is. This\n // protects us from situations where the prefix is a substring of the path\n // prefix such as:\n //\n // For prefix: /blog\n //\n // /blog -> true\n // /blog/ -> true\n // /blog/1 -> true\n // /blogging -> false\n // /blogging/ -> false\n // /blogging/1 -> false\n if (!pathHasPrefix(path, prefix)) {\n return path\n }\n\n // Remove the prefix from the path via slicing.\n const withoutPrefix = path.slice(prefix.length)\n\n // If the path without the prefix starts with a `/` we can return it as is.\n if (withoutPrefix.startsWith('/')) {\n return withoutPrefix\n }\n\n // If the path without the prefix doesn't start with a `/` we need to add it\n // back to the path to make sure it's a valid path.\n return `/${withoutPrefix}`\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/dfe7fc03e2268e7cb765dce6a89e02c831c922d5/packages/next/src/client/normalize-trailing-slash.ts#L16\n * Normalizes the trailing slash of a path according to the `trailingSlash` option\n * in `next.config.js`.\n */\nexport const normalizePathTrailingSlash = (path: string, trailingSlash: boolean): string => {\n const {pathname, query, hash} = parsePath(path)\n if (trailingSlash) {\n if (pathname.endsWith('/')) {\n return `${pathname}${query}${hash}`\n }\n return `${pathname}/${query}${hash}`\n }\n\n return `${removeTrailingSlash(pathname)}${query}${hash}`\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/dfe7fc03e2268e7cb765dce6a89e02c831c922d5/packages/next/src/shared/lib/router/utils/remove-trailing-slash.ts#L8\n * Removes the trailing slash for a given route or page path. Preserves the\n * root page. Examples:\n * - `/foo/bar/` -> `/foo/bar`\n * - `/foo/bar` -> `/foo/bar`\n * - `/` -> `/`\n */\nfunction removeTrailingSlash(route: string) {\n return route.replace(/\\/$/, '') || '/'\n}\n","import {\n type HistoryAdapter,\n type HistoryAdapterNavigate,\n type HistoryRefresh,\n VisualEditing as VisualEditingComponent,\n type VisualEditingOptions,\n} from '@sanity/visual-editing/react'\nimport {actionRefresh} from 'next-sanity/visual-editing/server-actions'\nimport {usePathname, useRouter, useSearchParams} from 'next/navigation'\nimport {useCallback, useEffect, useMemo, useRef, useState} from 'react'\n\nimport {addPathPrefix, normalizePathTrailingSlash, removePathPrefix} from './utils'\n\n/**\n * @public\n */\nexport interface VisualEditingProps extends Omit<VisualEditingOptions, 'history'> {\n /**\n * @deprecated The histoy adapter is already implemented\n */\n history?: never\n /**\n * If next.config.ts is configured with a basePath we try to configure it automatically,\n * you can disable this by setting basePath to ''.\n * @example basePath=\"/my-custom-base-path\"\n * @alpha experimental and may change without notice\n * @defaultValue process.env.__NEXT_ROUTER_BASEPATH || ''\n */\n basePath?: string\n /**\n * If next.config.ts is configured with a `trailingSlash` we try to detect it automatically,\n * it can be controlled manually by passing a boolean.\n * @example trailingSlash={true}\n * @alpha experimental and may change without notice\n * @defaultValue Boolean(process.env.__NEXT_TRAILING_SLASH)\n */\n trailingSlash?: boolean\n}\n\nexport default function VisualEditing(props: VisualEditingProps): React.JSX.Element | null {\n const {\n basePath = '',\n plugins,\n components,\n refresh,\n trailingSlash = false,\n zIndex,\n onPerspectiveChange,\n } = props\n\n const router = useRouter()\n const routerRef = useRef(router)\n const [navigate, setNavigate] = useState<HistoryAdapterNavigate | undefined>()\n\n useEffect(() => {\n routerRef.current = router\n }, [router])\n\n const history = useMemo<HistoryAdapter>(\n () => ({\n subscribe: (_navigate) => {\n setNavigate(() => _navigate)\n return () => setNavigate(undefined)\n },\n update: (update) => {\n switch (update.type) {\n case 'push':\n return routerRef.current.push(removePathPrefix(update.url, basePath))\n case 'pop':\n return routerRef.current.back()\n case 'replace':\n return routerRef.current.replace(removePathPrefix(update.url, basePath))\n default:\n throw new Error(`Unknown update type`, {cause: update})\n }\n },\n }),\n [basePath],\n )\n\n const pathname = usePathname()\n const searchParams = useSearchParams()\n useEffect(() => {\n if (navigate) {\n navigate({\n type: 'push',\n url: normalizePathTrailingSlash(\n addPathPrefix(\n `${pathname}${searchParams?.size ? `?${searchParams.toString()}` : ''}`,\n basePath,\n ),\n trailingSlash,\n ),\n })\n }\n }, [basePath, navigate, pathname, searchParams, trailingSlash])\n\n const handleRefresh = useCallback((payload: HistoryRefresh): false | Promise<void> => {\n switch (payload.source) {\n case 'manual':\n return actionRefresh(payload)\n case 'mutation': {\n if (payload.livePreviewEnabled) {\n // oxlint-disable-next-line no-console\n console.debug(\n 'Live preview is setup, mutation is skipped assuming its handled by the live preview',\n )\n return false\n }\n return actionRefresh(payload)\n }\n default:\n throw new Error('Unknown refresh source', {cause: payload})\n }\n }, [])\n\n return (\n <VisualEditingComponent\n plugins={plugins}\n components={components}\n history={history}\n portal\n refresh={refresh ?? handleRefresh}\n onPerspectiveChange={onPerspectiveChange}\n zIndex={zIndex}\n />\n )\n}\n"],"mappings":";;;;;AAQA,SAAS,cAAc,MAAc,QAAyB;AAC5D,KAAI,OAAO,SAAS,SAClB,QAAO;CAGT,MAAM,EAAC,aAAY,UAAU,KAAK;AAClC,QAAO,aAAa,UAAU,SAAS,WAAW,GAAG,OAAO,GAAG;;AASjE,SAAS,UAAU,MAIjB;CACA,MAAM,YAAY,KAAK,QAAQ,IAAI;CACnC,MAAM,aAAa,KAAK,QAAQ,IAAI;CACpC,MAAM,WAAW,aAAa,OAAO,YAAY,KAAK,aAAa;AAEnE,KAAI,YAAY,YAAY,GAC1B,QAAO;EACL,UAAU,KAAK,UAAU,GAAG,WAAW,aAAa,UAAU;EAC9D,OAAO,WAAW,KAAK,UAAU,YAAY,YAAY,KAAK,YAAY,KAAA,EAAU,GAAG;EACvF,MAAM,YAAY,KAAK,KAAK,MAAM,UAAU,GAAG;EAChD;AAGH,QAAO;EAAC,UAAU;EAAM,OAAO;EAAI,MAAM;EAAG;;AAQ9C,SAAgB,cAAc,MAAc,QAAyB;AACnE,KAAI,CAAC,KAAK,WAAW,IAAI,IAAI,CAAC,OAC5B,QAAO;AAGT,KAAI,SAAS,OAAO,OAClB,QAAO;CAGT,MAAM,EAAC,UAAU,OAAO,SAAQ,UAAU,KAAK;AAC/C,QAAO,GAAG,SAAS,WAAW,QAAQ;;AAYxC,SAAgB,iBAAiB,MAAc,QAAwB;AAarE,KAAI,CAAC,cAAc,MAAM,OAAO,CAC9B,QAAO;CAIT,MAAM,gBAAgB,KAAK,MAAM,OAAO,OAAO;AAG/C,KAAI,cAAc,WAAW,IAAI,CAC/B,QAAO;AAKT,QAAO,IAAI;;AAQb,MAAa,8BAA8B,MAAc,kBAAmC;CAC1F,MAAM,EAAC,UA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AQ,QAAQ,QAAhB;GACE,KAAK,SACH,QAAO,cAAc,QAAQ;GAC/B,KAAK;AACH,QAAI,QAAQ,oBAAoB;AAE9B,aAAQ,MACN,sFACD;AACD,YAAO;;AAET,WAAO,cAAc,QAAQ;GAE/B,QACE,OAAM,IAAI,MAAM,0BAA0B,EAAC,OAAO,SAAQ,CAAC;;IAE9D,EAAE,CAAC;AAEN,QACE,oBAACA,iBAAAA;EACU;EACG;EACH;EACT,QAAA;EACA,SAAS,WAAW;EACC;EACb;GACR"}
|
|
1
|
+
{"version":3,"file":"VisualEditing.js","names":["VisualEditingComponent"],"sources":["../src/visual-editing/client-component/utils.ts","../src/visual-editing/client-component/VisualEditing.tsx"],"sourcesContent":["/**\n * From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/path-has-prefix.ts#L10-L17\n * Checks if a given path starts with a given prefix. It ensures it matches\n * exactly without containing extra chars. e.g. prefix /docs should replace\n * for /docs, /docs/, /docs/a but not /docsss\n * @param path The path to check.\n * @param prefix The prefix to check against.\n */\nfunction pathHasPrefix(path: string, prefix: string): boolean {\n if (typeof path !== 'string') {\n return false\n }\n\n const {pathname} = parsePath(path)\n return pathname === prefix || pathname.startsWith(`${prefix}/`)\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/parse-path.ts#L6-L22\n * Given a path this function will find the pathname, query and hash and return\n * them. This is useful to parse full paths on the client side.\n * @param path A path to parse e.g. /foo/bar?id=1#hash\n */\nfunction parsePath(path: string): {\n pathname: string\n query: string\n hash: string\n} {\n const hashIndex = path.indexOf('#')\n const queryIndex = path.indexOf('?')\n const hasQuery = queryIndex > -1 && (hashIndex < 0 || queryIndex < hashIndex)\n\n if (hasQuery || hashIndex > -1) {\n return {\n pathname: path.substring(0, hasQuery ? queryIndex : hashIndex),\n query: hasQuery ? path.substring(queryIndex, hashIndex > -1 ? hashIndex : undefined) : '',\n hash: hashIndex > -1 ? path.slice(hashIndex) : '',\n }\n }\n\n return {pathname: path, query: '', hash: ''}\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/add-path-prefix.ts#L3C1-L14C2\n * Adds the provided prefix to the given path. It first ensures that the path\n * is indeed starting with a slash.\n */\nexport function addPathPrefix(path: string, prefix?: string): string {\n if (!path.startsWith('/') || !prefix) {\n return path\n }\n // If the path is exactly '/' then return just the prefix\n if (path === '/' && prefix) {\n return prefix\n }\n\n const {pathname, query, hash} = parsePath(path)\n return `${prefix}${pathname}${query}${hash}`\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/5469e6427b54ab7e9876d4c85b47f9c3afdc5c1f/packages/next/src/shared/lib/router/utils/remove-path-prefix.ts#L3-L39\n * Given a path and a prefix it will remove the prefix when it exists in the\n * given path. It ensures it matches exactly without containing extra chars\n * and if the prefix is not there it will be noop.\n *\n * @param path The path to remove the prefix from.\n * @param prefix The prefix to be removed.\n */\nexport function removePathPrefix(path: string, prefix: string): string {\n // If the path doesn't start with the prefix we can return it as is. This\n // protects us from situations where the prefix is a substring of the path\n // prefix such as:\n //\n // For prefix: /blog\n //\n // /blog -> true\n // /blog/ -> true\n // /blog/1 -> true\n // /blogging -> false\n // /blogging/ -> false\n // /blogging/1 -> false\n if (!pathHasPrefix(path, prefix)) {\n return path\n }\n\n // Remove the prefix from the path via slicing.\n const withoutPrefix = path.slice(prefix.length)\n\n // If the path without the prefix starts with a `/` we can return it as is.\n if (withoutPrefix.startsWith('/')) {\n return withoutPrefix\n }\n\n // If the path without the prefix doesn't start with a `/` we need to add it\n // back to the path to make sure it's a valid path.\n return `/${withoutPrefix}`\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/dfe7fc03e2268e7cb765dce6a89e02c831c922d5/packages/next/src/client/normalize-trailing-slash.ts#L16\n * Normalizes the trailing slash of a path according to the `trailingSlash` option\n * in `next.config.js`.\n */\nexport const normalizePathTrailingSlash = (path: string, trailingSlash: boolean): string => {\n const {pathname, query, hash} = parsePath(path)\n if (trailingSlash) {\n if (pathname.endsWith('/')) {\n return `${pathname}${query}${hash}`\n }\n return `${pathname}/${query}${hash}`\n }\n\n return `${removeTrailingSlash(pathname)}${query}${hash}`\n}\n\n/**\n * From: https://github.com/vercel/next.js/blob/dfe7fc03e2268e7cb765dce6a89e02c831c922d5/packages/next/src/shared/lib/router/utils/remove-trailing-slash.ts#L8\n * Removes the trailing slash for a given route or page path. Preserves the\n * root page. Examples:\n * - `/foo/bar/` -> `/foo/bar`\n * - `/foo/bar` -> `/foo/bar`\n * - `/` -> `/`\n */\nfunction removeTrailingSlash(route: string) {\n return route.replace(/\\/$/, '') || '/'\n}\n","import {\n type HistoryAdapter,\n type HistoryAdapterNavigate,\n type HistoryRefresh,\n VisualEditing as VisualEditingComponent,\n type VisualEditingOptions,\n} from '@sanity/visual-editing/react'\nimport {actionRefresh} from 'next-sanity/visual-editing/server-actions'\nimport {usePathname, useRouter, useSearchParams} from 'next/navigation'\nimport {useCallback, useEffect, useMemo, useRef, useState} from 'react'\n\nimport {addPathPrefix, normalizePathTrailingSlash, removePathPrefix} from './utils'\n\n/**\n * @public\n */\nexport interface VisualEditingProps extends Omit<VisualEditingOptions, 'history'> {\n /**\n * @deprecated The histoy adapter is already implemented\n */\n history?: never\n /**\n * If next.config.ts is configured with a basePath we try to configure it automatically,\n * you can disable this by setting basePath to ''.\n * @example basePath=\"/my-custom-base-path\"\n * @alpha experimental and may change without notice\n * @defaultValue process.env.__NEXT_ROUTER_BASEPATH || ''\n */\n basePath?: string\n /**\n * If next.config.ts is configured with a `trailingSlash` we try to detect it automatically,\n * it can be controlled manually by passing a boolean.\n * @example trailingSlash={true}\n * @alpha experimental and may change without notice\n * @defaultValue Boolean(process.env.__NEXT_TRAILING_SLASH)\n */\n trailingSlash?: boolean\n}\n\nexport default function VisualEditing(props: VisualEditingProps): React.JSX.Element | null {\n const {\n basePath = '',\n plugins,\n components,\n refresh,\n trailingSlash = false,\n zIndex,\n onPerspectiveChange,\n } = props\n\n const router = useRouter()\n const routerRef = useRef(router)\n const [navigate, setNavigate] = useState<HistoryAdapterNavigate | undefined>()\n\n useEffect(() => {\n routerRef.current = router\n }, [router])\n\n const history = useMemo<HistoryAdapter>(\n () => ({\n subscribe: (_navigate) => {\n setNavigate(() => _navigate)\n return () => setNavigate(undefined)\n },\n update: (update) => {\n switch (update.type) {\n case 'push':\n return routerRef.current.push(removePathPrefix(update.url, basePath))\n case 'pop':\n return routerRef.current.back()\n case 'replace':\n return routerRef.current.replace(removePathPrefix(update.url, basePath))\n default:\n throw new Error(`Unknown update type`, {cause: update})\n }\n },\n }),\n [basePath],\n )\n\n const pathname = usePathname()\n const searchParams = useSearchParams()\n useEffect(() => {\n if (navigate) {\n navigate({\n type: 'push',\n url: normalizePathTrailingSlash(\n addPathPrefix(\n `${pathname}${searchParams?.size ? `?${searchParams.toString()}` : ''}`,\n basePath,\n ),\n trailingSlash,\n ),\n })\n }\n }, [basePath, navigate, pathname, searchParams, trailingSlash])\n\n const handleRefresh = useCallback((payload: HistoryRefresh): false | Promise<void> => {\n switch (payload.source) {\n case 'manual':\n return actionRefresh(payload)\n case 'mutation': {\n if (payload.livePreviewEnabled) {\n // oxlint-disable-next-line no-console\n console.debug(\n 'Live preview is setup, mutation is skipped assuming its handled by the live preview',\n )\n return false\n }\n return actionRefresh(payload)\n }\n default:\n throw new Error('Unknown refresh source', {cause: payload})\n }\n }, [])\n\n return (\n <VisualEditingComponent\n plugins={plugins}\n components={components}\n history={history}\n portal\n refresh={refresh ?? handleRefresh}\n onPerspectiveChange={onPerspectiveChange}\n zIndex={zIndex}\n />\n )\n}\n"],"mappings":";;;;;;;;;;;;;AAQA,SAAS,cAAc,MAAc,QAAyB;AAC5D,KAAI,OAAO,SAAS,SAClB,QAAO;CAGT,MAAM,EAAC,aAAY,UAAU,KA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ACE,oBAACA,iBAAD;EACW;EACG;EACH;EACT,QAAA;EACA,SAAS,WAAW;EACC;EACb;EACR,CAAA"}
|
package/dist/constants.js
CHANGED
package/dist/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","names":[],"sources":["../src/live/shared/constants.ts"],"sourcesContent":["import type {CacheTagPrefixes} from './types'\n\n/**\n * Sanity Live handles on-demand revalidation, so the default 15min time based revalidation is too short\n */\nexport const revalidate = 31_536_000 // 365 days\n\nexport const cacheTagPrefixes = {\n published: 'sanity:',\n drafts: 'sanity-drafts:',\n} satisfies CacheTagPrefixes\n"],"mappings":"AAKA,MAAa,aAAa;AAE1B,MAAa,mBAAmB;CAC9B,WAAW;CACX,QAAQ;CACT"}
|
|
1
|
+
{"version":3,"file":"constants.js","names":[],"sources":["../src/live/shared/constants.ts"],"sourcesContent":["import type {CacheTagPrefixes} from './types'\n\n/**\n * Sanity Live handles on-demand revalidation, so the default 15min time based revalidation is too short\n */\nexport const revalidate = 31_536_000 // 365 days\n\nexport const cacheTagPrefixes = {\n published: 'sanity:',\n drafts: 'sanity-drafts:',\n} satisfies CacheTagPrefixes\n"],"mappings":";;;AAKA,MAAa,aAAa;AAE1B,MAAa,mBAAmB;CAC9B,WAAW;CACX,QAAQ;CACT"}
|
package/dist/draft-mode/index.js
CHANGED
|
@@ -2,6 +2,23 @@ import { validatePreviewUrl } from "@sanity/preview-url-secret";
|
|
|
2
2
|
import { perspectiveCookieName } from "@sanity/preview-url-secret/constants";
|
|
3
3
|
import { cookies, draftMode } from "next/headers";
|
|
4
4
|
import { redirect } from "next/navigation";
|
|
5
|
+
/**
|
|
6
|
+
* Sets up an API route for enabling draft mode, can be paired with the `previewUrl.previewMode.enable` in `sanity/presentation`.
|
|
7
|
+
* Can also be used with `sanity-plugin-iframe-pane`.
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* // src/app/api/draft-mode/enable/route.ts
|
|
11
|
+
*
|
|
12
|
+
* import { defineEnableDraftMode } from "next-sanity/draft-mode";
|
|
13
|
+
* import { client } from "@/sanity/lib/client";
|
|
14
|
+
*
|
|
15
|
+
* export const { GET } = defineEnableDraftMode({
|
|
16
|
+
* client: client.withConfig({ token: process.env.SANITY_API_READ_TOKEN }),
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
5
22
|
function defineEnableDraftMode(options) {
|
|
6
23
|
const { client } = options;
|
|
7
24
|
return { GET: async (request) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/draft-mode/define-enable-draft-mode.ts"],"sourcesContent":["import type {SanityClient} from '@sanity/client'\nimport {validatePreviewUrl} from '@sanity/preview-url-secret'\nimport {perspectiveCookieName} from '@sanity/preview-url-secret/constants'\nimport {cookies, draftMode} from 'next/headers'\nimport {redirect} from 'next/navigation'\n\n/**\n * @public\n */\nexport interface DefineEnableDraftModeOptions {\n client: SanityClient\n /**\n * Force secure cookies in development mode.\n * Enable this when using Next.js --experimental-https flag.\n * This option has no effect in production (cookies are always secure).\n * @defaultValue false\n */\n secureDevMode?: boolean\n}\n\n/**\n * @public\n */\nexport interface EnableDraftMode {\n GET: (request: Request) => Promise<Response>\n}\n\n/**\n * Sets up an API route for enabling draft mode, can be paired with the `previewUrl.previewMode.enable` in `sanity/presentation`.\n * Can also be used with `sanity-plugin-iframe-pane`.\n * @example\n * ```ts\n * // src/app/api/draft-mode/enable/route.ts\n *\n * import { defineEnableDraftMode } from \"next-sanity/draft-mode\";\n * import { client } from \"@/sanity/lib/client\";\n *\n * export const { GET } = defineEnableDraftMode({\n * client: client.withConfig({ token: process.env.SANITY_API_READ_TOKEN }),\n * });\n * ```\n *\n * @public\n */\nexport function defineEnableDraftMode(options: DefineEnableDraftModeOptions): EnableDraftMode {\n const {client} = options\n return {\n GET: async (request: Request) => {\n // @TODO check if already in draft mode at a much earlier stage, and skip validation\n\n const {\n isValid,\n redirectTo = '/',\n studioPreviewPerspective,\n } = await validatePreviewUrl(client, request.url)\n if (!isValid) {\n return new Response('Invalid secret', {status: 401})\n }\n\n const draftModeStore = await draftMode()\n\n // Let's enable draft mode if it's not already enabled\n if (!draftModeStore.isEnabled) {\n draftModeStore.enable()\n }\n\n const isProduction = process.env.NODE_ENV === 'production'\n\n // We can't auto-detect HTTPS in dev due to Next.js limitations,\n // so we need an explicit option\n const isSecure = isProduction || (options.secureDevMode ?? false)\n\n // Override cookie header for draft mode for usage in live-preview\n // https://github.com/vercel/next.js/issues/49927\n const cookieStore = await cookies()\n const cookie = cookieStore.get('__prerender_bypass')!\n cookieStore.set({\n name: '__prerender_bypass',\n value: cookie?.value,\n httpOnly: true,\n path: '/',\n secure: isSecure,\n sameSite: isSecure ? 'none' : 'lax',\n })\n\n if (studioPreviewPerspective) {\n cookieStore.set({\n name: perspectiveCookieName,\n value: studioPreviewPerspective,\n httpOnly: true,\n path: '/',\n secure: isSecure,\n sameSite: isSecure ? 'none' : 'lax',\n })\n }\n\n // the `redirect` function throws, and eventually returns a Promise<Response>. TSC doesn't \"see\" that so we have to tell it\n return redirect(redirectTo) as Promise<Response>\n },\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/draft-mode/define-enable-draft-mode.ts"],"sourcesContent":["import type {SanityClient} from '@sanity/client'\nimport {validatePreviewUrl} from '@sanity/preview-url-secret'\nimport {perspectiveCookieName} from '@sanity/preview-url-secret/constants'\nimport {cookies, draftMode} from 'next/headers'\nimport {redirect} from 'next/navigation'\n\n/**\n * @public\n */\nexport interface DefineEnableDraftModeOptions {\n client: SanityClient\n /**\n * Force secure cookies in development mode.\n * Enable this when using Next.js --experimental-https flag.\n * This option has no effect in production (cookies are always secure).\n * @defaultValue false\n */\n secureDevMode?: boolean\n}\n\n/**\n * @public\n */\nexport interface EnableDraftMode {\n GET: (request: Request) => Promise<Response>\n}\n\n/**\n * Sets up an API route for enabling draft mode, can be paired with the `previewUrl.previewMode.enable` in `sanity/presentation`.\n * Can also be used with `sanity-plugin-iframe-pane`.\n * @example\n * ```ts\n * // src/app/api/draft-mode/enable/route.ts\n *\n * import { defineEnableDraftMode } from \"next-sanity/draft-mode\";\n * import { client } from \"@/sanity/lib/client\";\n *\n * export const { GET } = defineEnableDraftMode({\n * client: client.withConfig({ token: process.env.SANITY_API_READ_TOKEN }),\n * });\n * ```\n *\n * @public\n */\nexport function defineEnableDraftMode(options: DefineEnableDraftModeOptions): EnableDraftMode {\n const {client} = options\n return {\n GET: async (request: Request) => {\n // @TODO check if already in draft mode at a much earlier stage, and skip validation\n\n const {\n isValid,\n redirectTo = '/',\n studioPreviewPerspective,\n } = await validatePreviewUrl(client, request.url)\n if (!isValid) {\n return new Response('Invalid secret', {status: 401})\n }\n\n const draftModeStore = await draftMode()\n\n // Let's enable draft mode if it's not already enabled\n if (!draftModeStore.isEnabled) {\n draftModeStore.enable()\n }\n\n const isProduction = process.env.NODE_ENV === 'production'\n\n // We can't auto-detect HTTPS in dev due to Next.js limitations,\n // so we need an explicit option\n const isSecure = isProduction || (options.secureDevMode ?? false)\n\n // Override cookie header for draft mode for usage in live-preview\n // https://github.com/vercel/next.js/issues/49927\n const cookieStore = await cookies()\n const cookie = cookieStore.get('__prerender_bypass')!\n cookieStore.set({\n name: '__prerender_bypass',\n value: cookie?.value,\n httpOnly: true,\n path: '/',\n secure: isSecure,\n sameSite: isSecure ? 'none' : 'lax',\n })\n\n if (studioPreviewPerspective) {\n cookieStore.set({\n name: perspectiveCookieName,\n value: studioPreviewPerspective,\n httpOnly: true,\n path: '/',\n secure: isSecure,\n sameSite: isSecure ? 'none' : 'lax',\n })\n }\n\n // the `redirect` function throws, and eventually returns a Promise<Response>. TSC doesn't \"see\" that so we have to tell it\n return redirect(redirectTo) as Promise<Response>\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,sBAAsB,SAAwD;CAC5F,MAAM,EAAC,WAAU;AACjB,QAAO,EACL,KAAK,OAAO,YAAqB;EAG/B,MAAM,EACJ,SACA,aAAa,KACb,6BACE,MAAM,mBAAmB,QAAQ,QAAQ,IAAI;AACjD,MAAI,CAAC,QACH,QAAO,IAAI,SAAS,kBAAkB,EAAC,QAAQ,KAAI,CAAC;EAGtD,MAAM,iBAAiB,MAAM,WAAW;AAGxC,MAAI,CAAC,eAAe,UAClB,gBAAe,QAAQ;EAOzB,MAAM,WAJe,QAAQ,IAAI,aAAa,iBAIZ,QAAQ,iBAAiB;EAI3D,MAAM,cAAc,MAAM,SAAS;EACnC,MAAM,SAAS,YAAY,IAAI,qBAAqB;AACpD,cAAY,IAAI;GACd,MAAM;GACN,OAAO,QAAQ;GACf,UAAU;GACV,MAAM;GACN,QAAQ;GACR,UAAU,WAAW,SAAS;GAC/B,CAAC;AAEF,MAAI,yBACF,aAAY,IAAI;GACd,MAAM;GACN,OAAO;GACP,UAAU;GACV,MAAM;GACN,QAAQ;GACR,UAAU,WAAW,SAAS;GAC/B,CAAC;AAIJ,SAAO,SAAS,WAAW;IAE9B"}
|
package/dist/image/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import NextImage from "next/image";
|
|
3
3
|
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
/**
|
|
5
|
+
* @alpha
|
|
6
|
+
*/
|
|
4
7
|
const imageLoader = ({ src, width, quality }) => {
|
|
5
8
|
const url = new URL(src);
|
|
6
9
|
url.searchParams.set("auto", "format");
|
|
@@ -14,6 +17,9 @@ const imageLoader = ({ src, width, quality }) => {
|
|
|
14
17
|
if (quality) url.searchParams.set("q", quality.toString());
|
|
15
18
|
return url.href;
|
|
16
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* @alpha
|
|
22
|
+
*/
|
|
17
23
|
function Image(props) {
|
|
18
24
|
const { loader, src, ...rest } = props;
|
|
19
25
|
if (loader) throw new TypeError("The `loader` prop is not supported on `Image` components. Use `next/image` directly to use a custom loader.");
|
package/dist/image/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/image/imageLoader.ts","../../src/image/Image.tsx"],"sourcesContent":["import type {ImageLoader} from 'next/image'\n\n/**\n * @alpha\n */\nexport const imageLoader: ImageLoader = ({src, width, quality}) => {\n const url = new URL(src)\n url.searchParams.set('auto', 'format')\n if (!url.searchParams.has('fit')) {\n url.searchParams.set('fit', url.searchParams.has('h') ? 'min' : 'max')\n }\n if (url.searchParams.has('h') && url.searchParams.has('w')) {\n const originalHeight = parseInt(url.searchParams.get('h')!, 10)\n const originalWidth = parseInt(url.searchParams.get('w')!, 10)\n url.searchParams.set('h', Math.round((originalHeight / originalWidth) * width).toString())\n }\n url.searchParams.set('w', width.toString())\n if (quality) {\n url.searchParams.set('q', quality.toString())\n }\n return url.href\n}\n","import NextImage, {type ImageProps as NextImageProps} from 'next/image'\n\nimport {imageLoader} from './imageLoader'\n\n/**\n * @alpha\n */\nexport interface ImageProps extends Omit<NextImageProps, 'loader' | 'src'> {\n /**\n * The `loader` prop is not supported on `Image` components. Use `next/image` directly to use a custom loader.\n */\n loader?: never\n /**\n * Must be a string that is a valid URL to an image on the Sanity Image CDN.\n */\n src: string\n}\n\n/**\n * @alpha\n */\nexport function Image(props: ImageProps): React.JSX.Element {\n const {loader, src, ...rest} = props\n if (loader) {\n throw new TypeError(\n 'The `loader` prop is not supported on `Image` components. Use `next/image` directly to use a custom loader.',\n )\n }\n let srcUrl: URL\n try {\n srcUrl = new URL(src)\n if (props.height) {\n srcUrl.searchParams.set('h', `${props.height}`)\n }\n if (props.width) {\n srcUrl.searchParams.set('w', `${props.width}`)\n }\n } catch (err) {\n throw new TypeError('The `src` prop must be a valid URL to an image on the Sanity Image CDN.', {\n cause: err,\n })\n }\n return <NextImage {...rest} src={srcUrl.toString()} loader={imageLoader} />\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/image/imageLoader.ts","../../src/image/Image.tsx"],"sourcesContent":["import type {ImageLoader} from 'next/image'\n\n/**\n * @alpha\n */\nexport const imageLoader: ImageLoader = ({src, width, quality}) => {\n const url = new URL(src)\n url.searchParams.set('auto', 'format')\n if (!url.searchParams.has('fit')) {\n url.searchParams.set('fit', url.searchParams.has('h') ? 'min' : 'max')\n }\n if (url.searchParams.has('h') && url.searchParams.has('w')) {\n const originalHeight = parseInt(url.searchParams.get('h')!, 10)\n const originalWidth = parseInt(url.searchParams.get('w')!, 10)\n url.searchParams.set('h', Math.round((originalHeight / originalWidth) * width).toString())\n }\n url.searchParams.set('w', width.toString())\n if (quality) {\n url.searchParams.set('q', quality.toString())\n }\n return url.href\n}\n","import NextImage, {type ImageProps as NextImageProps} from 'next/image'\n\nimport {imageLoader} from './imageLoader'\n\n/**\n * @alpha\n */\nexport interface ImageProps extends Omit<NextImageProps, 'loader' | 'src'> {\n /**\n * The `loader` prop is not supported on `Image` components. Use `next/image` directly to use a custom loader.\n */\n loader?: never\n /**\n * Must be a string that is a valid URL to an image on the Sanity Image CDN.\n */\n src: string\n}\n\n/**\n * @alpha\n */\nexport function Image(props: ImageProps): React.JSX.Element {\n const {loader, src, ...rest} = props\n if (loader) {\n throw new TypeError(\n 'The `loader` prop is not supported on `Image` components. Use `next/image` directly to use a custom loader.',\n )\n }\n let srcUrl: URL\n try {\n srcUrl = new URL(src)\n if (props.height) {\n srcUrl.searchParams.set('h', `${props.height}`)\n }\n if (props.width) {\n srcUrl.searchParams.set('w', `${props.width}`)\n }\n } catch (err) {\n throw new TypeError('The `src` prop must be a valid URL to an image on the Sanity Image CDN.', {\n cause: err,\n })\n }\n return <NextImage {...rest} src={srcUrl.toString()} loader={imageLoader} />\n}\n"],"mappings":";;;;;;AAKA,MAAa,eAA4B,EAAC,KAAK,OAAO,cAAa;CACjE,MAAM,MAAM,IAAI,IAAI,IAAI;AACxB,KAAI,aAAa,IAAI,QAAQ,SAAS;AACtC,KAAI,CAAC,IAAI,aAAa,IAAI,MAAM,CAC9B,KAAI,aAAa,IAAI,OAAO,IAAI,aAAa,IAAI,IAAI,GAAG,QAAQ,MAAM;AAExE,KAAI,IAAI,aAAa,IAAI,IAAI,IAAI,IAAI,aAAa,IAAI,IAAI,EAAE;EAC1D,MAAM,iBAAiB,SAAS,IAAI,aAAa,IAAI,IAAI,EAAG,GAAG;EAC/D,MAAM,gBAAgB,SAAS,IAAI,aAAa,IAAI,IAAI,EAAG,GAAG;AAC9D,MAAI,aAAa,IAAI,KAAK,KAAK,MAAO,iBAAiB,gBAAiB,MAAM,CAAC,UAAU,CAAC;;AAE5F,KAAI,aAAa,IAAI,KAAK,MAAM,UAAU,CAAC;AAC3C,KAAI,QACF,KAAI,aAAa,IAAI,KAAK,QAAQ,UAAU,CAAC;AAE/C,QAAO,IAAI;;;;;ACCb,SAAgB,MAAM,OAAsC;CAC1D,MAAM,EAAC,QAAQ,KAAK,GAAG,SAAQ;AAC/B,KAAI,OACF,OAAM,IAAI,UACR,8GACD;CAEH,IAAI;AACJ,KAAI;AACF,WAAS,IAAI,IAAI,IAAI;AACrB,MAAI,MAAM,OACR,QAAO,aAAa,IAAI,KAAK,GAAG,MAAM,SAAS;AAEjD,MAAI,MAAM,MACR,QAAO,aAAa,IAAI,KAAK,GAAG,MAAM,QAAQ;UAEzC,KAAK;AACZ,QAAM,IAAI,UAAU,2EAA2E,EAC7F,OAAO,KACR,CAAC;;AAEJ,QAAO,oBAAC,WAAD;EAAW,GAAI;EAAM,KAAK,OAAO,UAAU;EAAE,QAAQ;EAAe,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isCorsOriginError.js","names":[],"sources":["../src/live/shared/isCorsOriginError.ts"],"sourcesContent":["import type {CorsOriginError} from '@sanity/client'\n\n/** @public */\nexport function isCorsOriginError(error: unknown): error is CorsOriginError {\n return error instanceof Error && error.name === 'CorsOriginError'\n}\n\nexport type {CorsOriginError}\n"],"mappings":"AAGA,SAAgB,kBAAkB,OAA0C;AAC1E,QAAO,iBAAiB,SAAS,MAAM,SAAS"}
|
|
1
|
+
{"version":3,"file":"isCorsOriginError.js","names":[],"sources":["../src/live/shared/isCorsOriginError.ts"],"sourcesContent":["import type {CorsOriginError} from '@sanity/client'\n\n/** @public */\nexport function isCorsOriginError(error: unknown): error is CorsOriginError {\n return error instanceof Error && error.name === 'CorsOriginError'\n}\n\nexport type {CorsOriginError}\n"],"mappings":";AAGA,SAAgB,kBAAkB,OAA0C;AAC1E,QAAO,iBAAiB,SAAS,MAAM,SAAS"}
|
package/dist/live/cache-life.js
CHANGED
|
@@ -1,4 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* For usage with `cacheComponents: true`, and `defineLive`:
|
|
3
|
+
* ```ts
|
|
4
|
+
* // next.config.ts
|
|
5
|
+
*
|
|
6
|
+
* import type {NextConfig} from 'next'
|
|
7
|
+
* import {sanity} from 'next-sanity/cache-life'
|
|
8
|
+
*
|
|
9
|
+
* const nextConfig: NextConfig = {
|
|
10
|
+
* cacheComponents: true,
|
|
11
|
+
* cacheLife: {
|
|
12
|
+
* sanity
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* export default nextConfig
|
|
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
|
+
*/
|
|
28
|
+
const sanity = {
|
|
29
|
+
/**
|
|
30
|
+
* Sanity Live handles on-demand revalidation, so the default 15min time based revalidation is too short
|
|
31
|
+
*/
|
|
32
|
+
revalidate: 31536e3 };
|
|
2
33
|
export { sanity };
|
|
3
34
|
|
|
4
35
|
//# sourceMappingURL=cache-life.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-life.js","names":[],"sources":["../../src/live/cache-life.ts"],"sourcesContent":["import {revalidate} from '#live/constants'\n\n/**\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/cache-life'\n *\n * const nextConfig: NextConfig = {\n * cacheComponents: true,\n * cacheLife: {\n * sanity\n * }\n * }\n *\n * export default nextConfig\n * ```\n *\n * ```ts\n *\n * async function sanityFetch() {\n * 'use cache'\n * cacheLife('sanity')\n * const {data} = await fetch({query, params})\n * return data\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?: typeof revalidate\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":"AA6BA,MAAa,SAAS,
|
|
1
|
+
{"version":3,"file":"cache-life.js","names":[],"sources":["../../src/live/cache-life.ts"],"sourcesContent":["import {revalidate} from '#live/constants'\n\n/**\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/cache-life'\n *\n * const nextConfig: NextConfig = {\n * cacheComponents: true,\n * cacheLife: {\n * sanity\n * }\n * }\n *\n * export default nextConfig\n * ```\n *\n * ```ts\n *\n * async function sanityFetch() {\n * 'use cache'\n * cacheLife('sanity')\n * const {data} = await fetch({query, params})\n * return data\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?: typeof revalidate\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,SAAS;;;;AAIpB,YAAY,SACb"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../../src/live/client-components/SanityLive.tsx","../../../src/live/client-components/index.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../../src/live/client-components/SanityLive.tsx","../../../src/live/client-components/index.ts"],"mappings":";;UAsBU,kBAAA,SAA2B,IAAA,CACnC,uBAAA;AAAA,UAUe,eAAA;EACf,MAAA,EAAQ,kBAAA;EACR,aAAA;EACA,UAAA;EAEA,MAAA,EAAQ,gBAAA;EACR,OAAA,EAAS,iBAAA;EACT,SAAA,EAAW,mBAAA;EACX,WAAA,EAAa,qBAAA;EACb,SAAA,EAAW,mBAAA;EACX,QAAA,EAAU,kBAAA;EAEV,cAAA;EACA,cAAA;EACA,kBAAA;AAAA;;;AAhCK;cCPM,UAAA,EAAY,KAAA,CAAM,aAAA,CAAc,eAAA"}
|