@sanity/react-loader
Advanced tools
Comparing version 0.7.0-pink-lizard to 0.7.1-pink-lizard
@@ -9,2 +9,17 @@ # Changelog | ||
## [0.7.1-pink-lizard](https://github.com/sanity-io/visual-editing/compare/react-loader-v0.7.0-pink-lizard...react-loader-v0.7.1-pink-lizard) (2023-11-08) | ||
### Bug Fixes | ||
* **deps:** update dependency @sanity/client to v6.8.0-pink-lizard.12 ([#298](https://github.com/sanity-io/visual-editing/issues/298)) ([4bfbfff](https://github.com/sanity-io/visual-editing/commit/4bfbfffb8fab9e3440fb525babd2df0120fc4900)) | ||
* **README:** document SSR mode ([718f84a](https://github.com/sanity-io/visual-editing/commit/718f84a68fc5938363c865d0efee1649a7e53069)) | ||
### Dependencies | ||
* The following workspace dependencies were updated | ||
* dependencies | ||
* @sanity/core-loader bumped to 0.7.1-pink-lizard | ||
## [0.7.0-pink-lizard](https://github.com/sanity-io/visual-editing/compare/react-loader-v0.6.0-pink-lizard...react-loader-v0.7.0-pink-lizard) (2023-11-08) | ||
@@ -11,0 +26,0 @@ |
{ | ||
"name": "@sanity/react-loader", | ||
"version": "0.7.0-pink-lizard", | ||
"version": "0.7.1-pink-lizard", | ||
"homepage": "https://github.com/sanity-io/visual-editing/tree/main/packages/react-loader#readme", | ||
@@ -123,3 +123,3 @@ "bugs": { | ||
"dependencies": { | ||
"@sanity/core-loader": "0.7.0-pink-lizard" | ||
"@sanity/core-loader": "0.7.1-pink-lizard" | ||
}, | ||
@@ -126,0 +126,0 @@ "devDependencies": { |
125
README.md
@@ -18,4 +18,123 @@ # @sanity/react-loader | ||
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<Product>(queryProduct, params), | ||
}) | ||
} | ||
export default function ShoePage() { | ||
const { params, initial } = useLoaderData<typeof loader>() | ||
if (!params.slug || !initial.data?.slug?.current) { | ||
throw new Error('No slug, 404?') | ||
} | ||
const { data } = useQuery<Product>(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 <ProductTemplate data={data} /> | ||
} | ||
``` | ||
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 | ||
@@ -38,1 +157,5 @@ | ||
[bundlephobia]: https://bundlephobia.com/package/@sanity/react-loader@pink-lizard | ||
``` | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
380503
160
+ Added@sanity/core-loader@0.7.1-pink-lizard(transitive)
+ Added@sanity/groq-store@5.2.5-pink-lizard(transitive)
- Removed@sanity/core-loader@0.7.0-pink-lizard(transitive)
- Removed@sanity/groq-store@5.2.4-pink-lizard(transitive)