Skip to content

Commit 1ff0dab

Browse files
authored
feat: add /visual-editing and /live exports, root exports deprecated (#2766)
1 parent 4e7df1f commit 1ff0dab

File tree

14 files changed

+182
-27
lines changed

14 files changed

+182
-27
lines changed

apps/mvp/app/(website)/live.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {defineLive} from 'next-sanity'
1+
import {defineLive} from 'next-sanity/live'
22

33
import {client} from '@/app/sanity.client'
44

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Migrate
2+
3+
### The `VisualEditing` export has been moved to `next-sanity/visual-editing`
4+
5+
The export has been moved to support `output: 'static'` builds. `VisualEditing` makes use of Server Actions, and thus it cannot be exposed to the root `'next-sanity'` export without blocking the ability to use features like `import {defineQuery} from 'next-sanity'` on static builds.
6+
7+
```diff
8+
// src/app/layout.tsx
9+
10+
-import {VisualEditing} from 'next-sanity'
11+
+import {VisualEditing} from 'next-sanity/visual-editing'
12+
import {SanityLive} from '@/sanity/lib/live'
13+
14+
export default function RootLayout({children}: {children: React.ReactNode}) {
15+
return (
16+
<html lang="en">
17+
<body>
18+
{children}
19+
<SanityLive />
20+
{(await draftMode()).isEnabled && <VisualEditing />}
21+
</body>
22+
</html>
23+
)
24+
}
25+
```
26+
27+
### The `defineLive` export has been moved to `next-sanity/live`
28+
29+
The export has been moved to support `output: 'static'` builds. `defineLive` makes use of Server Actions, and thus it cannot be exposed to the root `'next-sanity'` export without blocking the ability to use features like `import {defineQuery} from 'next-sanity'` on static builds.
30+
31+
```diff
32+
// src/sanity/lib/live.ts
33+
34+
import {createClient} from 'next-sanity'
35+
-import {defineLive} from 'next-sanity'
36+
+import {defineLive} from 'next-sanity/live'
37+
38+
const client = createClient({
39+
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
40+
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
41+
useCdn: true,
42+
apiVersion: 'v2025-03-04',
43+
stega: {studioUrl: '/studio'},
44+
})
45+
46+
const token = process.env.SANITY_API_READ_TOKEN
47+
if (!token) {
48+
throw new Error('Missing SANITY_API_READ_TOKEN')
49+
}
50+
51+
export const {sanityFetch, SanityLive} = defineLive({
52+
client,
53+
serverToken: token,
54+
browserToken: token,
55+
})
56+
```

packages/next-sanity/MIGRATE-v9-to-v10.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Here's an example migration, [based on the setup guide for live preview in v9](h
1919
Create a `lib/sanity.client.ts` file with the following content:
2020

2121
```ts
22-
import {defineLive} from 'next-sanity'
22+
import {defineLive} from 'next-sanity/live'
2323

2424
import {client} from './sanity.client'
2525

@@ -106,7 +106,7 @@ Here's an example migration, [based on the setup guide for live preview in v9](h
106106
Create a `lib/sanity.client.ts` file with the following content:
107107

108108
```ts
109-
import {defineLive} from 'next-sanity'
109+
import {defineLive} from 'next-sanity/live'
110110

111111
import {client} from './sanity.client'
112112

packages/next-sanity/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,8 @@ Use `defineLive` to enable automatic revalidation and refreshing of your fetched
606606
```tsx
607607
// src/sanity/lib/live.ts
608608

609-
import {createClient, defineLive} from 'next-sanity'
609+
import {createClient} from 'next-sanity'
610+
import {defineLive} from 'next-sanity/live'
610611

611612
const client = createClient({
612613
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
@@ -640,7 +641,7 @@ The same token can be used as both `browserToken` and `serverToken`, as the `bro
640641
```tsx
641642
// src/app/layout.tsx
642643

643-
import {VisualEditing} from 'next-sanity'
644+
import {VisualEditing} from 'next-sanity/visual-editing'
644645
import {SanityLive} from '@/sanity/lib/live'
645646

646647
export default function RootLayout({children}: {children: React.ReactNode}) {

packages/next-sanity/package.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next-sanity",
3-
"version": "10.0.16",
3+
"version": "10.0.17-canary.1",
44
"description": "Sanity.io toolkit for Next.js",
55
"keywords": [
66
"sanity",
@@ -42,6 +42,10 @@
4242
"source": "./src/image/index.ts",
4343
"default": "./dist/image.js"
4444
},
45+
"./live": {
46+
"source": "./src/live.ts",
47+
"default": "./dist/live.js"
48+
},
4549
"./studio": {
4650
"source": "./src/studio/index.ts",
4751
"default": "./dist/studio.js"
@@ -50,6 +54,10 @@
5054
"source": "./src/studio/client-component/index.ts",
5155
"default": "./dist/studio/client-component.js"
5256
},
57+
"./visual-editing": {
58+
"source": "./src/visual-editing/index.ts",
59+
"default": "./dist/visual-editing.js"
60+
},
5361
"./visual-editing/client-component": {
5462
"source": "./src/visual-editing/client-component/index.ts",
5563
"default": "./dist/visual-editing/client-component.js"
@@ -74,6 +82,9 @@
7482
"hooks": [
7583
"./dist/hooks.d.ts"
7684
],
85+
"live": [
86+
"./dist/live.d.ts"
87+
],
7788
"image": [
7889
"./dist/image.d.ts"
7990
],
@@ -83,6 +94,9 @@
8394
"studio/client-component": [
8495
"./dist/studio/client-component.d.ts"
8596
],
97+
"visual-editing": [
98+
"./dist/visual-editing.d.ts"
99+
],
86100
"visual-editing/client-component": [
87101
"./dist/visual-editing/client-component.d.ts"
88102
],
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
type CorsOriginError,
3+
type DefineSanityLiveOptions,
4+
type DefinedSanityFetchType,
5+
type DefinedSanityLiveProps,
6+
type DefinedSanityLiveStreamType,
7+
isCorsOriginError,
8+
defineLive as defineLiveImplementation,
9+
} from '@sanity/next-loader'
10+
11+
export {
12+
type CorsOriginError,
13+
DefineSanityLiveOptions,
14+
DefinedSanityFetchType,
15+
DefinedSanityLiveProps,
16+
DefinedSanityLiveStreamType,
17+
isCorsOriginError,
18+
}
19+
20+
let warned = false
21+
/**
22+
* @deprecated import `defineLive` from `next-sanity/live` instead
23+
*/
24+
export function defineLive(options: DefineSanityLiveOptions) {
25+
if (!warned) {
26+
console.warn(
27+
`Importing defineLive from the root import is deprecated and will be removed in next-sanity v11. Please change "import {defineLive} from 'next-sanity'" to "import {definveLive} from 'next-sanity/live'"`,
28+
)
29+
warned = true
30+
}
31+
return defineLiveImplementation(options)
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
type VisualEditingProps,
3+
VisualEditing as VisualEditingComponent,
4+
} from './visual-editing/VisualEditing'
5+
export type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'
6+
7+
let warned = false
8+
/**
9+
* @deprecated import `VisualEditing` from `next-sanity/visual-editing` instead
10+
*/
11+
export function VisualEditing(props: VisualEditingProps): React.ReactNode {
12+
if (!warned) {
13+
console.warn(
14+
`Importing VisualEditing from the root import is deprecated and will be removed in next-sanity v11. Please change "import {VisualEditing} from 'next-sanity'" to "import {VisualEditing} from 'next-sanity/visual-editing'"`,
15+
)
16+
warned = true
17+
}
18+
return <VisualEditingComponent {...props} />
19+
}

packages/next-sanity/src/index.edge-light.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
* Some of the exports on index.ts causes errors on the edge runtime, so we omit them here.
33
*/
44

5-
import type {VisualEditingProps} from './visual-editing'
6-
75
export * from './client'
86
export * from './create-data-attribute'
7+
export * from './deprecated-live'
98
export * from '@portabletext/react'
10-
export * from '@sanity/next-loader'
119
export {defineQuery, default as groq} from 'groq'
10+
import type {VisualEditingProps} from './visual-editing/VisualEditing'
1211
export type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'
1312

14-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
13+
/**
14+
* @deprecated import `VisualEditing` from `next-sanity/visual-editing` instead
15+
*/
1516
export function VisualEditing(_props: VisualEditingProps): React.ReactNode {
1617
throw new TypeError('VisualEditing is not supported on the edge runtime')
1718
}

packages/next-sanity/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './client'
22
export * from './create-data-attribute'
3-
export * from './visual-editing'
3+
export * from './deprecated-visual-editing'
4+
export * from './deprecated-live'
45
export * from '@portabletext/react'
5-
export * from '@sanity/next-loader'
66
export {defineQuery, default as groq} from 'groq'

packages/next-sanity/src/live.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '@sanity/next-loader'

0 commit comments

Comments
 (0)