
Security News
Official Go SDK for MCP in Development, Stable Release Expected in August
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
@sanity/react-loader
Advanced tools
[](https://npm-stat.com/charts.html?package=@sanity/react-loader) [](https://www.np
Warning
This is an experimental package. Breaking changes may be introduced at any time. It's not production ready.
npm install @sanity/react-loader @sanity/client react@^18.2
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
// ./src/app/sanity.loader.ts
import { createQueryStore } from '@sanity/react-loader'
export const {
// Used only server side
loadQuery,
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.
// ./src/app/sanity.loader.server.ts
import { createClient } from '@sanity/client/stega'
import { setServerClient, loadQuery } 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 { loadQuery }
Then somewhere in your app, you can use the loadQuery
and useQuery
utilities together. useQuery
now only fetches data when Live Mode is active. Otherwise it's loadQuery
that is used.
// ./src/app/routes/products.$slug.tsx
import { Link, useLoaderData, useParams } from '@remix-run/react'
import { json, type LoaderFunction } from '@remix-run/node'
import { loadQuery } from '~/sanity.loader.server'
import { useQuery } from '~/sanity.loader'
interface Product {}
const query = `*[_type == "product" && slug.current == $slug][0]`
export const loader: LoaderFunction = async ({ params }) => {
return json({
params,
initial: await loadQuery<Product>(query, params),
})
}
export default function ProductPage() {
const { params, initial } = useLoaderData<typeof loader>()
if (!params.slug || !initial.data?.slug?.current) {
throw new Error('No slug, 404?')
}
const { data } = useQuery<Product>(query, 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
:
// ./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
}
You can use useEncodeDataAttribute
to create data-json
attributes, that are picked up by @sanity/overlays
.
This allows you to link to elements that otherwise isn't automatically linked to using @sanity/client/stega
, such as array root item, or an image field.
Update your shared loader to change useQuery
, adding useEncodeDataAttribute
// ./src/app/sanity.loader.ts
import {
createQueryStore,
useEncodeDataAttribute,
QueryParams,
UseQueryOptions,
} from '@sanity/react-loader'
const studioUrl = 'https://my.sanity.studio'
const {
// Used only server side
loadQuery,
setServerClient,
// Used only client side
useQuery: _useQuery,
useLiveMode,
} = createQueryStore({ client: false, ssr: true })
// Used only server side
export { loadQuery, setServerClient }
// Used only client side
export { useLiveMode }
export const useQuery = <
QueryResponseResult = unknown,
QueryResponseError = unknown,
>(
loadQuery: string,
params?: QueryParams,
options?: UseQueryOptions<QueryResponseResult>,
) => {
const snapshot = _useQuery<QueryResponseResult, QueryResponseError>(
loadQuery,
params,
options,
)
const encodeDataAttribute = useEncodeDataAttribute(
snapshot.data,
snapshot.sourceMap,
studioUrl,
)
return {
...snapshot,
encodeDataAttribute,
}
}
You then use it in your template:
// ./src/app/routes/products.$slug.tsx
import { Link, useLoaderData, useParams } from '@remix-run/react'
import { json, type LoaderFunction } from '@remix-run/node'
import { loadQuery } from '~/sanity.loader.server'
import { useQuery } from '~/sanity.loader'
interface Product {}
const query = `*[_type == "product" && slug.current == $slug][0]`
export const loader: LoaderFunction = async ({ params }) => {
return json({
params,
initial: await loadQuery<Product>(query, params),
})
}
export default function ProductPage() {
const { params, initial } = useLoaderData<typeof loader>()
if (!params.slug || !initial.data?.slug?.current) {
throw new Error('No slug, 404?')
}
const { data, encodeDataAttribute } = useQuery<Product>(query, 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.
// And `encodeDataAttribute` is a helpful utility for adding custom `data-sanity` attributes.
return <ProductTemplate data={data} />
}
You use encodeDataAttribute
by giving it a path to the data you want to be linked to, or open in the Studio when in the Presentation tool.
// ./src/app/templates/product.tsx
import { StudioPathLike } from '@sanity/react-loader'
interface Product {}
interface Props {
data: Product
encodeDataAttribute: (path: StudioPathLike) => string | undefined
}
export default function ProductTemplate(props: Props) {
const { data, encodeDataAttribute } = props
return (
<>
<img
// Adding this attribute makes sure the image is always clickable in the Presentation tool
data-sanity={encodeDataAttribute('image')}
src={urlFor(data.image.asset).url()}
// other props
/>
</>
)
}
FAQs
[](https://npm-stat.com/charts.html?package=@sanity/react-loader) [](https://www.np
We found that @sanity/react-loader demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 87 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
Security News
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.