astro-sanity
astro-sanity copied to clipboard
Integration doesn't work in ssr
This integration doesn't work in ssr mode (output:'server'
in astro config). This is because initSanityClient(options)
is only called in astro:config:setup
hook which only runs during dev and build AFAIK
Thanks for reporting this! I can confirm it is in fact a bug. I'll see what I can do to support SSR ASAP. 👍🏼
I reported basically a similar issue here for the storyblok astro plugin, it appears to be any Astro plugin wrote in the same manner using globalThis
will exhibit this issue https://github.com/storyblok/storyblok-astro/issues/46
Not too sure what the fix is exactly, but figured I'd drop this here
Having similar issue with cloudflare pages. basically seems like in setup sanity is attached to globalthis successfully. But that is not the service worker process running later when called to render routes.
@jaydanurwin You should take a look at the injectSrcipt
option.
I've not tested this but seem like this could do the trick
export default function astroSanityIntegration(options: ClientConfig): AstroIntegration {
return {
name: 'astro-sanity',
hooks: {
'astro:config:setup': ({injectScript, config}) => {
if (config.output === "static"){
return initSanityClient(options)
}
const code =
`if (!globalThis.sanity){
const sanityClient = (await import('@sanity/client')).default;
const client = sanityClient(${JSON.stringify(options)});
globalThis.sanity = client;
}`
injectScript("page-ssr", code)
},
},
};
}
I'll experiment more when I have some time and submit a PR if I get it working
Is there any progress on this issue? Still getting it.
I have it on my todo list today to dig into this actually. Progress is being made over on the next branch!
I'm still testing but this might have been fixed with the latest version of Astro (1.7.2). It's building successfully for me in SSR mode.
@MoustaphaDev Can you confirm on your end?
Scratch that, you're right. It's building and running in dev but it won't serve once it's deployed.
This is fixed! 🎉
Please upgrade to v1.1.1 and you should be able to use the setup described in the docs and shown in the demos.
Special thanks to the storyblok-astro repo for leading me in the right direction on how to fix it. If you still are having issues with SSR please reach out.