UNPKG

hydrogen-sanity

Version:
2 lines (1 loc) 2.64 kB
import{SanityClient as e,createClient as t}from"@sanity/client";import{SanityClient as n,createClient as i}from"@sanity/client";import{setServerClient as a,loadQuery as o}from"@sanity/react-loader";import{CacheLong as r,createWithCache as s,CacheNone as c}from"@shopify/hydrogen";import{h as l}from"./_chunks-es/utils.js";const u=r();function h(n){const{cache:i,waitUntil:r=()=>Promise.resolve(),request:h,preview:d,defaultStrategy:p}=n,g=i?s({cache:i,waitUntil:r,request:h}):null;let y=n.client instanceof e?n.client:t(n.client);if("1"===y.config().apiVersion&&(console.warn("No API version specified, defaulting to `v2022-03-07` which supports perspectives.\nYou can find the latest version in the Sanity changelog: https://www.sanity.io/changelog"),y=y.withConfig({apiVersion:"v2022-03-07"})),d&&d.enabled){if(!d.token)throw new Error("Enabling preview mode requires a token.");const e=y.withConfig({useCdn:!1,token:d.token,perspective:"previewDrafts",stega:{...y.config().stega,enabled:!0,studioUrl:d.studioUrl}});a(e)}else a(y);return{async loadQuery(e,t,n){if(!g)return await o(e,t,n);const i=d&&d.enabled?c():n?.hydrogen?.cache||p||u,a=await l(e,t),r=n?.hydrogen?.shouldCacheResult??(()=>!0),s=async function({addDebugData:i}){if("development"===process.env.NODE_ENV){i({displayName:n?.hydrogen?.debug?.displayName||"query Sanity"})}return await o(e,t,n)};return await("run"in g?g.run({cacheKey:a,cacheStrategy:i,shouldCacheResult:r},s):g(a,i,s))},client:y,preview:d}}const d=r();function p(n){const{withCache:i,preview:r,strategy:s}=n;let u=n.client instanceof e?n.client:t(n.client);if("1"===u.config().apiVersion&&(console.warn("No API version specified, defaulting to `v2022-03-07` which supports perspectives.\nYou can find the latest version in the Sanity changelog: https://www.sanity.io/changelog"),u=u.withConfig({apiVersion:"v2022-03-07"})),r&&r.enabled){if(!r.token)throw new Error("Enabling preview mode requires a token.");const e=u.withConfig({useCdn:!1,token:r.token,perspective:"previewDrafts",stega:{...u.config().stega,enabled:!0,studioUrl:r.studioUrl}});a(e)}else a(u);return{async loadQuery(e,t,n){const a=r&&r.enabled?c():n?.hydrogen?.cache||s||d,u=await l(e,t),h=n?.hydrogen?.shouldCacheResult??(()=>!0),p=async function({addDebugData:i}){if("development"===process.env.NODE_ENV){i({displayName:n?.hydrogen?.debug?.displayName||"query Sanity"})}return await o(e,t,n)};return await("run"in i?i.run({cacheKey:u,cacheStrategy:a,shouldCacheResult:h},p):i(u,a,p))},client:u,preview:r}}export{n as SanityClient,i as createClient,h as createSanityContext,p as createSanityLoader};//# sourceMappingURL=index.js.map