next-sanity 9.2.0 → 9.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.create,Object.defineProperty,Object.getOwnPropertyDescriptor,Object.getOwnPropertyNames,Object.getPrototypeOf,Object.prototype.hasOwnProperty;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@sanity/client"),t=require("react/jsx-runtime"),r=require("react"),n=require("@portabletext/react"),a=require("groq"),i=require("@sanity/visual-editing/create-data-attribute");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=o(a);const u=r.lazy((()=>import("next-sanity/visual-editing/client-component")));Object.defineProperty(exports,"createClient",{enumerable:!0,get:function(){return e.createClient}}),Object.defineProperty(exports,"unstable__adapter",{enumerable:!0,get:function(){return e.unstable__adapter}}),Object.defineProperty(exports,"unstable__environment",{enumerable:!0,get:function(){return e.unstable__environment}}),Object.defineProperty(exports,"groq",{enumerable:!0,get:function(){return c.default}}),Object.defineProperty(exports,"createDataAttribute",{enumerable:!0,get:function(){return i.createDataAttribute}}),exports.VisualEditing=function(e){var n;let a;if("string"!=typeof e.basePath)try{a=process.env.__NEXT_ROUTER_BASEPATH,a&&console.log(`Detected next basePath as ${JSON.stringify(a)} by reading "process.env.__NEXT_ROUTER_BASEPATH". If this is incorrect then you can set it manually with the basePath prop on the <VisualEditing /> component.`)}catch(e){console.error("Failed detecting basePath",e)}return t.jsx(r.Suspense,{fallback:null,children:t.jsx(u,{...e,basePath:null!=(n=e.basePath)?n:a})})},Object.keys(n).forEach((function(e){"default"!==e&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:function(){return n[e]}})}));//# sourceMappingURL=index.cjs.map
1
+ "use strict";Object.create,Object.defineProperty,Object.getOwnPropertyDescriptor,Object.getOwnPropertyNames,Object.getPrototypeOf,Object.prototype.hasOwnProperty;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@sanity/client"),t=require("react/jsx-runtime"),r=require("react"),n=require("@portabletext/react"),a=require("groq"),i=require("@sanity/visual-editing/create-data-attribute");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=o(a);const c=r.lazy((()=>import("next-sanity/visual-editing/client-component")));Object.defineProperty(exports,"createClient",{enumerable:!0,get:function(){return e.createClient}}),Object.defineProperty(exports,"unstable__adapter",{enumerable:!0,get:function(){return e.unstable__adapter}}),Object.defineProperty(exports,"unstable__environment",{enumerable:!0,get:function(){return e.unstable__environment}}),Object.defineProperty(exports,"groq",{enumerable:!0,get:function(){return s.default}}),Object.defineProperty(exports,"createDataAttribute",{enumerable:!0,get:function(){return i.createDataAttribute}}),exports.VisualEditing=function(e){var n,a;let i,o;if("string"!=typeof e.basePath)try{i=process.env.__NEXT_ROUTER_BASEPATH,i&&console.log(`Detected next basePath as ${JSON.stringify(i)} by reading "process.env.__NEXT_ROUTER_BASEPATH". If this is incorrect then you can set it manually with the basePath prop on the <VisualEditing /> component.`)}catch(e){console.error("Failed detecting basePath",e)}if("boolean"!=typeof e.trailingSlash)try{o=!!process.env.__NEXT_TRAILING_SLASH,o&&console.log(`Detected next trailingSlash as ${JSON.stringify(o)} by reading "process.env.__NEXT_TRAILING_SLASH". If this is incorrect then you can set it manually with the trailingSlash prop on the <VisualEditing /> component.`)}catch(e){console.error("Failed detecting trailingSlash",e)}return t.jsx(r.Suspense,{fallback:null,children:t.jsx(c,{...e,basePath:null!=(n=e.basePath)?n:i,trailingSlash:null!=(a=e.trailingSlash)?a:o})})},Object.keys(n).forEach((function(e){"default"!==e&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:function(){return n[e]}})}));//# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/visual-editing/index.tsx"],"sourcesContent":["/* eslint-disable dot-notation */\nimport type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'\nimport {lazy, Suspense} from 'react'\n\nconst VisualEditingComponent = lazy(() => import('next-sanity/visual-editing/client-component'))\n\n/**\n * @public\n */\nexport function VisualEditing(props: VisualEditingProps): React.ReactElement {\n let autoBasePath: string | undefined\n if (typeof props.basePath !== 'string') {\n try {\n autoBasePath = process.env['__NEXT_ROUTER_BASEPATH']\n if (autoBasePath) {\n // eslint-disable-next-line no-console\n console.log(\n `Detected next basePath as ${JSON.stringify(autoBasePath)} by reading \"process.env.__NEXT_ROUTER_BASEPATH\". If this is incorrect then you can set it manually with the basePath prop on the <VisualEditing /> component.`,\n )\n }\n } catch (err) {\n console.error('Failed detecting basePath', err)\n }\n }\n return (\n <Suspense fallback={null}>\n <VisualEditingComponent {...props} basePath={props.basePath ?? autoBasePath} />\n </Suspense>\n )\n}\n\nexport {\n type CreateDataAttribute,\n createDataAttribute,\n type CreateDataAttributeProps,\n} from '@sanity/visual-editing/create-data-attribute'\nexport type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'\n"],"names":["VisualEditingComponent","lazy","import","Object","defineProperty","exports","enumerable","get","client","createClient","unstable__adapter","unstable__environment","groq__default","default","createDataAttribute","VisualEditing","props","_a","autoBasePath","basePath","process","env","__NEXT_ROUTER_BASEPATH","console","log","JSON","stringify","err","error","jsx","Suspense","fallback","children","keys","react","forEach","k","prototype","hasOwnProperty","call"],"mappings":"2eAIA,MAAMA,EAAyBC,EAAAA,MAAK,IAAMC,OAAO,iDAyBjDC,OAAAC,eAAAC,QAAA,eAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAC,EAAAC,YAAA,IAAAN,OAAAC,eAAAC,QAAA,oBAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAC,EAAAE,iBAAA,IAAAP,OAAAC,eAAAC,QAAA,wBAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAC,EAAAG,qBAAA,IAAAR,OAAAC,eAAAC,QAAA,OAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAK,EAAAC,OAAA,IAAAV,OAAAC,eAAAC,QAAA,sBAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAO,EAAAA,mBAAA,IAAAT,QAAAU,cApBO,SAAuBC,GAT9B,IAAAC,EAUM,IAAAC,EACA,GAA0B,iBAAnBF,EAAMG,SACX,IACFD,EAAeE,QAAQC,IAAIC,uBACvBJ,GAEFK,QAAQC,IACN,6BAA6BC,KAAKC,UAAUR,0KAGzCS,GACCJ,QAAAK,MAAM,4BAA6BD,EAC7C,CAGCE,OAAAA,EAAAA,IAAAC,EAAAA,SAAA,CAASC,SAAU,KAClBC,SAACH,EAAAA,IAAA7B,EAAA,IAA2BgB,EAAOG,SAAU,OAAAF,EAAAD,EAAMG,UAANF,EAAkBC,KAGrE,EAAAf,OAAA8B,KAAAC,GAAAC,SAAA,SAAAC,GAAA,YAAAA,IAAAjC,OAAAkC,UAAAC,eAAAC,KAAAlC,QAAA+B,IAAAjC,OAAAC,eAAAC,QAAA+B,EAAA,CAAA9B,YAAA,EAAAC,IAAA,WAAA,OAAA2B,EAAAE,EAAA,GAAA"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/visual-editing/index.tsx"],"sourcesContent":["/* eslint-disable dot-notation */\nimport type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'\nimport {lazy, Suspense} from 'react'\n\nconst VisualEditingComponent = lazy(() => import('next-sanity/visual-editing/client-component'))\n\n/**\n * @public\n */\nexport function VisualEditing(props: VisualEditingProps): React.ReactElement {\n let autoBasePath: string | undefined\n if (typeof props.basePath !== 'string') {\n try {\n autoBasePath = process.env['__NEXT_ROUTER_BASEPATH']\n if (autoBasePath) {\n // eslint-disable-next-line no-console\n console.log(\n `Detected next basePath as ${JSON.stringify(autoBasePath)} by reading \"process.env.__NEXT_ROUTER_BASEPATH\". If this is incorrect then you can set it manually with the basePath prop on the <VisualEditing /> component.`,\n )\n }\n } catch (err) {\n console.error('Failed detecting basePath', err)\n }\n }\n let autoTrailingSlash: boolean | undefined\n if (typeof props.trailingSlash !== 'boolean') {\n try {\n autoTrailingSlash = Boolean(process.env['__NEXT_TRAILING_SLASH'])\n if (autoTrailingSlash) {\n // eslint-disable-next-line no-console\n console.log(\n `Detected next trailingSlash as ${JSON.stringify(autoTrailingSlash)} by reading \"process.env.__NEXT_TRAILING_SLASH\". If this is incorrect then you can set it manually with the trailingSlash prop on the <VisualEditing /> component.`,\n )\n }\n } catch (err) {\n console.error('Failed detecting trailingSlash', err)\n }\n }\n return (\n <Suspense fallback={null}>\n <VisualEditingComponent\n {...props}\n basePath={props.basePath ?? autoBasePath}\n trailingSlash={props.trailingSlash ?? autoTrailingSlash}\n />\n </Suspense>\n )\n}\n\nexport {\n type CreateDataAttribute,\n createDataAttribute,\n type CreateDataAttributeProps,\n} from '@sanity/visual-editing/create-data-attribute'\nexport type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'\n"],"names":["VisualEditingComponent","lazy","import","Object","defineProperty","exports","enumerable","get","client","createClient","unstable__adapter","unstable__environment","groq__default","default","createDataAttribute","VisualEditing","props","_a","_b","autoBasePath","autoTrailingSlash","basePath","process","env","__NEXT_ROUTER_BASEPATH","console","log","JSON","stringify","err","error","trailingSlash","__NEXT_TRAILING_SLASH","jsx","Suspense","fallback","children","jsxRuntime","keys","react","forEach","k","prototype","hasOwnProperty","call"],"mappings":"2eAIA,MAAMA,EAAyBC,EAAAA,MAAK,IAAMC,OAAO,iDA2CjDC,OAAAC,eAAAC,QAAA,eAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAC,EAAAC,YAAA,IAAAN,OAAAC,eAAAC,QAAA,oBAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAC,EAAAE,iBAAA,IAAAP,OAAAC,eAAAC,QAAA,wBAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAC,EAAAG,qBAAA,IAAAR,OAAAC,eAAAC,QAAA,OAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAK,EAAAC,OAAA,IAAAV,OAAAC,eAAAC,QAAA,sBAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAO,EAAAA,mBAAA,IAAAT,QAAAU,cAtCO,SAAuBC,GAT9B,IAAAC,EAAAC,EAUM,IAAAC,EAcAC,EAbA,GAA0B,iBAAnBJ,EAAMK,SACX,IACFF,EAAeG,QAAQC,IAAIC,uBACvBL,GAEFM,QAAQC,IACN,6BAA6BC,KAAKC,UAAUT,0KAGzCU,GACCJ,QAAAK,MAAM,4BAA6BD,EAC7C,CAGE,GAA+B,kBAAxBb,EAAMe,cACX,IACFX,IAA4BE,QAAQC,IAAIS,sBACpCZ,GAEFK,QAAQC,IACN,kCAAkCC,KAAKC,UAAUR,8KAG9CS,GACCJ,QAAAK,MAAM,iCAAkCD,EAClD,CAGAI,OAAAA,EAAAA,IAACC,EAAAA,SAAS,CAAAC,SAAU,KAClBC,SAAAC,EAAAJ,IAACjC,EAAA,IACKgB,EACJK,SAAU,OAAAJ,EAAMD,EAAAK,UAAYJ,EAAAE,EAC5BY,cAAe,OAAAb,EAAMF,EAAAe,eAAiBb,EAAAE,KAI9C,EAAAjB,OAAAmC,KAAAC,GAAAC,SAAA,SAAAC,GAAA,YAAAA,IAAAtC,OAAAuC,UAAAC,eAAAC,KAAAvC,QAAAoC,IAAAtC,OAAAC,eAAAC,QAAAoC,EAAA,CAAAnC,YAAA,EAAAC,IAAA,WAAA,OAAAgC,EAAAE,EAAA,GAAA"}
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{createClient as t,unstable__adapter as e,unstable__environment as a}from"@sanity/client";import{jsx as r}from"react/jsx-runtime";import{lazy as i,Suspense as o}from"react";export*from"@portabletext/react";import{default as n}from"groq";import{createDataAttribute as s}from"@sanity/visual-editing/create-data-attribute";const c=i((()=>import("next-sanity/visual-editing/client-component")));function l(t){var e;let a;if("string"!=typeof t.basePath)try{a=process.env.__NEXT_ROUTER_BASEPATH,a&&console.log(`Detected next basePath as ${JSON.stringify(a)} by reading "process.env.__NEXT_ROUTER_BASEPATH". If this is incorrect then you can set it manually with the basePath prop on the <VisualEditing /> component.`)}catch(t){console.error("Failed detecting basePath",t)}return r(o,{fallback:null,children:r(c,{...t,basePath:null!=(e=t.basePath)?e:a})})}export{l as VisualEditing,t as createClient,s as createDataAttribute,n as groq,e as unstable__adapter,a as unstable__environment};//# sourceMappingURL=index.js.map
1
+ import{createClient as t,unstable__adapter as e,unstable__environment as i}from"@sanity/client";import{jsx as a}from"react/jsx-runtime";import{lazy as n,Suspense as r}from"react";export*from"@portabletext/react";import{default as o}from"groq";import{createDataAttribute as s}from"@sanity/visual-editing/create-data-attribute";const l=n((()=>import("next-sanity/visual-editing/client-component")));function c(t){var e,i;let n,o;if("string"!=typeof t.basePath)try{n=process.env.__NEXT_ROUTER_BASEPATH,n&&console.log(`Detected next basePath as ${JSON.stringify(n)} by reading "process.env.__NEXT_ROUTER_BASEPATH". If this is incorrect then you can set it manually with the basePath prop on the <VisualEditing /> component.`)}catch(t){console.error("Failed detecting basePath",t)}if("boolean"!=typeof t.trailingSlash)try{o=!!process.env.__NEXT_TRAILING_SLASH,o&&console.log(`Detected next trailingSlash as ${JSON.stringify(o)} by reading "process.env.__NEXT_TRAILING_SLASH". If this is incorrect then you can set it manually with the trailingSlash prop on the <VisualEditing /> component.`)}catch(t){console.error("Failed detecting trailingSlash",t)}return a(r,{fallback:null,children:a(l,{...t,basePath:null!=(e=t.basePath)?e:n,trailingSlash:null!=(i=t.trailingSlash)?i:o})})}export{c as VisualEditing,t as createClient,s as createDataAttribute,o as groq,e as unstable__adapter,i as unstable__environment};//# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/visual-editing/index.tsx"],"sourcesContent":["/* eslint-disable dot-notation */\nimport type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'\nimport {lazy, Suspense} from 'react'\n\nconst VisualEditingComponent = lazy(() => import('next-sanity/visual-editing/client-component'))\n\n/**\n * @public\n */\nexport function VisualEditing(props: VisualEditingProps): React.ReactElement {\n let autoBasePath: string | undefined\n if (typeof props.basePath !== 'string') {\n try {\n autoBasePath = process.env['__NEXT_ROUTER_BASEPATH']\n if (autoBasePath) {\n // eslint-disable-next-line no-console\n console.log(\n `Detected next basePath as ${JSON.stringify(autoBasePath)} by reading \"process.env.__NEXT_ROUTER_BASEPATH\". If this is incorrect then you can set it manually with the basePath prop on the <VisualEditing /> component.`,\n )\n }\n } catch (err) {\n console.error('Failed detecting basePath', err)\n }\n }\n return (\n <Suspense fallback={null}>\n <VisualEditingComponent {...props} basePath={props.basePath ?? autoBasePath} />\n </Suspense>\n )\n}\n\nexport {\n type CreateDataAttribute,\n createDataAttribute,\n type CreateDataAttributeProps,\n} from '@sanity/visual-editing/create-data-attribute'\nexport type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'\n"],"names":["VisualEditingComponent","lazy","import","VisualEditing","props","_a","autoBasePath","basePath","process","env","__NEXT_ROUTER_BASEPATH","console","log","JSON","stringify","err","error","jsx","Suspense","fallback","children"],"mappings":"sUAIA,MAAMA,EAAyBC,GAAK,IAAMC,OAAO,iDAK1C,SAASC,EAAcC,GAT9B,IAAAC,EAUM,IAAAC,EACA,GAA0B,iBAAnBF,EAAMG,SACX,IACFD,EAAeE,QAAQC,IAAIC,uBACvBJ,GAEFK,QAAQC,IACN,6BAA6BC,KAAKC,UAAUR,0KAGzCS,GACCJ,QAAAK,MAAM,4BAA6BD,EAC7C,CAGC,OAAAE,EAAAC,EAAA,CAASC,SAAU,KAClBC,SAACH,EAAAjB,EAAA,IAA2BI,EAAOG,SAAU,OAAAF,EAAAD,EAAMG,UAANF,EAAkBC,KAGrE"}
1
+ {"version":3,"file":"index.js","sources":["../src/visual-editing/index.tsx"],"sourcesContent":["/* eslint-disable dot-notation */\nimport type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'\nimport {lazy, Suspense} from 'react'\n\nconst VisualEditingComponent = lazy(() => import('next-sanity/visual-editing/client-component'))\n\n/**\n * @public\n */\nexport function VisualEditing(props: VisualEditingProps): React.ReactElement {\n let autoBasePath: string | undefined\n if (typeof props.basePath !== 'string') {\n try {\n autoBasePath = process.env['__NEXT_ROUTER_BASEPATH']\n if (autoBasePath) {\n // eslint-disable-next-line no-console\n console.log(\n `Detected next basePath as ${JSON.stringify(autoBasePath)} by reading \"process.env.__NEXT_ROUTER_BASEPATH\". If this is incorrect then you can set it manually with the basePath prop on the <VisualEditing /> component.`,\n )\n }\n } catch (err) {\n console.error('Failed detecting basePath', err)\n }\n }\n let autoTrailingSlash: boolean | undefined\n if (typeof props.trailingSlash !== 'boolean') {\n try {\n autoTrailingSlash = Boolean(process.env['__NEXT_TRAILING_SLASH'])\n if (autoTrailingSlash) {\n // eslint-disable-next-line no-console\n console.log(\n `Detected next trailingSlash as ${JSON.stringify(autoTrailingSlash)} by reading \"process.env.__NEXT_TRAILING_SLASH\". If this is incorrect then you can set it manually with the trailingSlash prop on the <VisualEditing /> component.`,\n )\n }\n } catch (err) {\n console.error('Failed detecting trailingSlash', err)\n }\n }\n return (\n <Suspense fallback={null}>\n <VisualEditingComponent\n {...props}\n basePath={props.basePath ?? autoBasePath}\n trailingSlash={props.trailingSlash ?? autoTrailingSlash}\n />\n </Suspense>\n )\n}\n\nexport {\n type CreateDataAttribute,\n createDataAttribute,\n type CreateDataAttributeProps,\n} from '@sanity/visual-editing/create-data-attribute'\nexport type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'\n"],"names":["VisualEditingComponent","lazy","import","VisualEditing","props","_a","_b","autoBasePath","autoTrailingSlash","basePath","process","env","__NEXT_ROUTER_BASEPATH","console","log","JSON","stringify","err","error","trailingSlash","__NEXT_TRAILING_SLASH","jsx","Suspense","fallback","children"],"mappings":"sUAIA,MAAMA,EAAyBC,GAAK,IAAMC,OAAO,iDAK1C,SAASC,EAAcC,GAT9B,IAAAC,EAAAC,EAUM,IAAAC,EAcAC,EAbA,GAA0B,iBAAnBJ,EAAMK,SACX,IACFF,EAAeG,QAAQC,IAAIC,uBACvBL,GAEFM,QAAQC,IACN,6BAA6BC,KAAKC,UAAUT,0KAGzCU,GACCJ,QAAAK,MAAM,4BAA6BD,EAC7C,CAGE,GAA+B,kBAAxBb,EAAMe,cACX,IACFX,IAA4BE,QAAQC,IAAIS,sBACpCZ,GAEFK,QAAQC,IACN,kCAAkCC,KAAKC,UAAUR,8KAG9CS,GACCJ,QAAAK,MAAM,iCAAkCD,EAClD,CAGA,OAACI,EAAAC,EAAS,CAAAC,SAAU,KAClBC,SAAAH,EAACrB,EAAA,IACKI,EACJK,SAAU,OAAAJ,EAAMD,EAAAK,UAAYJ,EAAAE,EAC5BY,cAAe,OAAAb,EAAMF,EAAAe,eAAiBb,EAAAE,KAI9C"}
@@ -1 +1 @@
1
- "use client";"use strict";var e=require("@sanity/visual-editing"),t=require("next/navigation.js"),r=require("next-sanity/visual-editing/server-actions"),n=require("react");function s(e){const t=e.indexOf("#"),r=e.indexOf("?"),n=r>-1&&(t<0||r<t);return n||t>-1?{pathname:e.substring(0,n?r:t),query:n?e.substring(r,t>-1?t:void 0):"",hash:t>-1?e.slice(t):""}:{pathname:e,query:"",hash:""}}function u(e,t){if(!e.startsWith("/")||!t)return e;if("/"===e&&t)return t;const{pathname:r,query:n,hash:u}=s(e);return`${t}${r}${n}${u}`}function i(e,t){if(!function(e,t){if("string"!=typeof e)return!1;const{pathname:r}=s(e);return r===t||r.startsWith(`${t}/`)}(e,t))return e;const r=e.slice(t.length);return r.startsWith("/")?r:`/${r}`}module.exports=function(s){const{refresh:a,zIndex:o,basePath:c=""}=s,l=t.useRouter(),d=n.useRef(l),[h,p]=n.useState();n.useEffect((()=>{d.current=l}),[l]),n.useEffect((()=>{const t=e.enableVisualEditing({zIndex:o,refresh:a||(e=>{switch(e.source){case"manual":return e.livePreviewEnabled?(console.debug("Live preview is setup, calling router.refresh() to refresh the server components without refetching cached data"),d.current.refresh(),Promise.resolve()):(console.debug("No loaders in live mode detected, or preview kit setup, revalidating root layout"),r.revalidateRootLayout());case"mutation":return e.livePreviewEnabled?(console.debug("Live preview is setup, mutation is skipped assuming its handled by the live preview"),!1):(console.debug("No loaders in live mode detected, or preview kit setup, revalidating root layout"),r.revalidateRootLayout());default:throw new Error("Unknown refresh source",{cause:e})}}),history:{subscribe:e=>(p((()=>e)),()=>p(void 0)),update:e=>{switch(e.type){case"push":return d.current.push(i(e.url,c));case"pop":return d.current.back();case"replace":return d.current.replace(i(e.url,c));default:throw new Error(`Unknown update type: ${e.type}`)}}}});return()=>t()}),[c,a,o]);const f=t.usePathname(),v=t.useSearchParams();return n.useEffect((()=>{h&&h({type:"push",url:u(`${f}${null!=v&&v.size?`?${v}`:""}`,c)})}),[c,h,f,v]),null};//# sourceMappingURL=client-component.cjs.map
1
+ "use client";"use strict";var e=require("@sanity/visual-editing"),t=require("next/navigation.js"),r=require("next-sanity/visual-editing/server-actions"),n=require("react");function s(e){const t=e.indexOf("#"),r=e.indexOf("?"),n=r>-1&&(t<0||r<t);return n||t>-1?{pathname:e.substring(0,n?r:t),query:n?e.substring(r,t>-1?t:void 0):"",hash:t>-1?e.slice(t):""}:{pathname:e,query:"",hash:""}}function i(e,t){if(!e.startsWith("/")||!t)return e;if("/"===e&&t)return t;const{pathname:r,query:n,hash:i}=s(e);return`${t}${r}${n}${i}`}function u(e,t){if(!function(e,t){if("string"!=typeof e)return!1;const{pathname:r}=s(e);return r===t||r.startsWith(`${t}/`)}(e,t))return e;const r=e.slice(t.length);return r.startsWith("/")?r:`/${r}`}const a=(e,t)=>{const{pathname:r,query:n,hash:i}=s(e);return t?r.endsWith("/")?`${r}${n}${i}`:`${r}/${n}${i}`:`${u=r,u.replace(/\/$/,"")||"/"}${n}${i}`;var u};module.exports=function(s){const{refresh:o,zIndex:c,basePath:l="",trailingSlash:h=!1}=s,d=t.useRouter(),p=n.useRef(d),[f,v]=n.useState();n.useEffect((()=>{p.current=d}),[d]),n.useEffect((()=>{const t=e.enableVisualEditing({zIndex:c,refresh:o||(e=>{switch(e.source){case"manual":return e.livePreviewEnabled?(console.debug("Live preview is setup, calling router.refresh() to refresh the server components without refetching cached data"),p.current.refresh(),Promise.resolve()):(console.debug("No loaders in live mode detected, or preview kit setup, revalidating root layout"),r.revalidateRootLayout());case"mutation":return e.livePreviewEnabled?(console.debug("Live preview is setup, mutation is skipped assuming its handled by the live preview"),!1):(console.debug("No loaders in live mode detected, or preview kit setup, revalidating root layout"),r.revalidateRootLayout());default:throw new Error("Unknown refresh source",{cause:e})}}),history:{subscribe:e=>(v((()=>e)),()=>v(void 0)),update:e=>{switch(e.type){case"push":return p.current.push(u(e.url,l));case"pop":return p.current.back();case"replace":return p.current.replace(u(e.url,l));default:throw new Error(`Unknown update type: ${e.type}`)}}}});return()=>t()}),[l,o,c]);const $=t.usePathname(),g=t.useSearchParams();return n.useEffect((()=>{f&&f({type:"push",url:a(i(`${$}${null!=g&&g.size?`?${g}`:""}`,l),h)})}),[l,f,$,g,h]),null};//# sourceMappingURL=client-component.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"client-component.cjs","sources":["../../src/visual-editing/client-component/utils.ts","../../src/visual-editing/client-component/index.ts"],"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","'use client'\nimport {\n enableVisualEditing,\n type HistoryAdapterNavigate,\n type VisualEditingOptions,\n} from '@sanity/visual-editing'\nimport {usePathname, useRouter, useSearchParams} from 'next/navigation.js'\nimport {revalidateRootLayout} from 'next-sanity/visual-editing/server-actions'\nimport {useEffect, useRef, useState} from 'react'\n\nimport {addPathPrefix, 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\nexport default function VisualEditing(props: VisualEditingProps): null {\n const {refresh, zIndex, basePath = ''} = 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 useEffect(() => {\n const disable = enableVisualEditing({\n zIndex,\n refresh: refresh\n ? refresh\n : (payload) => {\n switch (payload.source) {\n case 'manual':\n return payload.livePreviewEnabled ? manualFastRefresh() : manualFallbackRefresh()\n case 'mutation':\n return payload.livePreviewEnabled\n ? mutationFastRefresh()\n : mutationFallbackRefresh()\n default:\n throw new Error('Unknown refresh source', {cause: payload})\n }\n },\n history: {\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: ${update.type}`)\n }\n },\n },\n })\n\n function manualFastRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'Live preview is setup, calling router.refresh() to refresh the server components without refetching cached data',\n )\n routerRef.current.refresh()\n return Promise.resolve()\n }\n function manualFallbackRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'No loaders in live mode detected, or preview kit setup, revalidating root layout',\n )\n return revalidateRootLayout()\n }\n function mutationFastRefresh(): false {\n // eslint-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 function mutationFallbackRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'No loaders in live mode detected, or preview kit setup, revalidating root layout',\n )\n return revalidateRootLayout()\n }\n\n return () => disable()\n }, [basePath, refresh, zIndex])\n\n const pathname = usePathname()\n const searchParams = useSearchParams()\n useEffect(() => {\n if (navigate) {\n navigate({\n type: 'push',\n url: addPathPrefix(\n `${pathname}${searchParams?.size ? `?${searchParams}` : ''}`,\n basePath,\n ),\n })\n }\n }, [basePath, navigate, pathname, searchParams])\n\n return null\n}\n"],"names":["visualEditing","require","navigation_js","serverActions","react","parsePath","path","hashIndex","indexOf","queryIndex","hasQuery","pathname","substring","query","hash","slice","addPathPrefix","prefix","startsWith","removePathPrefix","pathHasPrefix","withoutPrefix","length","module","exports","props","refresh","zIndex","basePath","router","useRouter","routerRef","useRef","navigate","setNavigate","useState","useEffect","current","disable","enableVisualEditing","payload","source","livePreviewEnabled","console","debug","Promise","resolve","revalidateRootLayout","Error","cause","history","subscribe","_navigate","update","type","push","url","back","replace","usePathname","searchParams","useSearchParams","size"],"mappings":"0BAQA,IAAAA,EAAAC,QAAA,0BAAAC,EAAAD,QAAA,sBAAAE,EAAAF,QAAA,6CAAAG,EAAAH,QAAA,SAeA,SAASI,EAAUC,GAKjB,MAAMC,EAAYD,EAAKE,Q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,QAAAC,MACN,mHAEFb,EAAUM,QAAQX,UACXmB,QAAQC,YAIPH,QAAAC,MACN,oFAEKG,EAAqBA,wBAzCpB,IAAK,WACH,OAAOP,EAAQE,oBA4CjBC,QAAAC,MACN,wFAEK,IAICD,QAAAC,MACN,oFAEKG,EAAqBA,wBAnDpB,QACE,MAAM,IAAIC,MAAM,yBAA0B,CAACC,MAAOT,IACtD,GAENU,QAAS,CACPC,UAAYC,IACVlB,GAAY,IAAMkB,IACX,IAAMlB,OAAY,IAE3BmB,OAASA,IACP,OAAQA,EAAOC,MACb,IAAK,OACH,OAAOvB,EAAUM,QAAQkB,KAAKpC,EAAiBkC,EAAOG,IAAK5B,IAC7D,IAAK,MACI,OAAAG,EAAUM,QAAQoB,OAC3B,IAAK,UACH,OAAO1B,EAAUM,QAAQqB,QAAQvC,EAAiBkC,EAAOG,IAAK5B,IAChE,QACE,MAAM,IAAIoB,MAAM,wBAAwBK,EAAOC,QACnD,KAmCN,MAAO,IAAMhB,GAAQ,GACpB,CAACV,EAAUF,EAASC,IAEvB,MAAMhB,EAAWgD,EAAAA,cACXC,EAAeC,EAAgBA,kBACrCzB,OAAAA,EAAAA,WAAU,KACJH,GACFA,EAAS,CACPqB,KAAM,OACNE,IAAKxC,EACH,GAAGL,IAAW,MAAAiD,GAAAA,EAAcE,KAAO,IAAIF,IAAiB,KACxDhC,IAEH,GAEF,CAACA,EAAUK,EAAUtB,EAAUiD,IAE3B,IACT"}
1
+ {"version":3,"file":"client-component.cjs","sources":["../../src/visual-editing/client-component/utils.ts","../../src/visual-editing/client-component/index.ts"],"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","'use client'\nimport {\n enableVisualEditing,\n type HistoryAdapterNavigate,\n type VisualEditingOptions,\n} from '@sanity/visual-editing'\nimport {usePathname, useRouter, useSearchParams} from 'next/navigation.js'\nimport {revalidateRootLayout} from 'next-sanity/visual-editing/server-actions'\nimport {useEffect, 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): null {\n const {refresh, zIndex, basePath = '', trailingSlash = false} = 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 useEffect(() => {\n const disable = enableVisualEditing({\n zIndex,\n refresh: refresh\n ? refresh\n : (payload) => {\n switch (payload.source) {\n case 'manual':\n return payload.livePreviewEnabled ? manualFastRefresh() : manualFallbackRefresh()\n case 'mutation':\n return payload.livePreviewEnabled\n ? mutationFastRefresh()\n : mutationFallbackRefresh()\n default:\n throw new Error('Unknown refresh source', {cause: payload})\n }\n },\n history: {\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: ${update.type}`)\n }\n },\n },\n })\n\n function manualFastRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'Live preview is setup, calling router.refresh() to refresh the server components without refetching cached data',\n )\n routerRef.current.refresh()\n return Promise.resolve()\n }\n function manualFallbackRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'No loaders in live mode detected, or preview kit setup, revalidating root layout',\n )\n return revalidateRootLayout()\n }\n function mutationFastRefresh(): false {\n // eslint-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 function mutationFallbackRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'No loaders in live mode detected, or preview kit setup, revalidating root layout',\n )\n return revalidateRootLayout()\n }\n\n return () => disable()\n }, [basePath, refresh, zIndex])\n\n const pathname = usePathname()\n const searchParams = useSearchParams()\n useEffect(() => {\n if (navigate) {\n navigate({\n type: 'push',\n url: normalizePathTrailingSlash(\n addPathPrefix(`${pathname}${searchParams?.size ? `?${searchParams}` : ''}`, basePath),\n trailingSlash,\n ),\n })\n }\n }, [basePath, navigate, pathname, searchParams, trailingSlash])\n\n return null\n}\n"],"names":["visualEditing","require","navigation_js","serverActions","react","parsePath","path","hashIndex","indexOf","queryIndex","hasQuery","pathname","substring","query","hash","slice","addPathPrefix","prefix","startsWith","removePathPrefix","pathHasPrefix","withoutPrefix","length","normalizePathTrailingSlash","trailingSlash","endsWith","route","replace","module","exports","props","refresh","zIndex","basePath","router","useRouter","routerRef","useRef","navigate","setNavigate","useState","useEffect","current","disable","enableVisualEditing","payload","source","livePreviewEnabled","console","debug","Promise","resolve","revalidateRootLayout","Error","cause","history","subscribe","_navigate","update","type","push","url","back","usePathname","searchParams","useSearchParams","size"],"mappings":"0BAQA,IAAAA,EAAAC,QAAA,0BAAAC,EAAAD,QAAA,sBAAAE,EAAAF,QAAA,6CAAAG,EAAAH,QAAA,SAeA,SAASI,EAAUC,GAKjB,MAAMC,EAAYD,EAAKE,QAAQ,KACzBC,EAAaH,EAAKE,QAAQ,KAC1BE,EAAWD,GAAoB,IAAAF,EAAY,GAAKE,EAAaF,GAE/D,OAAAG,GAAYH,GAC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A,WAAU,KACRL,EAAUM,QAAUR,CAAA,GACnB,CAACA,IACJO,EAAAA,WAAU,KACF,MAAAE,EAAUC,EAAAA,oBAAoB,CAClCZ,SACAD,QAASA,GAEL,CAACc,IACC,OAAQA,EAAQC,QACd,IAAK,SACH,OAAOD,EAAQE,oBA+BjBC,QAAAC,MACN,mHAEFb,EAAUM,QAAQX,UACXmB,QAAQC,YAIPH,QAAAC,MACN,oFAEKG,EAAqBA,wBAzCpB,IAAK,WACH,OAAOP,EAAQE,oBA4CjBC,QAAAC,MACN,wFAEK,IAICD,QAAAC,MACN,oFAEKG,EAAqBA,wBAnDpB,QACE,MAAM,IAAIC,MAAM,yBAA0B,CAACC,MAAOT,IACtD,GAENU,QAAS,CACPC,UAAYC,IACVlB,GAAY,IAAMkB,IACX,IAAMlB,OAAY,IAE3BmB,OAASA,IACP,OAAQA,EAAOC,MACb,IAAK,OACH,OAAOvB,EAAUM,QAAQkB,KAAKzC,EAAiBuC,EAAOG,IAAK5B,IAC7D,IAAK,MACI,OAAAG,EAAUM,QAAQoB,OAC3B,IAAK,UACH,OAAO1B,EAAUM,QAAQf,QAAQR,EAAiBuC,EAAOG,IAAK5B,IAChE,QACE,MAAM,IAAIoB,MAAM,wBAAwBK,EAAOC,QACnD,KAmCN,MAAO,IAAMhB,GAAQ,GACpB,CAACV,EAAUF,EAASC,IAEvB,MAAMrB,EAAWoD,EAAAA,cACXC,EAAeC,EAAgBA,kBACrCxB,OAAAA,EAAAA,WAAU,KACJH,GACFA,EAAS,CACPqB,KAAM,OACNE,IAAKtC,EACHP,EAAc,GAAGL,IAAW,MAAAqD,GAAAA,EAAcE,KAAO,IAAIF,IAAiB,KAAM/B,GAC5ET,IAEH,GAEF,CAACS,EAAUK,EAAU3B,EAAUqD,EAAcxC,IAEzC,IACT"}
@@ -19,6 +19,14 @@ export declare interface VisualEditingProps extends Omit<VisualEditingOptions, '
19
19
  * @defaultValue process.env.__NEXT_ROUTER_BASEPATH || ''
20
20
  */
21
21
  basePath?: string
22
+ /**
23
+ * If next.config.ts is configured with a `trailingSlash` we try to detect it automatically,
24
+ * it can be controlled manually by passing a boolean.
25
+ * @example trailingSlash={true}
26
+ * @alpha experimental and may change without notice
27
+ * @defaultValue Boolean(process.env.__NEXT_TRAILING_SLASH)
28
+ */
29
+ trailingSlash?: boolean
22
30
  }
23
31
 
24
32
  export {}
@@ -19,6 +19,14 @@ export declare interface VisualEditingProps extends Omit<VisualEditingOptions, '
19
19
  * @defaultValue process.env.__NEXT_ROUTER_BASEPATH || ''
20
20
  */
21
21
  basePath?: string
22
+ /**
23
+ * If next.config.ts is configured with a `trailingSlash` we try to detect it automatically,
24
+ * it can be controlled manually by passing a boolean.
25
+ * @example trailingSlash={true}
26
+ * @alpha experimental and may change without notice
27
+ * @defaultValue Boolean(process.env.__NEXT_TRAILING_SLASH)
28
+ */
29
+ trailingSlash?: boolean
22
30
  }
23
31
 
24
32
  export {}
@@ -1 +1 @@
1
- "use client";import{enableVisualEditing as e}from"@sanity/visual-editing";import{useRouter as r,usePathname as t,useSearchParams as n}from"next/navigation.js";import{revalidateRootLayout as s}from"next-sanity/visual-editing/server-actions";import{useRef as i,useState as o,useEffect as u}from"react";function a(e){const r=e.indexOf("#"),t=e.indexOf("?"),n=t>-1&&(r<0||t<r);return n||r>-1?{pathname:e.substring(0,n?t:r),query:n?e.substring(t,r>-1?r:void 0):"",hash:r>-1?e.slice(r):""}:{pathname:e,query:"",hash:""}}function c(e,r){if(!e.startsWith("/")||!r)return e;if("/"===e&&r)return r;const{pathname:t,query:n,hash:s}=a(e);return`${r}${t}${n}${s}`}function l(e,r){if(!function(e,r){if("string"!=typeof e)return!1;const{pathname:t}=a(e);return t===r||t.startsWith(`${r}/`)}(e,r))return e;const t=e.slice(r.length);return t.startsWith("/")?t:`/${t}`}function h(a){const{refresh:h,zIndex:p,basePath:d=""}=a,f=r(),v=i(f),[m,g]=o();u((()=>{v.current=f}),[f]),u((()=>{const r=e({zIndex:p,refresh:h||(e=>{switch(e.source){case"manual":return e.livePreviewEnabled?(console.debug("Live preview is setup, calling router.refresh() to refresh the server components without refetching cached data"),v.current.refresh(),Promise.resolve()):(console.debug("No loaders in live mode detected, or preview kit setup, revalidating root layout"),s());case"mutation":return e.livePreviewEnabled?(console.debug("Live preview is setup, mutation is skipped assuming its handled by the live preview"),!1):(console.debug("No loaders in live mode detected, or preview kit setup, revalidating root layout"),s());default:throw new Error("Unknown refresh source",{cause:e})}}),history:{subscribe:e=>(g((()=>e)),()=>g(void 0)),update:e=>{switch(e.type){case"push":return v.current.push(l(e.url,d));case"pop":return v.current.back();case"replace":return v.current.replace(l(e.url,d));default:throw new Error(`Unknown update type: ${e.type}`)}}}});return()=>r()}),[d,h,p]);const w=t(),y=n();return u((()=>{m&&m({type:"push",url:c(`${w}${null!=y&&y.size?`?${y}`:""}`,d)})}),[d,m,w,y]),null}export{h as default};//# sourceMappingURL=client-component.js.map
1
+ "use client";import{enableVisualEditing as e}from"@sanity/visual-editing";import{useRouter as r,usePathname as t,useSearchParams as n}from"next/navigation.js";import{revalidateRootLayout as s}from"next-sanity/visual-editing/server-actions";import{useRef as i,useState as o,useEffect as u}from"react";function a(e){const r=e.indexOf("#"),t=e.indexOf("?"),n=t>-1&&(r<0||t<r);return n||r>-1?{pathname:e.substring(0,n?t:r),query:n?e.substring(t,r>-1?r:void 0):"",hash:r>-1?e.slice(r):""}:{pathname:e,query:"",hash:""}}function c(e,r){if(!e.startsWith("/")||!r)return e;if("/"===e&&r)return r;const{pathname:t,query:n,hash:s}=a(e);return`${r}${t}${n}${s}`}function l(e,r){if(!function(e,r){if("string"!=typeof e)return!1;const{pathname:t}=a(e);return t===r||t.startsWith(`${r}/`)}(e,r))return e;const t=e.slice(r.length);return t.startsWith("/")?t:`/${t}`}const h=(e,r)=>{const{pathname:t,query:n,hash:s}=a(e);return r?t.endsWith("/")?`${t}${n}${s}`:`${t}/${n}${s}`:`${i=t,i.replace(/\/$/,"")||"/"}${n}${s}`;var i};function p(a){const{refresh:p,zIndex:d,basePath:f="",trailingSlash:v=!1}=a,m=r(),$=i(m),[g,w]=o();u((()=>{$.current=m}),[m]),u((()=>{const r=e({zIndex:d,refresh:p||(e=>{switch(e.source){case"manual":return e.livePreviewEnabled?(console.debug("Live preview is setup, calling router.refresh() to refresh the server components without refetching cached data"),$.current.refresh(),Promise.resolve()):(console.debug("No loaders in live mode detected, or preview kit setup, revalidating root layout"),s());case"mutation":return e.livePreviewEnabled?(console.debug("Live preview is setup, mutation is skipped assuming its handled by the live preview"),!1):(console.debug("No loaders in live mode detected, or preview kit setup, revalidating root layout"),s());default:throw new Error("Unknown refresh source",{cause:e})}}),history:{subscribe:e=>(w((()=>e)),()=>w(void 0)),update:e=>{switch(e.type){case"push":return $.current.push(l(e.url,f));case"pop":return $.current.back();case"replace":return $.current.replace(l(e.url,f));default:throw new Error(`Unknown update type: ${e.type}`)}}}});return()=>r()}),[f,p,d]);const y=t(),b=n();return u((()=>{g&&g({type:"push",url:h(c(`${y}${null!=b&&b.size?`?${b}`:""}`,f),v)})}),[f,g,y,b,v]),null}export{p as default};//# sourceMappingURL=client-component.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"client-component.js","sources":["../../src/visual-editing/client-component/utils.ts","../../src/visual-editing/client-component/index.ts"],"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","'use client'\nimport {\n enableVisualEditing,\n type HistoryAdapterNavigate,\n type VisualEditingOptions,\n} from '@sanity/visual-editing'\nimport {usePathname, useRouter, useSearchParams} from 'next/navigation.js'\nimport {revalidateRootLayout} from 'next-sanity/visual-editing/server-actions'\nimport {useEffect, useRef, useState} from 'react'\n\nimport {addPathPrefix, 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\nexport default function VisualEditing(props: VisualEditingProps): null {\n const {refresh, zIndex, basePath = ''} = 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 useEffect(() => {\n const disable = enableVisualEditing({\n zIndex,\n refresh: refresh\n ? refresh\n : (payload) => {\n switch (payload.source) {\n case 'manual':\n return payload.livePreviewEnabled ? manualFastRefresh() : manualFallbackRefresh()\n case 'mutation':\n return payload.livePreviewEnabled\n ? mutationFastRefresh()\n : mutationFallbackRefresh()\n default:\n throw new Error('Unknown refresh source', {cause: payload})\n }\n },\n history: {\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: ${update.type}`)\n }\n },\n },\n })\n\n function manualFastRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'Live preview is setup, calling router.refresh() to refresh the server components without refetching cached data',\n )\n routerRef.current.refresh()\n return Promise.resolve()\n }\n function manualFallbackRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'No loaders in live mode detected, or preview kit setup, revalidating root layout',\n )\n return revalidateRootLayout()\n }\n function mutationFastRefresh(): false {\n // eslint-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 function mutationFallbackRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'No loaders in live mode detected, or preview kit setup, revalidating root layout',\n )\n return revalidateRootLayout()\n }\n\n return () => disable()\n }, [basePath, refresh, zIndex])\n\n const pathname = usePathname()\n const searchParams = useSearchParams()\n useEffect(() => {\n if (navigate) {\n navigate({\n type: 'push',\n url: addPathPrefix(\n `${pathname}${searchParams?.size ? `?${searchParams}` : ''}`,\n basePath,\n ),\n })\n }\n }, [basePath, navigate, pathname, searchParams])\n\n return null\n}\n"],"names":["enableVisualEditing","useRouter","usePathname","useSearchParams","revalidateRootLayout","useRef","useState","useEffect","parsePath","path","hashIndex","indexOf","queryIndex","hasQuery","pathname","substring","query","hash","slice","addPathPrefix","prefix","startsWith","removePathPrefix","pathHasPrefix","withoutPrefix","length","VisualEditing","props","refresh","zIndex","basePath","router","routerRef","navigate","setNavigate","current","disable","payload","source","livePreviewEnabled","console","debug","Promise","resolve","Error","cause","history","subscribe","_navigate","update","type","push","url","back","replace","searchParams","size"],"mappings":"2CAQAA,MAAA,6CAAAC,iBAAAC,qBAAAC,MAAA,oDAAAC,MAAA,6DAAAC,cAAAC,eAAAC,MAAA,QAeA,SAASC,EAAUC,GAK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,QAAAC,MACN,mHAEFT,EAAUG,QAAQP,UACXc,QAAQC,YAIPH,QAAAC,MACN,oFAEKrC,KAzCC,IAAK,WACH,OAAOiC,EAAQE,oBA4CjBC,QAAAC,MACN,wFAEK,IAICD,QAAAC,MACN,oFAEKrC,KAnDC,QACE,MAAM,IAAIwC,MAAM,yBAA0B,CAACC,MAAOR,IACtD,GAENS,QAAS,CACPC,UAAYC,IACVd,GAAY,IAAMc,IACX,IAAMd,OAAY,IAE3Be,OAASA,IACP,OAAQA,EAAOC,MACb,IAAK,OACH,OAAOlB,EAAUG,QAAQgB,KAAK7B,EAAiB2B,EAAOG,IAAKtB,IAC7D,IAAK,MACI,OAAAE,EAAUG,QAAQkB,OAC3B,IAAK,UACH,OAAOrB,EAAUG,QAAQmB,QAAQhC,EAAiB2B,EAAOG,IAAKtB,IAChE,QACE,MAAM,IAAIc,MAAM,wBAAwBK,EAAOC,QACnD,KAmCN,MAAO,IAAMd,GAAQ,GACpB,CAACN,EAAUF,EAASC,IAEvB,MAAMf,EAAWZ,IACXqD,EAAepD,IACrB,OAAAI,GAAU,KACJ0B,GACFA,EAAS,CACPiB,KAAM,OACNE,IAAKjC,EACH,GAAGL,IAAW,MAAAyC,GAAAA,EAAcC,KAAO,IAAID,IAAiB,KACxDzB,IAEH,GAEF,CAACA,EAAUG,EAAUnB,EAAUyC,IAE3B,IACT"}
1
+ {"version":3,"file":"client-component.js","sources":["../../src/visual-editing/client-component/utils.ts","../../src/visual-editing/client-component/index.ts"],"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","'use client'\nimport {\n enableVisualEditing,\n type HistoryAdapterNavigate,\n type VisualEditingOptions,\n} from '@sanity/visual-editing'\nimport {usePathname, useRouter, useSearchParams} from 'next/navigation.js'\nimport {revalidateRootLayout} from 'next-sanity/visual-editing/server-actions'\nimport {useEffect, 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): null {\n const {refresh, zIndex, basePath = '', trailingSlash = false} = 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 useEffect(() => {\n const disable = enableVisualEditing({\n zIndex,\n refresh: refresh\n ? refresh\n : (payload) => {\n switch (payload.source) {\n case 'manual':\n return payload.livePreviewEnabled ? manualFastRefresh() : manualFallbackRefresh()\n case 'mutation':\n return payload.livePreviewEnabled\n ? mutationFastRefresh()\n : mutationFallbackRefresh()\n default:\n throw new Error('Unknown refresh source', {cause: payload})\n }\n },\n history: {\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: ${update.type}`)\n }\n },\n },\n })\n\n function manualFastRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'Live preview is setup, calling router.refresh() to refresh the server components without refetching cached data',\n )\n routerRef.current.refresh()\n return Promise.resolve()\n }\n function manualFallbackRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'No loaders in live mode detected, or preview kit setup, revalidating root layout',\n )\n return revalidateRootLayout()\n }\n function mutationFastRefresh(): false {\n // eslint-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 function mutationFallbackRefresh() {\n // eslint-disable-next-line no-console\n console.debug(\n 'No loaders in live mode detected, or preview kit setup, revalidating root layout',\n )\n return revalidateRootLayout()\n }\n\n return () => disable()\n }, [basePath, refresh, zIndex])\n\n const pathname = usePathname()\n const searchParams = useSearchParams()\n useEffect(() => {\n if (navigate) {\n navigate({\n type: 'push',\n url: normalizePathTrailingSlash(\n addPathPrefix(`${pathname}${searchParams?.size ? `?${searchParams}` : ''}`, basePath),\n trailingSlash,\n ),\n })\n }\n }, [basePath, navigate, pathname, searchParams, trailingSlash])\n\n return null\n}\n"],"names":["enableVisualEditing","useRouter","usePathname","useSearchParams","revalidateRootLayout","useRef","useState","useEffect","parsePath","path","hashIndex","indexOf","queryIndex","hasQuery","pathname","substring","query","hash","slice","addPathPrefix","prefix","startsWith","removePathPrefix","pathHasPrefix","withoutPrefix","length","normalizePathTrailingSlash","trailingSlash","endsWith","route","replace","VisualEditing","props","refresh","zIndex","basePath","router","routerRef","navigate","setNavigate","current","disable","payload","source","livePreviewEnabled","console","debug","Promise","resolve","Error","cause","history","subscribe","_navigate","update","type","push","url","back","searchParams","size"],"mappings":"2CAQAA,MAAA,6CAAAC,iBAAAC,qBAAAC,MAAA,oDAAAC,MAAA,6DAAAC,cAAAC,eAAAC,MAAA,QAeA,SAASC,EAAUC,GAKjB,MAAMC,EAAYD,EAAKE,QAAQ,KACzBC,EAAaH,EAAKE,QAAQ,KAC1BE,EAAWD,GAAoB,IAAAF,EAAY,GAAKE,EAAaF,GAE/D,OAAAG,GAAYH,GACP,EAAA,CACLI,SAAUL,EAAKM,UAAU,EAAGF,EAAWD,EAAaF,GACpDM,MAAOH,EAAWJ,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GAEL,CAACS,IACC,OAAQA,EAAQC,QACd,IAAK,SACH,OAAOD,EAAQE,oBA+BjBC,QAAAC,MACN,mHAEFT,EAAUG,QAAQP,UACXc,QAAQC,YAIPH,QAAAC,MACN,oFAEK1C,KAzCC,IAAK,WACH,OAAOsC,EAAQE,oBA4CjBC,QAAAC,MACN,wFAEK,IAICD,QAAAC,MACN,oFAEK1C,KAnDC,QACE,MAAM,IAAI6C,MAAM,yBAA0B,CAACC,MAAOR,IACtD,GAENS,QAAS,CACPC,UAAYC,IACVd,GAAY,IAAMc,IACX,IAAMd,OAAY,IAE3Be,OAASA,IACP,OAAQA,EAAOC,MACb,IAAK,OACH,OAAOlB,EAAUG,QAAQgB,KAAKlC,EAAiBgC,EAAOG,IAAKtB,IAC7D,IAAK,MACI,OAAAE,EAAUG,QAAQkB,OAC3B,IAAK,UACH,OAAOrB,EAAUG,QAAQV,QAAQR,EAAiBgC,EAAOG,IAAKtB,IAChE,QACE,MAAM,IAAIc,MAAM,wBAAwBK,EAAOC,QACnD,KAmCN,MAAO,IAAMd,GAAQ,GACpB,CAACN,EAAUF,EAASC,IAEvB,MAAMpB,EAAWZ,IACXyD,EAAexD,IACrB,OAAAI,GAAU,KACJ+B,GACFA,EAAS,CACPiB,KAAM,OACNE,IAAK/B,EACHP,EAAc,GAAGL,IAAW,MAAA6C,GAAAA,EAAcC,KAAO,IAAID,IAAiB,KAAMxB,GAC5ER,IAEH,GAEF,CAACQ,EAAUG,EAAUxB,EAAU6C,EAAchC,IAEzC,IACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-sanity",
3
- "version": "9.2.0",
3
+ "version": "9.3.0",
4
4
  "description": "Sanity.io toolkit for Next.js",
5
5
  "keywords": [
6
6
  "sanity",
@@ -119,8 +119,8 @@
119
119
  "browserslist": "extends @sanity/browserslist-config",
120
120
  "dependencies": {
121
121
  "@portabletext/react": "^3.0.18",
122
- "@sanity/client": "^6.18.2",
123
- "@sanity/preview-kit": "5.0.58",
122
+ "@sanity/client": "^6.18.3",
123
+ "@sanity/preview-kit": "5.0.59",
124
124
  "@sanity/visual-editing": "1.8.21",
125
125
  "groq": "^3.37.1",
126
126
  "history": "^5.3.0"
@@ -128,9 +128,9 @@
128
128
  "devDependencies": {
129
129
  "@sanity/browserslist-config": "^1.0.3",
130
130
  "@sanity/eslint-config-studio": "^4.0.0",
131
- "@sanity/pkg-utils": "^6.8.17",
131
+ "@sanity/pkg-utils": "^6.8.18",
132
132
  "@sanity/webhook": "4.0.4",
133
- "@types/react": "^18.3.2",
133
+ "@types/react": "^18.3.3",
134
134
  "@typescript-eslint/eslint-plugin": "^7.10.0",
135
135
  "@vitest/coverage-v8": "^1.6.0",
136
136
  "eslint": "^8.57.0",
@@ -147,7 +147,7 @@
147
147
  "vitest-github-actions-reporter": "^0.11.1"
148
148
  },
149
149
  "peerDependencies": {
150
- "@sanity/client": "^6.18.2",
150
+ "@sanity/client": "^6.18.3",
151
151
  "@sanity/icons": "^2.11.3",
152
152
  "@sanity/types": "^3.37.1",
153
153
  "@sanity/ui": "^2.0.11",
@@ -8,7 +8,7 @@ import {usePathname, useRouter, useSearchParams} from 'next/navigation.js'
8
8
  import {revalidateRootLayout} from 'next-sanity/visual-editing/server-actions'
9
9
  import {useEffect, useRef, useState} from 'react'
10
10
 
11
- import {addPathPrefix, removePathPrefix} from './utils'
11
+ import {addPathPrefix, normalizePathTrailingSlash, removePathPrefix} from './utils'
12
12
 
13
13
  /**
14
14
  * @public
@@ -26,10 +26,18 @@ export interface VisualEditingProps extends Omit<VisualEditingOptions, 'history'
26
26
  * @defaultValue process.env.__NEXT_ROUTER_BASEPATH || ''
27
27
  */
28
28
  basePath?: string
29
+ /**
30
+ * If next.config.ts is configured with a `trailingSlash` we try to detect it automatically,
31
+ * it can be controlled manually by passing a boolean.
32
+ * @example trailingSlash={true}
33
+ * @alpha experimental and may change without notice
34
+ * @defaultValue Boolean(process.env.__NEXT_TRAILING_SLASH)
35
+ */
36
+ trailingSlash?: boolean
29
37
  }
30
38
 
31
39
  export default function VisualEditing(props: VisualEditingProps): null {
32
- const {refresh, zIndex, basePath = ''} = props
40
+ const {refresh, zIndex, basePath = '', trailingSlash = false} = props
33
41
 
34
42
  const router = useRouter()
35
43
  const routerRef = useRef(router)
@@ -114,13 +122,13 @@ export default function VisualEditing(props: VisualEditingProps): null {
114
122
  if (navigate) {
115
123
  navigate({
116
124
  type: 'push',
117
- url: addPathPrefix(
118
- `${pathname}${searchParams?.size ? `?${searchParams}` : ''}`,
119
- basePath,
125
+ url: normalizePathTrailingSlash(
126
+ addPathPrefix(`${pathname}${searchParams?.size ? `?${searchParams}` : ''}`, basePath),
127
+ trailingSlash,
120
128
  ),
121
129
  })
122
130
  }
123
- }, [basePath, navigate, pathname, searchParams])
131
+ }, [basePath, navigate, pathname, searchParams, trailingSlash])
124
132
 
125
133
  return null
126
134
  }
@@ -97,3 +97,32 @@ export function removePathPrefix(path: string, prefix: string): string {
97
97
  // back to the path to make sure it's a valid path.
98
98
  return `/${withoutPrefix}`
99
99
  }
100
+
101
+ /**
102
+ * From: https://github.com/vercel/next.js/blob/dfe7fc03e2268e7cb765dce6a89e02c831c922d5/packages/next/src/client/normalize-trailing-slash.ts#L16
103
+ * Normalizes the trailing slash of a path according to the `trailingSlash` option
104
+ * in `next.config.js`.
105
+ */
106
+ export const normalizePathTrailingSlash = (path: string, trailingSlash: boolean): string => {
107
+ const {pathname, query, hash} = parsePath(path)
108
+ if (trailingSlash) {
109
+ if (pathname.endsWith('/')) {
110
+ return `${pathname}${query}${hash}`
111
+ }
112
+ return `${pathname}/${query}${hash}`
113
+ }
114
+
115
+ return `${removeTrailingSlash(pathname)}${query}${hash}`
116
+ }
117
+
118
+ /**
119
+ * From: https://github.com/vercel/next.js/blob/dfe7fc03e2268e7cb765dce6a89e02c831c922d5/packages/next/src/shared/lib/router/utils/remove-trailing-slash.ts#L8
120
+ * Removes the trailing slash for a given route or page path. Preserves the
121
+ * root page. Examples:
122
+ * - `/foo/bar/` -> `/foo/bar`
123
+ * - `/foo/bar` -> `/foo/bar`
124
+ * - `/` -> `/`
125
+ */
126
+ function removeTrailingSlash(route: string) {
127
+ return route.replace(/\/$/, '') || '/'
128
+ }
@@ -22,9 +22,27 @@ export function VisualEditing(props: VisualEditingProps): React.ReactElement {
22
22
  console.error('Failed detecting basePath', err)
23
23
  }
24
24
  }
25
+ let autoTrailingSlash: boolean | undefined
26
+ if (typeof props.trailingSlash !== 'boolean') {
27
+ try {
28
+ autoTrailingSlash = Boolean(process.env['__NEXT_TRAILING_SLASH'])
29
+ if (autoTrailingSlash) {
30
+ // eslint-disable-next-line no-console
31
+ console.log(
32
+ `Detected next trailingSlash as ${JSON.stringify(autoTrailingSlash)} by reading "process.env.__NEXT_TRAILING_SLASH". If this is incorrect then you can set it manually with the trailingSlash prop on the <VisualEditing /> component.`,
33
+ )
34
+ }
35
+ } catch (err) {
36
+ console.error('Failed detecting trailingSlash', err)
37
+ }
38
+ }
25
39
  return (
26
40
  <Suspense fallback={null}>
27
- <VisualEditingComponent {...props} basePath={props.basePath ?? autoBasePath} />
41
+ <VisualEditingComponent
42
+ {...props}
43
+ basePath={props.basePath ?? autoBasePath}
44
+ trailingSlash={props.trailingSlash ?? autoTrailingSlash}
45
+ />
28
46
  </Suspense>
29
47
  )
30
48
  }