hydrogen-sanity 4.0.2 → 4.0.3
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/README.md +29 -74
- package/dist/_chunks-cjs/VisualEditing.client.cjs +1 -0
- package/dist/_chunks-cjs/VisualEditing.client.cjs.map +1 -0
- package/dist/_chunks-es/VisualEditing.client.js +1 -0
- package/dist/_chunks-es/VisualEditing.client.js.map +1 -0
- package/dist/visual-editing/index.cjs +1 -1
- package/dist/visual-editing/index.cjs.map +1 -1
- package/dist/visual-editing/index.d.cts +0 -4
- package/dist/visual-editing/index.d.ts +0 -4
- package/dist/visual-editing/index.js +1 -1
- package/dist/visual-editing/index.js.map +1 -1
- package/package.json +1 -1
- package/src/visual-editing/VisualEditing.client.tsx +16 -0
- package/src/visual-editing/VisualEditing.tsx +21 -6
package/README.md
CHANGED
|
@@ -2,24 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
[Sanity.io](https://www.sanity.io) toolkit for [Hydrogen](https://hydrogen.shopify.dev/). Requires `@shopify/hydrogen >= 2023.7.0`.
|
|
4
4
|
|
|
5
|
-
- [
|
|
6
|
-
|
|
7
|
-
- [
|
|
8
|
-
|
|
9
|
-
- [
|
|
10
|
-
- [
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- [
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
- [
|
|
20
|
-
|
|
21
|
-
- [
|
|
22
|
-
- [Release new version](#release-new-version)
|
|
5
|
+
- [Installation](#installation)
|
|
6
|
+
- [Usage](#usage)
|
|
7
|
+
- [Satisfy TypeScript](#satisfy-typescript)
|
|
8
|
+
- [Interacting with Sanity data](#interacting-with-sanity-data)
|
|
9
|
+
- [Preferred: Cached fetches using `loadQuery`](#preferred-cached-fetches-using-loadquery)
|
|
10
|
+
- [`loadQuery` request options](#loadquery-request-options)
|
|
11
|
+
- [Alternatively: Using `client` directly](#alternatively-using-client-directly)
|
|
12
|
+
- [Visual Editing](#visual-editing)
|
|
13
|
+
- [Enabling preview mode](#enabling-preview-mode)
|
|
14
|
+
- [Setup CORS for front-end domains](#setup-cors-for-front-end-domains)
|
|
15
|
+
- [Modify Content Security Policy for Studio domains](#modify-content-security-policy-for-studio-domains)
|
|
16
|
+
- [Setup Presentation tool](#setup-presentation-tool)
|
|
17
|
+
- [Using `@sanity/client` instead of hydrogen-sanity](#using-sanityclient-instead-of-hydrogen-sanity)
|
|
18
|
+
- [Migration Guides](#migration-guides)
|
|
19
|
+
- [License](#license)
|
|
20
|
+
- [Develop \& test](#develop--test)
|
|
21
|
+
- [Release new version](#release-new-version)
|
|
23
22
|
|
|
24
23
|
**Features:**
|
|
25
24
|
|
|
@@ -135,11 +134,10 @@ SANITY_API_TOKEN=""
|
|
|
135
134
|
|
|
136
135
|
### Satisfy TypeScript
|
|
137
136
|
|
|
138
|
-
Update the environment variables in `Env`
|
|
137
|
+
Update the environment variables in `Env` to include the ones you created above:
|
|
139
138
|
|
|
140
139
|
```ts
|
|
141
140
|
// ./remix.env.d.ts
|
|
142
|
-
import type {Sanity} from 'hydrogen-sanity'
|
|
143
141
|
|
|
144
142
|
declare global {
|
|
145
143
|
// ...other Types
|
|
@@ -152,25 +150,21 @@ declare global {
|
|
|
152
150
|
SANITY_API_TOKEN: string
|
|
153
151
|
}
|
|
154
152
|
}
|
|
155
|
-
|
|
156
|
-
declare module '@shopify/remix-oxygen' {
|
|
157
|
-
export interface AppLoadContext {
|
|
158
|
-
// ...other Types
|
|
159
|
-
sanity: Sanity
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
153
|
```
|
|
163
154
|
|
|
155
|
+
> **Note**: `hydrogen-sanity` will automatically add `sanity` to the `AppLoadContext` interface
|
|
156
|
+
>
|
|
157
|
+
|
|
164
158
|
## Interacting with Sanity data
|
|
165
159
|
|
|
166
160
|
### Preferred: Cached fetches using `loadQuery`
|
|
167
161
|
|
|
168
|
-
Query Sanity's API and use Hydrogen's cache to store the response (defaults to `CacheLong` caching strategy).
|
|
169
|
-
|
|
170
|
-
`loadQuery` results will skip Hydrogen's caching when in preview mode.
|
|
162
|
+
Query Sanity's API and use Hydrogen's cache to store the response (defaults to `CacheLong` caching strategy). While in preview mode, `loadQuery` will use `CacheNone` to prevent results from being cached.
|
|
171
163
|
|
|
172
164
|
Learn more about configuring [caching in Hydrogen on the Shopify documentation](https://shopify.dev/docs/custom-storefronts/hydrogen/caching).
|
|
173
165
|
|
|
166
|
+
Sanity queries will appear in Hydrogen's [Subrequest Profiler](https://shopify.dev/docs/custom-storefronts/hydrogen/debugging/subrequest-profiler). By default, they're titled `Sanity query`; however, you can give your queries more descriptive titles by using the request option below.
|
|
167
|
+
|
|
174
168
|
```ts
|
|
175
169
|
export async function loader({context, params}: LoaderFunctionArgs) {
|
|
176
170
|
const query = `*[_type == "page" && _id == $id][0]`
|
|
@@ -198,23 +192,13 @@ const page = await context.sanity.loadQuery<HomePage>(query, params, {
|
|
|
198
192
|
// debug: {
|
|
199
193
|
// displayName: 'query Homepage'
|
|
200
194
|
// }
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// These additional options will be passed to sanity.loadQuery
|
|
204
|
-
queryOptions: {
|
|
205
|
-
tag: 'home',
|
|
206
|
-
headers: {
|
|
207
|
-
'Accept-Encoding': 'br, gzip, *',
|
|
208
|
-
},
|
|
209
|
-
},
|
|
195
|
+
}
|
|
210
196
|
})
|
|
211
197
|
```
|
|
212
198
|
|
|
213
199
|
### Alternatively: Using `client` directly
|
|
214
200
|
|
|
215
|
-
The Sanity
|
|
216
|
-
|
|
217
|
-
Sanity Client can be used for mutations within actions, for example:
|
|
201
|
+
The Sanity client (either instantiated from your configuration or passed through directly) is also available in your app's context. It is recommended to use `loadQuery` for data fetching; but the Sanity client can be used for mutations within actions, for example:
|
|
218
202
|
|
|
219
203
|
```ts
|
|
220
204
|
export async function action({context, request}: ActionFunctionArgs) {
|
|
@@ -235,10 +219,9 @@ export async function action({context, request}: ActionFunctionArgs) {
|
|
|
235
219
|
|
|
236
220
|
## Visual Editing
|
|
237
221
|
|
|
238
|
-
Enable real-time, interactive live preview inside the Presentation
|
|
222
|
+
Enable real-time, interactive live preview inside the Presentation tool of your Sanity Studio. `hydrogen-sanity` comes with a ready-to-use version of the `VisualEditing` component that's compatible with Hydrogen and Oxygen.
|
|
239
223
|
|
|
240
224
|
First set up your root route to enable preview mode across the entire application, if the preview session is active:
|
|
241
|
-
|
|
242
225
|
```tsx
|
|
243
226
|
// ./app/root.tsx
|
|
244
227
|
|
|
@@ -273,41 +256,13 @@ export default function App() {
|
|
|
273
256
|
)
|
|
274
257
|
}
|
|
275
258
|
```
|
|
276
|
-
|
|
277
259
|
This Visual Editing component will trigger incremental updates to draft documents from the server for users with a valid preview session. [Duplicate its source](https://github.com/sanity-io/visual-editing/blob/main/packages/visual-editing/src/remix/VisualEditing.tsx) into your own project if you wish to customize its behavior.
|
|
278
260
|
|
|
279
|
-
These updates are faster when your initial server-side content is passed through an optional `useQuery` hook.
|
|
280
|
-
|
|
281
|
-
```tsx
|
|
282
|
-
// Any route file, such as ./app/routes/index.tsx
|
|
283
|
-
|
|
284
|
-
// ...all other imports
|
|
285
|
-
import {useQuery} from '@sanity/react-loader'
|
|
286
|
-
|
|
287
|
-
export async function loader({context, params}: LoaderArgs) {
|
|
288
|
-
const query = `*[_type == "page" && _id == $id][0]`
|
|
289
|
-
const params = {id: 'home'}
|
|
290
|
-
const initial = await context.sanity.loadQuery(query, params)
|
|
291
|
-
|
|
292
|
-
return json({initial, query, params})
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// Default export where content is rendered
|
|
296
|
-
export default function Index() {
|
|
297
|
-
// Get initial data, passing it as snapshot to render preview...
|
|
298
|
-
const {initial, query, params} = useLoaderData<typeof loader>()
|
|
299
|
-
// Optional, pass query, params and initial data to useQuery for faster updates
|
|
300
|
-
const {loading, data} = useQuery(query, params, initial)
|
|
301
|
-
|
|
302
|
-
return loading ? <div>Loading</div> : <Page page={data} />
|
|
303
|
-
}
|
|
304
|
-
```
|
|
305
|
-
|
|
306
261
|
### Enabling preview mode
|
|
307
262
|
|
|
308
263
|
For users to enter preview mode, they will need to visit a route that performs some authentication and then writes to the session.
|
|
309
264
|
|
|
310
|
-
`hydrogen-sanity` comes with a preconfigured route for this purpose. It checks the value of a secret in the URL used by Presentation
|
|
265
|
+
`hydrogen-sanity` comes with a preconfigured route for this purpose. It checks the value of a secret in the URL used by Presentation tool - and if valid - writes the `projectId` to the Hydrogen session.
|
|
311
266
|
|
|
312
267
|
Add this route to your project like below, or view the source to copy and modify it in your project.
|
|
313
268
|
|
|
@@ -365,7 +320,7 @@ export default defineConfig({
|
|
|
365
320
|
})
|
|
366
321
|
```
|
|
367
322
|
|
|
368
|
-
You should now be able to view your Hydrogen app in the Presentation
|
|
323
|
+
You should now be able to view your Hydrogen app in the Presentation tool, click to edit any Sanity content and see live updates as you make changes.
|
|
369
324
|
|
|
370
325
|
## Using `@sanity/client` instead of hydrogen-sanity
|
|
371
326
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("@sanity/visual-editing/remix");if(typeof document>"u")throw new Error("Visual editing should only run client-side. Please check that this file is not being imported into a worker or server bundle.");Object.defineProperty(exports,"default",{enumerable:!0,get:function(){return e.VisualEditing}});//# sourceMappingURL=VisualEditing.client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VisualEditing.client.cjs","sources":["../../src/visual-editing/VisualEditing.client.tsx"],"sourcesContent":["import {VisualEditing} from '@sanity/visual-editing/remix'\n\n/**\n * Prevent a consumer from importing into a worker/server bundle.\n */\nif (typeof document === 'undefined') {\n throw new Error(\n 'Visual editing should only run client-side. Please check that this file is not being imported into a worker or server bundle.',\n )\n}\n\n/**\n * Enables visual editing on the front-end\n * @see https://www.sanity.io/docs/introduction-to-visual-editing\n */\nexport default VisualEditing\n"],"names":["document","Error","Object","defineProperty","exports","enumerable","get","remix","VisualEditing"],"mappings":"2DAKA,UAAWA,SAAa,IACtB,MAAM,IAAIC,MACR,iIACFC,OAAAC,eAAAC,QAAA,UAAA,CAAAC,YAAA,EAAAC,IAAA,WAAA,OAAAC,EAAAC,aAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"@sanity/visual-editing/remix";import{VisualEditing as i}from"@sanity/visual-editing/remix";if(typeof document>"u")throw new Error("Visual editing should only run client-side. Please check that this file is not being imported into a worker or server bundle.");export{i as default};//# sourceMappingURL=VisualEditing.client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VisualEditing.client.js","sources":["../../src/visual-editing/VisualEditing.client.tsx"],"sourcesContent":["import {VisualEditing} from '@sanity/visual-editing/remix'\n\n/**\n * Prevent a consumer from importing into a worker/server bundle.\n */\nif (typeof document === 'undefined') {\n throw new Error(\n 'Visual editing should only run client-side. Please check that this file is not being imported into a worker or server bundle.',\n )\n}\n\n/**\n * Enables visual editing on the front-end\n * @see https://www.sanity.io/docs/introduction-to-visual-editing\n */\nexport default VisualEditing\n"],"names":["document","Error"],"mappings":"kGAKA,UAAWA,SAAa,IACtB,MAAM,IAAIC,MACR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react/jsx-runtime"),t=require("react"),r=require("@sanity/visual-editing/create-data-attribute");const i=typeof document>"u"?function(){return e.jsx(e.Fragment,{})}:t.lazy((()=>Promise.resolve().then((function(){return require("../_chunks-cjs/VisualEditing.client.cjs")}))));Object.defineProperty(exports,"createDataAttribute",{enumerable:!0,get:function(){return r.createDataAttribute}}),exports.VisualEditing=function(r){return e.jsx(t.Suspense,{children:e.jsx(i,{...r})})};//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/visual-editing/VisualEditing.tsx"],"sourcesContent":["import type {VisualEditingProps} from '@sanity/visual-editing/remix'\nimport {lazy, type ReactElement, Suspense} from 'react'\n\
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/visual-editing/VisualEditing.tsx"],"sourcesContent":["import type {VisualEditingProps} from '@sanity/visual-editing/remix'\nimport {lazy, type ReactElement, Suspense} from 'react'\n\n/**\n * Provide a consistent fallback to prevent hydration mismatch errors.\n */\nfunction VisualEditingFallback(): ReactElement {\n return <></>\n}\n\n/**\n * If server-side rendering, then return the fallback instead of the heavy dependency.\n * @see https://remix.run/docs/en/1.14.3/guides/constraints#browser-only-code-on-the-server\n */\nconst VisualEditingComponent =\n typeof document === 'undefined'\n ? VisualEditingFallback\n : lazy(\n () =>\n /**\n * `lazy` expects the component as the default export\n * @see https://react.dev/reference/react/lazy\n */\n import('./VisualEditing.client'),\n )\n\nexport function VisualEditing(props: VisualEditingProps): ReactElement {\n return (\n <Suspense>\n <VisualEditingComponent {...props} />\n </Suspense>\n )\n}\n"],"names":["Object","defineProperty","exports","value","jsxRuntime","require","react","createDataAttribute","VisualEditingComponent","document","jsx","Fragment","lazy","Promise","resolve","then","enumerable","get","VisualEditing","props","Suspense","children"],"mappings":"aAMAA,OAAAC,eAAAC,QAAA,aAAA,CAAAC,OAAA,IAAA,IAAAC,EAAAC,QAAA,qBAAAC,EAAAD,QAAA,SAAAE,EAAAF,QAAA,gDAQA,MAAMG,SACGC,SAAa,IATtB,WACE,OAASL,EAAAM,IAAAC,WAAA,CAAA,EACX,EASMC,EAAAA,MACE,IAKEC,QAAAC,UAAAC,MAAA,WAAA,OAAAV,QAAO,0CAAwB,MASzCL,OAAAC,eAAAC,QAAA,sBAAA,CAAAc,YAAA,EAAAC,IAAA,WAAA,OAAAV,EAAAA,mBAAA,IAAAL,QAAAgB,cANO,SAAuBC,GAC5B,aACGC,WACC,CAAAC,SAAAX,EAAAA,IAACF,EAAwB,IAAGW,KAGlC"}
|
|
@@ -10,10 +10,6 @@ export {createDataAttribute}
|
|
|
10
10
|
|
|
11
11
|
export {CreateDataAttributeProps}
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
* Enables visual editing on the front-end
|
|
15
|
-
* @see https://www.sanity.io/docs/introduction-to-visual-editing
|
|
16
|
-
*/
|
|
17
13
|
export declare function VisualEditing(props: VisualEditingProps): ReactElement
|
|
18
14
|
|
|
19
15
|
export {}
|
|
@@ -10,10 +10,6 @@ export {createDataAttribute}
|
|
|
10
10
|
|
|
11
11
|
export {CreateDataAttributeProps}
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
* Enables visual editing on the front-end
|
|
15
|
-
* @see https://www.sanity.io/docs/introduction-to-visual-editing
|
|
16
|
-
*/
|
|
17
13
|
export declare function VisualEditing(props: VisualEditingProps): ReactElement
|
|
18
14
|
|
|
19
15
|
export {}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t}from"react/jsx-runtime";import{lazy as i,Suspense as
|
|
1
|
+
import{jsx as t,Fragment as r}from"react/jsx-runtime";import{lazy as i,Suspense as e}from"react";import{createDataAttribute as n}from"@sanity/visual-editing/create-data-attribute";const o=typeof document>"u"?function(){return t(r,{})}:i((()=>import("../_chunks-es/VisualEditing.client.js")));function u(r){return t(e,{children:t(o,{...r})})}export{u as VisualEditing,n as createDataAttribute};//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/visual-editing/VisualEditing.tsx"],"sourcesContent":["import type {VisualEditingProps} from '@sanity/visual-editing/remix'\nimport {lazy, type ReactElement, Suspense} from 'react'\n\
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/visual-editing/VisualEditing.tsx"],"sourcesContent":["import type {VisualEditingProps} from '@sanity/visual-editing/remix'\nimport {lazy, type ReactElement, Suspense} from 'react'\n\n/**\n * Provide a consistent fallback to prevent hydration mismatch errors.\n */\nfunction VisualEditingFallback(): ReactElement {\n return <></>\n}\n\n/**\n * If server-side rendering, then return the fallback instead of the heavy dependency.\n * @see https://remix.run/docs/en/1.14.3/guides/constraints#browser-only-code-on-the-server\n */\nconst VisualEditingComponent =\n typeof document === 'undefined'\n ? VisualEditingFallback\n : lazy(\n () =>\n /**\n * `lazy` expects the component as the default export\n * @see https://react.dev/reference/react/lazy\n */\n import('./VisualEditing.client'),\n )\n\nexport function VisualEditing(props: VisualEditingProps): ReactElement {\n return (\n <Suspense>\n <VisualEditingComponent {...props} />\n </Suspense>\n )\n}\n"],"names":["jsx","Fragment","lazy","Suspense","createDataAttribute","VisualEditingComponent","document","import","VisualEditing","props","children"],"mappings":"cAMAA,cAAAC,MAAA,mCAAAC,cAAAC,MAAA,sCAAAC,MAAA,+CAQA,MAAMC,SACGC,SAAa,IATtB,WACW,OAAAN,EAAAC,EAAA,CAAA,EACX,EASMC,GACE,IAKEK,OAAO,2CAGV,SAASC,EAAcC,YAEzBN,EACC,CAAAO,SAAAV,EAACK,EAAwB,IAAGI,KAGlC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {VisualEditing} from '@sanity/visual-editing/remix'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Prevent a consumer from importing into a worker/server bundle.
|
|
5
|
+
*/
|
|
6
|
+
if (typeof document === 'undefined') {
|
|
7
|
+
throw new Error(
|
|
8
|
+
'Visual editing should only run client-side. Please check that this file is not being imported into a worker or server bundle.',
|
|
9
|
+
)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Enables visual editing on the front-end
|
|
14
|
+
* @see https://www.sanity.io/docs/introduction-to-visual-editing
|
|
15
|
+
*/
|
|
16
|
+
export default VisualEditing
|
|
@@ -1,17 +1,32 @@
|
|
|
1
1
|
import type {VisualEditingProps} from '@sanity/visual-editing/remix'
|
|
2
2
|
import {lazy, type ReactElement, Suspense} from 'react'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Provide a consistent fallback to prevent hydration mismatch errors.
|
|
6
|
+
*/
|
|
7
|
+
function VisualEditingFallback(): ReactElement {
|
|
8
|
+
return <></>
|
|
9
|
+
}
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @see https://
|
|
12
|
+
* If server-side rendering, then return the fallback instead of the heavy dependency.
|
|
13
|
+
* @see https://remix.run/docs/en/1.14.3/guides/constraints#browser-only-code-on-the-server
|
|
11
14
|
*/
|
|
15
|
+
const VisualEditingComponent =
|
|
16
|
+
typeof document === 'undefined'
|
|
17
|
+
? VisualEditingFallback
|
|
18
|
+
: lazy(
|
|
19
|
+
() =>
|
|
20
|
+
/**
|
|
21
|
+
* `lazy` expects the component as the default export
|
|
22
|
+
* @see https://react.dev/reference/react/lazy
|
|
23
|
+
*/
|
|
24
|
+
import('./VisualEditing.client'),
|
|
25
|
+
)
|
|
26
|
+
|
|
12
27
|
export function VisualEditing(props: VisualEditingProps): ReactElement {
|
|
13
28
|
return (
|
|
14
|
-
<Suspense
|
|
29
|
+
<Suspense>
|
|
15
30
|
<VisualEditingComponent {...props} />
|
|
16
31
|
</Suspense>
|
|
17
32
|
)
|