Skip to content

Commit

Permalink
fix(README): document SSR mode
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 8, 2023
1 parent 5d842e5 commit 718f84a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 35 deletions.
1 change: 0 additions & 1 deletion apps/next/src/components/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ const client = createClient({

export const { query, useQuery, useLiveMode } = createQueryStore({
client,
allowStudioOrigin: studioUrl,
})
5 changes: 1 addition & 4 deletions apps/page-builder-demo/src/sanity/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
125 changes: 124 additions & 1 deletion packages/react-loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<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

Expand All @@ -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

```
```
61 changes: 33 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 comments on commit 718f84a

@vercel
Copy link

@vercel vercel bot commented on 718f84a Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visual-editing-remix – ./apps/remix

visual-editing-remix.sanity.build
visual-editing-remix-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 718f84a Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visual-editing-page-builder-demo – ./apps/page-builder-demo

visual-editing-page-builder-demo.sanity.build
visual-editing-page-builder-demo-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 718f84a Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visual-editing-studio – ./apps/studio

visual-editing-studio.sanity.build
visual-editing-studio-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 718f84a Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visual-editing-svelte – ./apps/svelte

visual-editing-svelte.sanity.build
visual-editing-svelte-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 718f84a Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visual-editing-nuxt – ./apps/nuxt

visual-editing-nuxt-git-main.sanity.build
visual-editing-nuxt.sanity.build

Please sign in to comment.