Sanity and Hydrogen v2 #530
-
Hi, before sanity integrated seamlessly with hydrogen v1, can somebody explain how you would do this now? Before i had a custom hook for making sanity calls:
Then i could do this in my server components:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @carstensbix thanks for stopping by. While we release examples, I suggest you take a look at how we create and embed the storefront API client inside the remix
// server.ts
const sanityClient = sanityClient(sanityConfig);
async function sanity<T>(query, params = {}): <T> {
return await client.fetch(query, params)
}
// server.ts
const handleRequest = createRequestHandler({
build: remixBuild,
mode: process.env.NODE_ENV
getLoadContext: () => ({cache, session, waitUntil, storefront, env, sanity}),
});
// routes/index.tsx
export async function loader({params, context}: LoaderArgs) {
const {data: sanityHome} = await context.sanity(QUERY_SANITY, {);
return json({data})
// or if you want to defer the query/loading
const {data: sanityHome} = context.sanity(QUERY_SANITY, {);
return defer({data})
}
|
Beta Was this translation helpful? Give feedback.
Hi @carstensbix thanks for stopping by.
While we release examples, I suggest you take a look at how we create and embed the storefront API client inside the remix
context
. Here's an approach you could pursue:hydrogen/templates/demo-store/server.ts
Line 34 in d94488b
loaders
andactions
hydrogen/templates/demo-store/server.ts
Line 54 in d94488b