next-sanity 0.0.0-dev.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/LICENSE +21 -0
- package/README.md +910 -0
- package/dist/_chunks/NextStudioLoading-8cf56cdf.js +127 -0
- package/dist/_chunks/NextStudioLoading-8cf56cdf.js.map +1 -0
- package/dist/_chunks/NextStudioLoading-bf57e61a.cjs +131 -0
- package/dist/_chunks/NextStudioLoading-bf57e61a.cjs.map +1 -0
- package/dist/index.cjs +26 -0
- package/dist/index.cjs.js +6 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/preview.cjs +19 -0
- package/dist/preview.cjs.js +6 -0
- package/dist/preview.cjs.map +1 -0
- package/dist/preview.d.ts +20 -0
- package/dist/preview.js +2 -0
- package/dist/preview.js.map +1 -0
- package/dist/studio/head.cjs +51 -0
- package/dist/studio/head.cjs.js +5 -0
- package/dist/studio/head.cjs.map +1 -0
- package/dist/studio/head.d.ts +79 -0
- package/dist/studio/head.js +46 -0
- package/dist/studio/head.js.map +1 -0
- package/dist/studio/index.cjs +111 -0
- package/dist/studio/index.cjs.js +10 -0
- package/dist/studio/index.cjs.map +1 -0
- package/dist/studio/index.d.ts +88 -0
- package/dist/studio/index.js +96 -0
- package/dist/studio/index.js.map +1 -0
- package/dist/studio/loading.cjs +8 -0
- package/dist/studio/loading.cjs.js +5 -0
- package/dist/studio/loading.cjs.map +1 -0
- package/dist/studio/loading.d.ts +23 -0
- package/dist/studio/loading.js +2 -0
- package/dist/studio/loading.js.map +1 -0
- package/dist/webhook.cjs +54 -0
- package/dist/webhook.cjs.js +6 -0
- package/dist/webhook.cjs.map +1 -0
- package/dist/webhook.d.ts +31 -0
- package/dist/webhook.js +42 -0
- package/dist/webhook.js.map +1 -0
- package/package.json +207 -0
- package/src/client.ts +2 -0
- package/src/index.ts +3 -0
- package/src/preview/definePreview.tsx +37 -0
- package/src/preview/index.ts +2 -0
- package/src/studio/NextStudio.tsx +61 -0
- package/src/studio/NextStudioClientOnly.tsx +15 -0
- package/src/studio/NextStudioLayout.tsx +46 -0
- package/src/studio/NextStudioLoading.tsx +120 -0
- package/src/studio/NextStudioNoScript.tsx +40 -0
- package/src/studio/head/NextStudioHead.tsx +112 -0
- package/src/studio/head/apple-touch-icon.png +0 -0
- package/src/studio/head/favicon.ico +0 -0
- package/src/studio/head/favicon.svg +7 -0
- package/src/studio/head/index.ts +1 -0
- package/src/studio/index.ts +6 -0
- package/src/studio/loading.ts +1 -0
- package/src/studio/usePrefersColorScheme.ts +30 -0
- package/src/studio/useTheme.ts +13 -0
- package/src/webhook/config.ts +18 -0
- package/src/webhook/index.ts +2 -0
- package/src/webhook/parseBody.ts +44 -0
- package/src/webhook/readBody.ts +10 -0
package/README.md
ADDED
|
@@ -0,0 +1,910 @@
|
|
|
1
|
+
# next-sanity<!-- omit in toc -->
|
|
2
|
+
|
|
3
|
+
[Sanity.io](https://www.sanity.io/?utm_source=github&utm_medium=readme&utm_campaign=next-sanity) toolkit for Next.js.
|
|
4
|
+
|
|
5
|
+
**Features:**
|
|
6
|
+
|
|
7
|
+
- [Client-side live real-time preview for authenticated users](#live-real-time-preview)
|
|
8
|
+
- [GROQ syntax highlighting](https://marketplace.visualstudio.com/items?itemName=sanity-io.vscode-sanity)
|
|
9
|
+
- [Embed](#next-sanitystudio) [Studio v3](https://www.sanity.io/studio-v3) in [Next.js](https://nextjs.org/) apps
|
|
10
|
+
|
|
11
|
+
## Table of contents
|
|
12
|
+
|
|
13
|
+
- [Table of contents](#table-of-contents)
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [`next-sanity` Running groq queries](#next-sanity-running-groq-queries)
|
|
16
|
+
- [`appDir`, React Server Components and caching](#appdir-react-server-components-and-caching)
|
|
17
|
+
- [`next-sanity/preview` Live real-time preview](#next-sanitypreview-live-real-time-preview)
|
|
18
|
+
- [Examples](#examples)
|
|
19
|
+
- [Built-in Sanity auth](#built-in-sanity-auth)
|
|
20
|
+
- [Using the `/pages` directory](#using-the-pages-directory)
|
|
21
|
+
- [Using the `/app` directory (experimental)](#using-the-app-directory-experimental)
|
|
22
|
+
- [Custom token auth](#custom-token-auth)
|
|
23
|
+
- [Using the `/pages` directory](#using-the-pages-directory-1)
|
|
24
|
+
- [Using the `/app` directory (experimental)](#using-the-app-directory-experimental-1)
|
|
25
|
+
- [Starters](#starters)
|
|
26
|
+
- [Limits](#limits)
|
|
27
|
+
- [`next-sanity/studio`](#next-sanitystudio)
|
|
28
|
+
- [Usage](#usage)
|
|
29
|
+
- [Using the `/app` directory (experimental)](#using-the-app-directory-experimental-2)
|
|
30
|
+
- [Using the `/pages` directory](#using-the-pages-directory-2)
|
|
31
|
+
- [Opt-in to using `StudioProvider` and `StudioLayout`](#opt-in-to-using-studioprovider-and-studiolayout)
|
|
32
|
+
- [`next-sanity/webhook`](#next-sanitywebhook)
|
|
33
|
+
- [Migrate](#migrate)
|
|
34
|
+
- [From `v2`](#from-v2)
|
|
35
|
+
- [`NextStudioGlobalStyle` is removed](#nextstudioglobalstyle-is-removed)
|
|
36
|
+
- [`ServerStyleSheetDocument` is removed](#serverstylesheetdocument-is-removed)
|
|
37
|
+
- [The internal `isWorkspaceWithTheme` and `isWorkspaces` utils are no longer exported](#the-internal-isworkspacewiththeme-and-isworkspaces-utils-are-no-longer-exported)
|
|
38
|
+
- [The `useBackgroundColorsFromTheme`, `useBasePath`, `useConfigWithBasePath`, and `useTextFontFamilyFromTheme`, hooks are removed](#the-usebackgroundcolorsfromtheme-usebasepath-useconfigwithbasepath-and-usetextfontfamilyfromtheme-hooks-are-removed)
|
|
39
|
+
- [The `NextStudioHead` component has moved from `next-sanity/studio` to `next-sanity/studio/head`](#the-nextstudiohead-component-has-moved-from-next-sanitystudio-to-next-sanitystudiohead)
|
|
40
|
+
- [From `v1`](#from-v1)
|
|
41
|
+
- [`createPreviewSubscriptionHook` is replaced with `definePreview`](#createpreviewsubscriptionhook-is-replaced-with-definepreview)
|
|
42
|
+
- [Before](#before)
|
|
43
|
+
- [After](#after)
|
|
44
|
+
- [`createCurrentUserHook` is removed](#createcurrentuserhook-is-removed)
|
|
45
|
+
- [From `v0.4`](#from-v04)
|
|
46
|
+
- [`createPortableTextComponent` is removed](#createportabletextcomponent-is-removed)
|
|
47
|
+
- [`createImageUrlBuilder` is removed](#createimageurlbuilder-is-removed)
|
|
48
|
+
- [Release new version](#release-new-version)
|
|
49
|
+
- [License](#license)
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install next-sanity @portabletext/react @sanity/image-url
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
yarn add next-sanity @portabletext/react @sanity/image-url
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pnpm install next-sanity @portabletext/react @sanity/image-url
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### `next-sanity/studio` peer dependencies
|
|
66
|
+
|
|
67
|
+
When using `npm` newer than `v7` you should end up with needed dependencies like `sanity` and `styled-components` when you `npm install next-sanity`. For other package managers you may need to do some extra steps.
|
|
68
|
+
|
|
69
|
+
#### Yarn
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npx install-peerdeps --yarn next-sanity
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### pnpm
|
|
76
|
+
|
|
77
|
+
You can either setup [`auto-install-peers`](https://stackoverflow.com/questions/72468635/pnpm-peer-dependencies-auto-install/74835069#74835069) and `pnpm install next-sanity` is enough, or:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
npx install-peerdeps --pnpm next-sanity
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## `next-sanity` Running groq queries
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import {createClient, groq} from 'next-sanity'
|
|
87
|
+
|
|
88
|
+
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID // "pv8y60vp"
|
|
89
|
+
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET // "production"
|
|
90
|
+
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION // "2022-11-16"
|
|
91
|
+
|
|
92
|
+
const client = createClient({
|
|
93
|
+
projectId,
|
|
94
|
+
dataset,
|
|
95
|
+
apiVersion, // https://www.sanity.io/docs/api-versioning
|
|
96
|
+
useCdn: true, // if you're using ISR or only static generation at build time then you can set this to `false` to guarantee no stale content
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
const data = await client.fetch(groq`*[]`)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### `appDir`, React Server Components and caching
|
|
103
|
+
|
|
104
|
+
As `@sanity/client` will only sometimes use `fetch` under the hood, it depends on the environment, [it's best to implement the cache function to ensure reliable caching.](https://beta.nextjs.org/docs/data-fetching/caching#per-request-cachingmd)
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import {createClient, groq} from 'next-sanity'
|
|
108
|
+
import {cache} from 'react'
|
|
109
|
+
|
|
110
|
+
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID // "pv8y60vp"
|
|
111
|
+
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET // "production"
|
|
112
|
+
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION // "2022-11-16"
|
|
113
|
+
|
|
114
|
+
const client = createClient({
|
|
115
|
+
projectId,
|
|
116
|
+
dataset,
|
|
117
|
+
apiVersion, // https://www.sanity.io/docs/api-versioning
|
|
118
|
+
useCdn: true, // if you're using ISR or only static generation at build time then you can set this to `false` to guarantee no stale content
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
// Wrap the cache function in a way that reuses the TypeScript definitions
|
|
122
|
+
const clientFetch = cache(client.fetch.bind(client))
|
|
123
|
+
|
|
124
|
+
// Now use it just like before, fully deduped, cached and optimized by react
|
|
125
|
+
const data = await clientFetch(groq`*[]`)
|
|
126
|
+
// You can use the same generics as before
|
|
127
|
+
const total = await clientFetch<number>(groq`count*()`)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## `next-sanity/preview` Live real-time preview
|
|
131
|
+
|
|
132
|
+
You can implement real-time client side preview using `definePreview`. It works by streaming the whole dataset to the browser, which it keeps updated using [listeners](https://www.sanity.io/docs/realtime-updates) and Mendoza patches. When it receives updates, then the query is run against the client-side datastore using [groq-js](https://github.com/sanity-io/groq-js).
|
|
133
|
+
It uses [`@sanity/preview-kit`](https://github.com/sanity-io/preview-kit) under the hood, which can be used in frameworks other than Nextjs if it supports React 18 Suspense APIs.
|
|
134
|
+
|
|
135
|
+
### Examples
|
|
136
|
+
|
|
137
|
+
When running `next dev` locally these examples start and exit preview mode by opening [localhost:3000/api/preview](http://localhost:3000/api/preview) and [localhost:3000/api/exit-preview](http://localhost:3000/api/exit-preview).
|
|
138
|
+
|
|
139
|
+
#### Built-in Sanity auth
|
|
140
|
+
|
|
141
|
+
Pros:
|
|
142
|
+
|
|
143
|
+
- Checks if the user is authenticated for you.
|
|
144
|
+
- Pairs well with Sanity Studio preview panes.
|
|
145
|
+
|
|
146
|
+
Cons:
|
|
147
|
+
|
|
148
|
+
- Doesn't implement a login flow:
|
|
149
|
+
- Requires the user to login to a Sanity Studio prior to starting Preview mode.
|
|
150
|
+
- Requires your Sanity Studio to be hosted on the same origin.
|
|
151
|
+
- Currently only supports cookie based auth, and not yet the `dual` [loginMethod in Sanity Studio](https://github.com/sanity-io/sanity/blob/9bf408d4cc8b3e14bac0bf94d3305d6960181d3c/packages/%40sanity/default-login/README.md?plain=1#L37):
|
|
152
|
+
- Safari based browsers (Desktop Safari on Macs, and all browsers on iOS) doesn't work.
|
|
153
|
+
- Doesn't support incognito browser modes.
|
|
154
|
+
|
|
155
|
+
`pages/api/preview.ts`:
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
export default function preview(req, res) {
|
|
159
|
+
res.setPreviewData({})
|
|
160
|
+
res.writeHead(307, {Location: '/'})
|
|
161
|
+
res.end()
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`pages/api/exit-preview.ts`:
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
export default function exit(req, res) {
|
|
169
|
+
res.clearPreviewData()
|
|
170
|
+
res.writeHead(307, {Location: '/'})
|
|
171
|
+
res.end()
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
`components/DocumentsCount.tsx`:
|
|
176
|
+
|
|
177
|
+
```tsx
|
|
178
|
+
import groq from 'groq'
|
|
179
|
+
|
|
180
|
+
export const query = groq`count(*[])`
|
|
181
|
+
|
|
182
|
+
export function DocumentsCount({data}) {
|
|
183
|
+
return (
|
|
184
|
+
<>
|
|
185
|
+
Documents: <strong>{data}</strong>
|
|
186
|
+
</>
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
`lib/sanity.client.ts`
|
|
192
|
+
|
|
193
|
+
```js
|
|
194
|
+
import {createClient} from 'next-sanity'
|
|
195
|
+
|
|
196
|
+
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID // "pv8y60vp"
|
|
197
|
+
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET // "production"
|
|
198
|
+
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION // "2022-11-16"
|
|
199
|
+
|
|
200
|
+
export const client = createClient({projectId, dataset, apiVersion})
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
`lib/sanity.preview.ts`
|
|
204
|
+
|
|
205
|
+
```js
|
|
206
|
+
import {definePreview} from 'next-sanity/preview'
|
|
207
|
+
import {projectId, dataset} from 'lib/sanity.client'
|
|
208
|
+
|
|
209
|
+
function onPublicAccessOnly() {
|
|
210
|
+
throw new Error(`Unable to load preview as you're not logged in`)
|
|
211
|
+
}
|
|
212
|
+
export const usePreview = definePreview({projectId, dataset, onPublicAccessOnly})
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
`components/PreviewDocumentsCount.ts`:
|
|
216
|
+
|
|
217
|
+
```tsx
|
|
218
|
+
'use client'
|
|
219
|
+
|
|
220
|
+
import {usePreview} from 'lib/sanity.preview'
|
|
221
|
+
import {query, DocumentsCount} from 'components/DocumentsCount'
|
|
222
|
+
|
|
223
|
+
export default function PreviewDocumentsCount() {
|
|
224
|
+
const data = usePreview(null, query)
|
|
225
|
+
return <DocumentsCount data={data} />
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
##### Using the `/pages` directory
|
|
230
|
+
|
|
231
|
+
`pages/index.tsx`:
|
|
232
|
+
|
|
233
|
+
```tsx
|
|
234
|
+
import {PreviewSuspense} from 'next-sanity/preview'
|
|
235
|
+
import {lazy} from 'react'
|
|
236
|
+
import {DocumentsCount, query} from 'components/DocumentsCount'
|
|
237
|
+
import {client} from 'lib/sanity.client'
|
|
238
|
+
|
|
239
|
+
const PreviewDocumentsCount = lazy(() => import('components/PreviewDocumentsCount'))
|
|
240
|
+
|
|
241
|
+
export const getStaticProps = async ({preview = false}) => {
|
|
242
|
+
if (preview) {
|
|
243
|
+
return {props: {preview}}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const data = await client.fetch(query)
|
|
247
|
+
|
|
248
|
+
return {props: {preview, data}}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export default function IndexPage({preview, data}) {
|
|
252
|
+
if (preview) {
|
|
253
|
+
return (
|
|
254
|
+
<PreviewSuspense fallback="Loading...">
|
|
255
|
+
<PreviewDocumentsCount />
|
|
256
|
+
</PreviewSuspense>
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return <DocumentsCount data={data} />
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
##### Using the `/app` directory (experimental)
|
|
265
|
+
|
|
266
|
+
We support the new `appDir` mode in Next, [but please note that `appDir` shouldn't be used in production before Vercel says it's stable](https://beta.nextjs.org/docs/getting-started).
|
|
267
|
+
|
|
268
|
+
`components/PreviewSuspense.tsx`:
|
|
269
|
+
|
|
270
|
+
```tsx
|
|
271
|
+
'use client'
|
|
272
|
+
|
|
273
|
+
// Once rollup supports 'use client' module directives then 'next-sanity' will include them and this re-export will no longer be necessary
|
|
274
|
+
export {PreviewSuspense as default} from 'next-sanity/preview'
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
`app/page.tsx`:
|
|
278
|
+
|
|
279
|
+
```tsx
|
|
280
|
+
import {previewData} from 'next/headers'
|
|
281
|
+
import PreviewSuspense from 'components/PreviewSuspense'
|
|
282
|
+
import {DocumentsCount, query} from 'components/DocumentsCount'
|
|
283
|
+
import PreviewDocumentsCount from 'components/PreviewDocumentsCount'
|
|
284
|
+
import {client} from 'lib/sanity.client'
|
|
285
|
+
import {cache} from 'react'
|
|
286
|
+
|
|
287
|
+
// Enable NextJS to cache and dedupe queries
|
|
288
|
+
const clientFetch = cache(client.fetch.bind(client))
|
|
289
|
+
|
|
290
|
+
export default async function IndexPage() {
|
|
291
|
+
if (previewData()) {
|
|
292
|
+
return (
|
|
293
|
+
<PreviewSuspense fallback="Loading...">
|
|
294
|
+
<PreviewDocumentsCount />
|
|
295
|
+
</PreviewSuspense>
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const data = await clientFetch(query)
|
|
300
|
+
return <DocumentsCount data={data} />
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
#### Custom token auth
|
|
305
|
+
|
|
306
|
+
By providing a read token (Sanity API token with `viewer` rights) you override the built-in auth and get more control and flexibility.
|
|
307
|
+
|
|
308
|
+
Pros:
|
|
309
|
+
|
|
310
|
+
- Allows launching previews for users without necessarily an Sanity account.
|
|
311
|
+
- Hosting a Sanity Studio on the same origin is optional.
|
|
312
|
+
- Can build preview experiences that start outside a Studio, like "Copy share link" functionality.
|
|
313
|
+
- Works in all Safari based browsers (Desktop Safari on Macs, and all browsers on iOS).
|
|
314
|
+
- Works with incognito browser modes.
|
|
315
|
+
|
|
316
|
+
Cons:
|
|
317
|
+
|
|
318
|
+
- Like all things with great power comes great responsibility. You're responsible for implementing adequate protection against leaking the `token` in your js bundle, or preventing the `/api/preview?secret=${secret}` from being easily guessable.
|
|
319
|
+
- It results in a larger JS bundle as `@sanity/groq-store` currently requires `event-source-polyfill` since native `window.EventSource` does not support setting `Authorization` headers needed for the token auth.
|
|
320
|
+
|
|
321
|
+
`pages/api/preview.ts`:
|
|
322
|
+
|
|
323
|
+
```js
|
|
324
|
+
import getSecret from 'lib/getSecret'
|
|
325
|
+
|
|
326
|
+
export default async function preview(req, res) {
|
|
327
|
+
// The secret can't be stored in an env variable with a NEXT_PUBLIC_ prefix, as it would make you vulnerable to leaking the token to anyone.
|
|
328
|
+
// If you don't have an custom API with authentication that can handle checking secrets, you may use https://github.com/sanity-io/sanity-studio-secrets to store the secret in your dataset.
|
|
329
|
+
const secret = await getSecret()
|
|
330
|
+
|
|
331
|
+
// This is the most common way to check for auth, but we encourage you to use your existing auth infra to protect your token and securely transmit it to the client
|
|
332
|
+
if (!req.query.secret || req.query.secret !== secret) {
|
|
333
|
+
return res.status(401).json({message: 'Invalid secret'})
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
res.setPreviewData({token: process.env.SANITY_API_READ_TOKEN})
|
|
337
|
+
res.writeHead(307, {Location: '/'})
|
|
338
|
+
res.end()
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
`pages/api/exit-preview.ts`:
|
|
343
|
+
|
|
344
|
+
```js
|
|
345
|
+
export default function exit(req, res) {
|
|
346
|
+
res.clearPreviewData()
|
|
347
|
+
res.writeHead(307, {Location: '/'})
|
|
348
|
+
res.end()
|
|
349
|
+
}
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
`components/DocumentsCount.tsx`:
|
|
353
|
+
|
|
354
|
+
```tsx
|
|
355
|
+
import groq from 'groq'
|
|
356
|
+
|
|
357
|
+
export const query = groq`count(*[])`
|
|
358
|
+
|
|
359
|
+
export function DocumentsCount({data}) {
|
|
360
|
+
return (
|
|
361
|
+
<>
|
|
362
|
+
Documents: <strong>{data}</strong>
|
|
363
|
+
</>
|
|
364
|
+
)
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
`lib/sanity.client.ts`
|
|
369
|
+
|
|
370
|
+
```js
|
|
371
|
+
import {createClient} from 'next-sanity'
|
|
372
|
+
|
|
373
|
+
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID // "pv8y60vp"
|
|
374
|
+
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET // "production"
|
|
375
|
+
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION // "2022-11-16"
|
|
376
|
+
|
|
377
|
+
export const client = createClient({projectId, dataset, apiVersion})
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
`lib/sanity.preview.ts`
|
|
381
|
+
|
|
382
|
+
```js
|
|
383
|
+
import {definePreview} from 'next-sanity/preview'
|
|
384
|
+
import {projectId, dataset} from 'lib/sanity.client'
|
|
385
|
+
|
|
386
|
+
export const usePreview = definePreview({projectId, dataset})
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
`components/PreviewDocumentsCount.tsx`:
|
|
390
|
+
|
|
391
|
+
```tsx
|
|
392
|
+
'use client'
|
|
393
|
+
|
|
394
|
+
import {usePreview} from 'lib/sanity.preview'
|
|
395
|
+
import {query, DocumentsCount} from 'components/DocumentsCount'
|
|
396
|
+
|
|
397
|
+
export default function PreviewDocumentsCount({token}) {
|
|
398
|
+
const data = usePreview(token, query)
|
|
399
|
+
return <DocumentsCount data={data} />
|
|
400
|
+
}
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
##### Using the `/pages` directory
|
|
404
|
+
|
|
405
|
+
`pages/index.tsx`:
|
|
406
|
+
|
|
407
|
+
```tsx
|
|
408
|
+
import {PreviewSuspense} from 'next-sanity/preview'
|
|
409
|
+
import {lazy} from 'react'
|
|
410
|
+
import {DocumentsCount, query} from 'components/DocumentsCount'
|
|
411
|
+
import {client} from 'lib/sanity.client'
|
|
412
|
+
|
|
413
|
+
// Wrapping preview components in React.lazy ensures visitors not on preview mode doesn't load any JS related to it
|
|
414
|
+
const PreviewDocumentsCount = lazy(() => import('components/PreviewDocumentsCount'))
|
|
415
|
+
|
|
416
|
+
export const getStaticProps = async ({preview = false, previewData = {}}) => {
|
|
417
|
+
if (preview && previewData?.token) {
|
|
418
|
+
return {props: {preview, token: previewData.token}}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const data = await client.fetch(query)
|
|
422
|
+
|
|
423
|
+
return {props: {preview, data}}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export default function IndexPage({preview, token, data}) {
|
|
427
|
+
if (preview) {
|
|
428
|
+
return (
|
|
429
|
+
<PreviewSuspense fallback="Loading...">
|
|
430
|
+
<PreviewDocumentsCount token={token} />
|
|
431
|
+
</PreviewSuspense>
|
|
432
|
+
)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
return <DocumentsCount data={data} />
|
|
436
|
+
}
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
##### Using the `/app` directory (experimental)
|
|
440
|
+
|
|
441
|
+
We support the new `appDir` mode in Next, [but please note that `appDir` shouldn't be used in production before Vercel says it's stable](https://beta.nextjs.org/docs/getting-started).
|
|
442
|
+
|
|
443
|
+
`components/PreviewSuspense.tsx`:
|
|
444
|
+
|
|
445
|
+
```tsx
|
|
446
|
+
'use client'
|
|
447
|
+
|
|
448
|
+
// Once rollup supports 'use client' module directives then 'next-sanity' will include them and this re-export will no longer be necessary
|
|
449
|
+
export {PreviewSuspense as default} from 'next-sanity/preview'
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
`app/page.tsx`:
|
|
453
|
+
|
|
454
|
+
```tsx
|
|
455
|
+
import {previewData} from 'next/headers'
|
|
456
|
+
import PreviewSuspense from 'components/PreviewSuspense'
|
|
457
|
+
import {DocumentsCount, query} from 'components/DocumentsCount'
|
|
458
|
+
import PreviewDocumentsCount from 'components/PreviewDocumentsCount'
|
|
459
|
+
import {client} from 'lib/sanity.client'
|
|
460
|
+
|
|
461
|
+
type AppPreviewData = {token: string} | undefined
|
|
462
|
+
export default async function IndexPage() {
|
|
463
|
+
if ((previewData() as AppPreviewData)?.token) {
|
|
464
|
+
return (
|
|
465
|
+
<PreviewSuspense fallback="Loading...">
|
|
466
|
+
<PreviewDocumentsCount token={(previewData() as AppPreviewData).token} />
|
|
467
|
+
</PreviewSuspense>
|
|
468
|
+
)
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const data = await client.fetch(query)
|
|
472
|
+
return <DocumentsCount data={data} />
|
|
473
|
+
}
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### Starters
|
|
477
|
+
|
|
478
|
+
- [A Next.js Blog with a Native Authoring Experience](https://github.com/sanity-io/nextjs-blog-cms-sanity-v3)
|
|
479
|
+
- [A multi-country ecommerce template built with Commerce Layer and Next.js](https://github.com/commercelayer/commercelayer-sanity-template)
|
|
480
|
+
|
|
481
|
+
### Limits
|
|
482
|
+
|
|
483
|
+
The real-time preview isn't optimized and comes with a configured limit of 3000 documents. You can experiment with larger datasets by configuring the hook with `documentLimit: <Integer>`. Be aware that this might significantly affect the preview performance.
|
|
484
|
+
You may use the `includeTypes` option to reduce the amount of documents and reduce the risk of hitting the `documentLimit`:
|
|
485
|
+
|
|
486
|
+
```js
|
|
487
|
+
import {definePreview} from 'next-sanity/preview'
|
|
488
|
+
|
|
489
|
+
export const usePreview = definePreview({
|
|
490
|
+
projectId,
|
|
491
|
+
dataset,
|
|
492
|
+
documentLimit: 10000,
|
|
493
|
+
includeTypes: ['page', 'product', 'sanity.imageAsset'],
|
|
494
|
+
// If you have a lot of editors changing content at the same time it might help to increase this value
|
|
495
|
+
// to reduce the amount of rerenders React have to perform.
|
|
496
|
+
subscriptionThrottleMs: 300,
|
|
497
|
+
})
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
We have plans for optimizations in the roadmap.
|
|
501
|
+
|
|
502
|
+
## `next-sanity/studio`
|
|
503
|
+
|
|
504
|
+
> [See it live](https://next.sanity.build/studio)
|
|
505
|
+
|
|
506
|
+
The latest version of Sanity Studio allows you to embed a near-infinitely configurable content editing interface into any React application. This opens up many possibilities:
|
|
507
|
+
|
|
508
|
+
- Any service that hosts Next.js apps can now host your Studio.
|
|
509
|
+
- Building previews for your content is easier as your Studio lives in the same environment.
|
|
510
|
+
- Use [Data Fetching](https://nextjs.org/docs/basic-features/data-fetching/overview) to configure your Studio.
|
|
511
|
+
- Easy setup of [Preview Mode](https://nextjs.org/docs/advanced-features/preview-mode).
|
|
512
|
+
|
|
513
|
+
### Usage
|
|
514
|
+
|
|
515
|
+
The basic setup is 2 components, `NextStudio` and `NextStudioHead`.
|
|
516
|
+
`NextStudio` loads up the `import {Studio} from 'sanity'` component for you and wraps it in a Next-friendly layout.
|
|
517
|
+
While `NextStudioHead` sets necessary `<head>` meta tags such as `<meta name="viewport">` to ensure the responsive CSS in the Studio works as expected.
|
|
518
|
+
|
|
519
|
+
Both the Next `/app` and `/pages` examples uses this config file:
|
|
520
|
+
`sanity.config.ts`:
|
|
521
|
+
|
|
522
|
+
```ts
|
|
523
|
+
import {visionTool} from '@sanity/vision'
|
|
524
|
+
import {defineConfig} from 'sanity'
|
|
525
|
+
import {deskTool} from 'sanity/desk'
|
|
526
|
+
|
|
527
|
+
import {schemaTypes} from './schemas'
|
|
528
|
+
|
|
529
|
+
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!
|
|
530
|
+
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET!
|
|
531
|
+
|
|
532
|
+
export default defineConfig({
|
|
533
|
+
basePath: '/studio', // <-- important that `basePath` matches the route you're mounting your studio from, it applies to both `/pages` and `/app`
|
|
534
|
+
|
|
535
|
+
projectId,
|
|
536
|
+
dataset,
|
|
537
|
+
|
|
538
|
+
plugins: [deskTool(), visionTool()],
|
|
539
|
+
|
|
540
|
+
schema: {
|
|
541
|
+
types: schemaTypes,
|
|
542
|
+
},
|
|
543
|
+
})
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
To use `sanity.cli.ts` with the same `projectId` and `dataset` as your `sanity.config.ts`:
|
|
547
|
+
|
|
548
|
+
```ts
|
|
549
|
+
/* eslint-disable no-process-env */
|
|
550
|
+
import {loadEnvConfig} from '@next/env'
|
|
551
|
+
import {defineCliConfig} from 'sanity/cli'
|
|
552
|
+
|
|
553
|
+
const dev = process.env.NODE_ENV !== 'production'
|
|
554
|
+
loadEnvConfig(__dirname, dev, {info: () => null, error: console.error})
|
|
555
|
+
|
|
556
|
+
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
|
|
557
|
+
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET
|
|
558
|
+
|
|
559
|
+
export default defineCliConfig({api: {projectId, dataset}})
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
Now you can run commands like `npx sanity cors add`. See `npx sanity help` for a full list of what you can do.
|
|
563
|
+
|
|
564
|
+
#### Using the `/app` directory (experimental)
|
|
565
|
+
|
|
566
|
+
We support the new `appDir` mode in Next, [but please note that `appDir` shouldn't be used in production before Vercel says it's stable](https://beta.nextjs.org/docs/getting-started).
|
|
567
|
+
|
|
568
|
+
In Next 13's new `appDir` mode you use `page.tsx` to load `NextStudio`, and optionally (recommended, especially if you want great support for iPhones and other devices with display cutouts like "The Notch" or "Dynamic Island") export `NextStudioHead` in a `head.tsx`.
|
|
569
|
+
In routes that load `NextStudio` ensure you have `'use client'` at the top of your file.
|
|
570
|
+
|
|
571
|
+
`app/studio/[[...index]]/page.tsx`:
|
|
572
|
+
|
|
573
|
+
```tsx
|
|
574
|
+
'use client'
|
|
575
|
+
|
|
576
|
+
import {NextStudio} from 'next-sanity/studio'
|
|
577
|
+
|
|
578
|
+
import config from '../../../sanity.config'
|
|
579
|
+
|
|
580
|
+
export default function StudioPage() {
|
|
581
|
+
// Supports the same props as `import {Studio} from 'sanity'`, `config` is required
|
|
582
|
+
return <NextStudio config={config} />
|
|
583
|
+
}
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
Set the right `viewport` meta tag, favicons and mroe
|
|
587
|
+
`app/studio/[[...index]]/head.tsx`:
|
|
588
|
+
|
|
589
|
+
```tsx
|
|
590
|
+
// Re-export `NextStudioHead` as default if you're happy with the default behavior
|
|
591
|
+
export {NextStudioHead as default} from 'next-sanity/studio/head'
|
|
592
|
+
|
|
593
|
+
// To customize it, use it as a children component:
|
|
594
|
+
import {NextStudioHead} from 'next-sanity/studio/head'
|
|
595
|
+
|
|
596
|
+
export default function CustomStudioHead() {
|
|
597
|
+
return (
|
|
598
|
+
<>
|
|
599
|
+
<NextStudioHead favicons={false} />
|
|
600
|
+
<link
|
|
601
|
+
rel="icon"
|
|
602
|
+
type="image/png"
|
|
603
|
+
sizes="32x32"
|
|
604
|
+
href="https://www.sanity.io/static/images/favicons/favicon-32x32.png"
|
|
605
|
+
/>
|
|
606
|
+
</>
|
|
607
|
+
)
|
|
608
|
+
}
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
Improve the Studio loading experience by setting a `loading.tsx` route.
|
|
612
|
+
`app/studio/[[...index]]/loading.tsx`:
|
|
613
|
+
|
|
614
|
+
```tsx
|
|
615
|
+
'use client'
|
|
616
|
+
|
|
617
|
+
import config from '../../../sanity.config'
|
|
618
|
+
import {NextStudioLoading} from 'next-sanity/studio/loading'
|
|
619
|
+
|
|
620
|
+
export default function Loading() {
|
|
621
|
+
return <NextStudioLoading config={config} />
|
|
622
|
+
}
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
#### Using the `/pages` directory
|
|
626
|
+
|
|
627
|
+
Using just `NextStudio` gives you a fully working Sanity Studio v3. However we recommend also using `NextStudioHead` as it ensures CSS Media Queries that target mobile devices with display cutouts (for example iPhone's "The Notch" and "Dynamic Island") and other details.
|
|
628
|
+
|
|
629
|
+
`/pages/studio/[[...index]].tsx`:
|
|
630
|
+
|
|
631
|
+
```tsx
|
|
632
|
+
import Head from 'next/head'
|
|
633
|
+
import {NextStudio} from 'next-sanity/studio'
|
|
634
|
+
import {NextStudioHead} from 'next-sanity/studio/head'
|
|
635
|
+
|
|
636
|
+
import config from '../../sanity.config'
|
|
637
|
+
|
|
638
|
+
export default function StudioPage() {
|
|
639
|
+
return (
|
|
640
|
+
<>
|
|
641
|
+
<Head>
|
|
642
|
+
<NextStudioHead />
|
|
643
|
+
</Head>
|
|
644
|
+
<NextStudio config={config} />
|
|
645
|
+
</>
|
|
646
|
+
)
|
|
647
|
+
}
|
|
648
|
+
```
|
|
649
|
+
|
|
650
|
+
### Opt-in to using `StudioProvider` and `StudioLayout`
|
|
651
|
+
|
|
652
|
+
If you want to go lower level and have more control over the studio you can pass `StudioProvider` and `StudioLayout` from `sanity` as `children`:
|
|
653
|
+
|
|
654
|
+
```tsx
|
|
655
|
+
import {NextStudio} from 'next-sanity/studio'
|
|
656
|
+
import {StudioProvider, StudioLayout} from 'sanity'
|
|
657
|
+
|
|
658
|
+
import config from '../../../sanity.config'
|
|
659
|
+
|
|
660
|
+
function StudioPage() {
|
|
661
|
+
return (
|
|
662
|
+
<NextStudio config={config}>
|
|
663
|
+
<StudioProvider config={config}>
|
|
664
|
+
{/* Put components here and you'll have access to the same React hooks as Studio gives you when writing plugins */}
|
|
665
|
+
<StudioLayout />
|
|
666
|
+
</StudioProvider>
|
|
667
|
+
</NextStudio>
|
|
668
|
+
)
|
|
669
|
+
}
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
## `next-sanity/webhook`
|
|
673
|
+
|
|
674
|
+
Implements [`@sanity/webhook`](https://github.com/sanity-io/webhook-toolkit) to parse and verify that a [Webhook](https://www.sanity.io/docs/webhooks) is indeed coming from Sanity infrastructure.
|
|
675
|
+
|
|
676
|
+
`pages/api/revalidate`:
|
|
677
|
+
|
|
678
|
+
```ts
|
|
679
|
+
import type {NextApiRequest, NextApiResponse} from 'next'
|
|
680
|
+
import {parseBody} from 'next-sanity/webhook'
|
|
681
|
+
|
|
682
|
+
// Export the config from next-sanity to enable validating the request body signature properly
|
|
683
|
+
export {config} from 'next-sanity/webhook'
|
|
684
|
+
|
|
685
|
+
export default async function revalidate(req: NextApiRequest, res: NextApiResponse) {
|
|
686
|
+
try {
|
|
687
|
+
const {isValidSignature, body} = await parseBody(req, process.env.SANITY_REVALIDATE_SECRET)
|
|
688
|
+
|
|
689
|
+
if (!isValidSignature) {
|
|
690
|
+
const message = 'Invalid signature'
|
|
691
|
+
console.warn(message)
|
|
692
|
+
res.status(401).json({message})
|
|
693
|
+
return
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
const staleRoute = `/${body.slug.current}`
|
|
697
|
+
await res.revalidate(staleRoute)
|
|
698
|
+
const message = `Updated route: ${staleRoute}`
|
|
699
|
+
console.log(message)
|
|
700
|
+
return res.status(200).json({message})
|
|
701
|
+
} catch (err) {
|
|
702
|
+
console.error(err)
|
|
703
|
+
return res.status(500).json({message: err.message})
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
## Migrate
|
|
709
|
+
|
|
710
|
+
### From `v2`
|
|
711
|
+
|
|
712
|
+
The `v3` release only contains breaking changes on the `next-sanity/studio` imports. If you're only using `import {createClient, groq} from 'next-sanity'` or `import {definePreview, PreviewSuspense} from 'next-sanity/preview'` then there's no migration for you to do.
|
|
713
|
+
|
|
714
|
+
#### `NextStudioGlobalStyle` is removed
|
|
715
|
+
|
|
716
|
+
The layout is no longer using global CSS to set the Studio height. The switch to local CSS helps interop between Next `/pages` and `/app` layouts.
|
|
717
|
+
|
|
718
|
+
#### `ServerStyleSheetDocument` is removed
|
|
719
|
+
|
|
720
|
+
It's no longer necessary to setup `styled-components` SSR for the Studio to render correctly.
|
|
721
|
+
|
|
722
|
+
#### The internal `isWorkspaceWithTheme` and `isWorkspaces` utils are no longer exported
|
|
723
|
+
|
|
724
|
+
The `useTheme` hook is still available if you're building abstractions that need to know what the initial workspace theme variables are.
|
|
725
|
+
|
|
726
|
+
#### The `useBackgroundColorsFromTheme`, `useBasePath`, `useConfigWithBasePath`, and `useTextFontFamilyFromTheme`, hooks are removed
|
|
727
|
+
|
|
728
|
+
You can `useTheme` to replace `useBackgroundColorsFromTheme` and `useTextFontFamilyFromTheme`:
|
|
729
|
+
|
|
730
|
+
```tsx
|
|
731
|
+
import {useMemo} from 'react'
|
|
732
|
+
import {useTheme} from 'next-sanity/studio'
|
|
733
|
+
import type {StudioProps} from 'sanity'
|
|
734
|
+
export default function MyComponent(props: Pick<StudioProps, 'config'>) {
|
|
735
|
+
const theme = useTheme(config)
|
|
736
|
+
// useBackgroundColorsFromTheme
|
|
737
|
+
const {themeColorLight, themeColorDark} = useMemo(
|
|
738
|
+
() => ({
|
|
739
|
+
themeColorLight: theme.color.light.default.base.bg,
|
|
740
|
+
themeColorDark: theme.color.dark.default.base.bg,
|
|
741
|
+
}),
|
|
742
|
+
[theme]
|
|
743
|
+
)
|
|
744
|
+
// useTextFontFamilyFromTheme
|
|
745
|
+
const fontFamily = useMemo(() => theme.fonts.text.family, [theme])
|
|
746
|
+
}
|
|
747
|
+
```
|
|
748
|
+
|
|
749
|
+
The reason why `useBasePath` and `useConfigWithBasePath` got removed is because Next `/pages` and `/app` diverge too much in how they declare dynamic segments. Thus you'll need to specify `basePath` in your `sanity.config.ts` manually to match the route you're loading the studio, for the time being.
|
|
750
|
+
|
|
751
|
+
#### The `NextStudioHead` component has moved from `next-sanity/studio` to `next-sanity/studio/head`
|
|
752
|
+
|
|
753
|
+
Its props are also quite different and it now requires you to wrap it in `import Head from 'next/head'` if you're not using a `head.tsx` in `appDir`. Make sure you use TypeScript to ease the migration.
|
|
754
|
+
|
|
755
|
+
### From `v1`
|
|
756
|
+
|
|
757
|
+
#### `createPreviewSubscriptionHook` is replaced with `definePreview`
|
|
758
|
+
|
|
759
|
+
There are several differences between the hooks. First of all, `definePreview` requires React 18 and Suspense. And as it's designed to work with React Server Components you provide `token` in the hook itself instead of in the `definePreview` step. Secondly, `definePreview` encourages code-splitting using `React.lazy` and that means you only call the `usePreview` hook in a component that is lazy loaded. Quite different from `usePreviewSubscription` which was designed to be used in both preview mode, and in production by providing `initialData`.
|
|
760
|
+
|
|
761
|
+
##### Before
|
|
762
|
+
|
|
763
|
+
The files that are imported here are the same as the [Next `/pages` example](#using-the-pages-director).
|
|
764
|
+
|
|
765
|
+
`pages/index.tsx`
|
|
766
|
+
|
|
767
|
+
```tsx
|
|
768
|
+
import {createPreviewSubscriptionHook} from 'next-sanity'
|
|
769
|
+
import {DocumentsCount, query} from 'components/DocumentsCount'
|
|
770
|
+
import {client, projectId, dataset} from 'lib/sanity.client'
|
|
771
|
+
|
|
772
|
+
export const getStaticProps = async ({preview = false}) => {
|
|
773
|
+
const data = await client.fetch(query)
|
|
774
|
+
|
|
775
|
+
return {props: {preview, data}}
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const usePreviewSubscription = createPreviewSubscriptionHook({projectId, dataset})
|
|
779
|
+
export default function IndexPage({preview, data: initialData}) {
|
|
780
|
+
const {data} = usePreviewSubscription(indexQuery, {initialData, enabled: preview})
|
|
781
|
+
return <DocumentsCount data={data} />
|
|
782
|
+
}
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
##### After
|
|
786
|
+
|
|
787
|
+
`components/PreviewDocumentsCount.tsx`
|
|
788
|
+
|
|
789
|
+
```tsx
|
|
790
|
+
import {definePreview} from 'next-sanity/preview'
|
|
791
|
+
import {projectId, dataset} from 'lib/sanity.client'
|
|
792
|
+
|
|
793
|
+
const usePreview = definePreview({projectId, dataset})
|
|
794
|
+
export default function PreviewDocumentsCount() {
|
|
795
|
+
const data = usePreview(null, query)
|
|
796
|
+
return <DocumentsCount data={data} />
|
|
797
|
+
}
|
|
798
|
+
```
|
|
799
|
+
|
|
800
|
+
`pages/index.tsx`
|
|
801
|
+
|
|
802
|
+
```tsx
|
|
803
|
+
import {lazy} from 'react'
|
|
804
|
+
import {PreviewSuspense} from 'next-sanity/preview'
|
|
805
|
+
import {DocumentsCount, query} from 'components/DocumentsCount'
|
|
806
|
+
import {client} from 'lib/sanity.client'
|
|
807
|
+
|
|
808
|
+
const PreviewDocumentsCount = lazy(() => import('components/PreviewDocumentsCount'))
|
|
809
|
+
|
|
810
|
+
export const getStaticProps = async ({preview = false}) => {
|
|
811
|
+
const data = await client.fetch(query)
|
|
812
|
+
|
|
813
|
+
return {props: {preview, data}}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
export default function IndexPage({preview, data}) {
|
|
817
|
+
if (preview) {
|
|
818
|
+
return (
|
|
819
|
+
<PreviewSuspense fallback={<DocumentsCount data={data} />}>
|
|
820
|
+
<PreviewDocumentsCount />
|
|
821
|
+
</PreviewSuspense>
|
|
822
|
+
)
|
|
823
|
+
}
|
|
824
|
+
return <DocumentsCount data={data} />
|
|
825
|
+
}
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
#### `createCurrentUserHook` is removed
|
|
829
|
+
|
|
830
|
+
If you used this hook to check if the user is cookie authenticated:
|
|
831
|
+
|
|
832
|
+
```tsx
|
|
833
|
+
import {createCurrentUserHook} from 'next-sanity'
|
|
834
|
+
|
|
835
|
+
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
|
|
836
|
+
const useCurrentUser = createCurrentUserHook({projectId})
|
|
837
|
+
const useCheckAuth = () => {
|
|
838
|
+
const {data, loading} = useCurrentUser()
|
|
839
|
+
return loading ? false : !!data
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
export default function Page() {
|
|
843
|
+
const isAuthenticated = useCheckAuth()
|
|
844
|
+
}
|
|
845
|
+
```
|
|
846
|
+
|
|
847
|
+
Then you can achieve the same functionality using `@sanity/preview-kit` and `suspend-react`:
|
|
848
|
+
|
|
849
|
+
```tsx
|
|
850
|
+
import {suspend} from 'suspend-react'
|
|
851
|
+
import {_checkAuth} from '@sanity/preview-kit'
|
|
852
|
+
|
|
853
|
+
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
|
|
854
|
+
const useCheckAuth = () =>
|
|
855
|
+
suspend(() => _checkAuth(projectId, null), ['@sanity/preview-kit', 'checkAuth', projectId])
|
|
856
|
+
|
|
857
|
+
export default function Page() {
|
|
858
|
+
const isAuthenticated = useCheckAuth()
|
|
859
|
+
}
|
|
860
|
+
```
|
|
861
|
+
|
|
862
|
+
### From `v0.4`
|
|
863
|
+
|
|
864
|
+
#### `createPortableTextComponent` is removed
|
|
865
|
+
|
|
866
|
+
This utility used to wrap `@sanity/block-content-to-react`. It's encouraged to upgrade to `@portabletext/react`.
|
|
867
|
+
|
|
868
|
+
```sh
|
|
869
|
+
$ npm install @portabletext/react
|
|
870
|
+
// or
|
|
871
|
+
$ yarn add @portabletext/react
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
```diff
|
|
875
|
+
-import { createPortableTextComponent } from 'next-sanity'
|
|
876
|
+
+import { PortableText as PortableTextComponent } from '@portabletext/react'
|
|
877
|
+
|
|
878
|
+
-export const PortableText = createPortableTextComponent({ serializers: {} })
|
|
879
|
+
+export const PortableText = (props) => <PortableTextComponent components={{}} {...props} />
|
|
880
|
+
```
|
|
881
|
+
|
|
882
|
+
Please note that the `serializers` and `components` are not 100% equivalent.
|
|
883
|
+
|
|
884
|
+
[Check the full migration guide.](https://github.com/portabletext/react-portabletext/blob/main/MIGRATING.md)
|
|
885
|
+
|
|
886
|
+
#### `createImageUrlBuilder` is removed
|
|
887
|
+
|
|
888
|
+
This utility is no longer wrapped by `next-sanity` and you'll need to install the dependency yourself:
|
|
889
|
+
|
|
890
|
+
```sh
|
|
891
|
+
$ npm install @sanity/image-url
|
|
892
|
+
// or
|
|
893
|
+
$ yarn add @sanity/image-url
|
|
894
|
+
```
|
|
895
|
+
|
|
896
|
+
```diff
|
|
897
|
+
-import { createImageUrlBuilder } from 'next-sanity'
|
|
898
|
+
+import createImageUrlBuilder from '@sanity/image-url'
|
|
899
|
+
```
|
|
900
|
+
|
|
901
|
+
## Release new version
|
|
902
|
+
|
|
903
|
+
Run ["CI & Release" workflow](https://github.com/sanity-io/next-sanity/actions/workflows/ci.yml).
|
|
904
|
+
Make sure to select the main branch and check "Release new version".
|
|
905
|
+
|
|
906
|
+
Semantic release will only release on configured branches, so it is safe to run release on any branch.
|
|
907
|
+
|
|
908
|
+
## License
|
|
909
|
+
|
|
910
|
+
MIT-licensed. See [LICENSE](LICENSE).
|