diff --git a/apps/next/src/components/useQuery.ts b/apps/next/src/components/useQuery.ts index d287a679a..05f521ccc 100644 --- a/apps/next/src/components/useQuery.ts +++ b/apps/next/src/components/useQuery.ts @@ -22,5 +22,4 @@ const client = createClient({ export const { query, useQuery, useLiveMode } = createQueryStore({ client, - allowStudioOrigin: studioUrl, }) diff --git a/apps/page-builder-demo/src/sanity/store.ts b/apps/page-builder-demo/src/sanity/store.ts index 87a32c70b..5825f0fad 100644 --- a/apps/page-builder-demo/src/sanity/store.ts +++ b/apps/page-builder-demo/src/sanity/store.ts @@ -9,10 +9,7 @@ const workspace = workspaces['page-builder-demo'] export const client = getClient() -const { useQuery: _useQuery, useLiveMode } = createQueryStore({ - client, - allowStudioOrigin: studioUrl, -}) +const { useQuery: _useQuery, useLiveMode } = createQueryStore({ client }) const context: SanityNodeContext = { projectId: workspace.projectId, diff --git a/package.json b/package.json index 15c4b0cc3..801440266 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "dependencies": { "@sanity/block-tools": "3.19.2-pink-lizard.19", "@sanity/cli": "3.19.2-pink-lizard.19", - "@sanity/client": "6.8.0-pink-lizard.9", + "@sanity/client": "6.8.0-pink-lizard.12", "@sanity/diff": "3.19.2-pink-lizard.19", "@sanity/export": "3.19.2-pink-lizard.19", "@sanity/import": "3.19.2-pink-lizard.19", diff --git a/packages/react-loader/README.md b/packages/react-loader/README.md index 6ffb8e250..2e16a5048 100644 --- a/packages/react-loader/README.md +++ b/packages/react-loader/README.md @@ -15,7 +15,126 @@ npm i --save-exact @sanity/react-loader@pink-lizard @sanity/client@pink-lizard r ## Usage -TODO data fetching example +### Server only production data fetching, client side Live Mode + +By default data is fetched on both the server, and on the client after hydration. +For private datasets, or other similar use cases, it may be desirable to only fetch data on the server when Live Mode is not enabled. + +For this to work you'll first have to setup a shared file that is loaded both on the server and the client, which sets `ssr: true` and defers setting the client to later by setting `client: false`. The snippets are for a Remix application + +```ts +// ./src/app/sanity.loader.ts +import { createQueryStore } from '@sanity/react-loader' + +export const { + // Used only server side + query, + setServerClient, + // Used only client side + useQuery, + useLiveMode, +} = createQueryStore({ client: false, ssr: true }) +``` + +Later in the server side of the app, you setup the client. The `.server.ts` suffix on Remix ensures that this file is only loaded on the server, and it avoids adding `@sanity/client` to the browser bundle in production. + +```ts +// ./src/app/sanity.loader.server.ts +import { createClient } from '@sanity/client/stega' +import { setServerClient, query } from './sanity.loader' + +const client = createClient({ + projectId: process.env.SANITY_PROJECT_ID, + dataset: process.env.SANITY_DATASET, + useCdn: true, + apiVersion: process.env.SANITY_API_VERSION, + stega: { + enabled: true, + studioUrl: 'https://my.sanity.studio', + }, +}) + +setServerClient(client) + +// Re-export for convenience +export { query } +``` + +Then somewhere in your app, you can use the `query` and `useQuery` utilities together. `useQuery` now only fetches data when Live Mode is active. Otherwise it's `query` that is used. + +```tsx +// ./src/app/routes/products.$slug.tsx + +import { Link, useLoaderData, useParams } from '@remix-run/react' +import { json, type LoaderFunction } from '@remix-run/node' +import { query } from '~/sanity.loader.server' +import { useQuery } from '~/sanity.loader' + +interface Product {} +const queryProduct = `*[_type == "product" && slug.current == $slug][0]` + +export const loader: LoaderFunction = async ({ params }) => { + return json({ + params, + initial: await query(queryProduct, params), + }) +} + +export default function ShoePage() { + const { params, initial } = useLoaderData() + + if (!params.slug || !initial.data?.slug?.current) { + throw new Error('No slug, 404?') + } + + const { data } = useQuery(queryProduct, params, { initial }) + + // Use `data` in your view, it'll mirror what the loader returns in production mode, + // while Live Mode it becomes reactive and respons in real-time to your edits in the Presentation tool. + return +} +``` + +Enabling Live Mode is done by adding `useLiveMode` to the same component you're currently calling `enableOverlays` from `@sanity/overlays`: + +```tsx +// ./src/app/VisualEditing.tsx +import { enableOverlays, type HistoryUpdate } from '@sanity/overlays' +import { useEffect } from 'react' +import { useLiveMode } from '~/sanity.loader' + +// Only a Studio from this origin is allowed to connect to overlays and initiate live mode, it's also used to build Stega encoded source links that can take you from the application to the Studio +const allowStudioOrigin = 'https://my.sanity.studio' + +// A browser client for Live Mode, it's only part of the browser bundle when the `VisualEditing` component is lazy loaded with `React.lazy` +const client = createClient({ + projectId: window.ENV.SANITY_PROJECT_ID, + dataset: window.ENV.SANITY_DATASET, + useCdn: true, + apiVersion: window.ENV.SANITY_API_VERSION, + stega: { + enabled: true, + studioUrl: allowStudioOrigin, + }, +}) + +export default function VisualEditing() { + useEffect( + () => + enableOverlays({ + allowStudioOrigin, + history: { + // setup Remix router integration + }, + }), + [], + ) + + useLiveMode({ allowStudioOrigin, client }) + + return null +} +``` ## Visual Editing @@ -34,3 +153,7 @@ Show how to enable stega in strings. [gzip-badge]: https://img.shields.io/bundlephobia/minzip/@sanity/react-loader@pink-lizard?label=gzip%20size&style=flat-square [size-badge]: https://img.shields.io/bundlephobia/min/@sanity/react-loader@pink-lizard?label=size&style=flat-square [bundlephobia]: https://bundlephobia.com/package/@sanity/react-loader@pink-lizard + +``` + +``` diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 02348342c..4772deb6f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,7 +7,7 @@ settings: overrides: '@sanity/block-tools': 3.19.2-pink-lizard.19 '@sanity/cli': 3.19.2-pink-lizard.19 - '@sanity/client': 6.8.0-pink-lizard.9 + '@sanity/client': 6.8.0-pink-lizard.12 '@sanity/diff': 3.19.2-pink-lizard.19 '@sanity/export': 3.19.2-pink-lizard.19 '@sanity/import': 3.19.2-pink-lizard.19 @@ -34,8 +34,8 @@ importers: specifier: 3.19.2-pink-lizard.19 version: 3.19.2-pink-lizard.19 '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/diff': specifier: 3.19.2-pink-lizard.19 version: 3.19.2-pink-lizard.19 @@ -147,8 +147,8 @@ importers: specifier: ^3.0.11 version: 3.0.11(react@18.2.0) '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/image-url': specifier: ^1.0.2 version: 1.0.2 @@ -216,8 +216,8 @@ importers: specifier: ^6.9.4 version: 6.9.4(rollup@4.3.0)(webpack@5.89.0) '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/nuxt-loader': specifier: workspace:* version: link:../../packages/nuxt-loader @@ -455,8 +455,8 @@ importers: specifier: ^1.0.0 version: 1.0.0 '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/overlays': specifier: workspace:* version: link:../../packages/overlays @@ -544,8 +544,8 @@ importers: version: 2.0.0 devDependencies: '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/pkg-utils': specifier: ^3.2.3 version: 3.2.3(typescript@5.2.2) @@ -587,8 +587,8 @@ importers: version: 0.39.5 devDependencies: '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/pkg-utils': specifier: ^3.2.3 version: 3.2.3(typescript@5.2.2) @@ -630,8 +630,8 @@ importers: specifier: 0.10.0 version: 0.10.0(nanostores@0.9.4)(vue@3.3.8) '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/pkg-utils': specifier: ^3.2.3 version: 3.2.3(typescript@5.2.2) @@ -767,8 +767,8 @@ importers: version: 7.8.1 devDependencies: '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/pkg-utils': specifier: ^3.2.3 version: 3.2.3(typescript@5.2.2) @@ -804,8 +804,8 @@ importers: version: link:../core-loader devDependencies: '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/pkg-utils': specifier: ^3.2.3 version: 3.2.3(typescript@5.2.2) @@ -850,8 +850,8 @@ importers: version: link:../core-loader devDependencies: '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/pkg-utils': specifier: ^3.2.3 version: 3.2.3(typescript@5.2.2) @@ -886,8 +886,8 @@ importers: packages/visual-editing-helpers: devDependencies: '@sanity/client': - specifier: 6.8.0-pink-lizard.9 - version: 6.8.0-pink-lizard.9 + specifier: 6.8.0-pink-lizard.12 + version: 6.8.0-pink-lizard.12 '@sanity/util': specifier: 3.19.2-pink-lizard.19 version: 3.19.2-pink-lizard.19 @@ -4210,6 +4210,7 @@ packages: dependencies: is-glob: 4.0.3 micromatch: 4.0.5 + napi-wasm: 1.1.0 dev: true bundledDependencies: - napi-wasm @@ -5022,8 +5023,8 @@ packages: transitivePeerDependencies: - supports-color - /@sanity/client@6.8.0-pink-lizard.9: - resolution: {integrity: sha512-HwjL0VfyI/8+5cadysZLFZLMR2tQQ5NAY6lo5DI6RxGL+9HO1Inqf8FtMcxMl5Tgdb9qeycY/+vnT1JQtN6Irw==} + /@sanity/client@6.8.0-pink-lizard.12: + resolution: {integrity: sha512-9pCx5JCsb/l/y4vWvaAndi61aqCEylJaMaJ/F0G8yFk3bWyKE5bjcJcjwM+8rUlc/dPFZi512uk5LZUr3ENKjA==} engines: {node: '>=14.18'} dependencies: '@sanity/eventsource': 5.0.1 @@ -5107,7 +5108,7 @@ packages: engines: {node: '>=18'} hasBin: true dependencies: - '@sanity/client': 6.8.0-pink-lizard.9 + '@sanity/client': 6.8.0-pink-lizard.12 '@sanity/import': 3.19.2-pink-lizard.19 get-it: 8.4.4 meow: 9.0.0 @@ -5266,7 +5267,7 @@ packages: /@sanity/types@3.19.2-pink-lizard.19: resolution: {integrity: sha512-ZIrjcn/NUXEtYjR3kz8c6Vp3knzarYQok4HgOIG97bkWDWlhvciY0IXSE+hRREPmyX1AAvqj/xJ57ETDItuprQ==} dependencies: - '@sanity/client': 6.8.0-pink-lizard.9 + '@sanity/client': 6.8.0-pink-lizard.12 '@types/react': 18.2.37 transitivePeerDependencies: - supports-color @@ -13163,6 +13164,10 @@ packages: engines: {node: ^16.0.0 || ^18.0.0 || >=20.0.0} dev: true + /napi-wasm@1.1.0: + resolution: {integrity: sha512-lHwIAJbmLSjF9VDRm9GoVOy9AGp3aIvkjv+Kvz9h16QR3uSVYH78PNQUnT2U4X53mhlnV2M7wrhibQ3GHicDmg==} + dev: true + /natural-compare-lite@1.4.0: resolution: {integrity: sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==} dev: true @@ -16172,7 +16177,7 @@ packages: '@sanity/bifur-client': 0.3.1 '@sanity/block-tools': 3.19.2-pink-lizard.19 '@sanity/cli': 3.19.2-pink-lizard.19 - '@sanity/client': 6.8.0-pink-lizard.9 + '@sanity/client': 6.8.0-pink-lizard.12 '@sanity/color': 2.2.5 '@sanity/diff': 3.19.2-pink-lizard.19 '@sanity/diff-match-patch': 3.1.1