treefarmstudio / astro-sanity

This is a helper package to integrate Astro and Sanity
https://www.npmjs.com/package/astro-sanity
MIT License
102 stars 10 forks source link

Integration doesn't work in ssr #1

Closed MoustaphaDev closed 1 year ago

MoustaphaDev commented 1 year ago

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

jaydanurwin commented 1 year ago

Thanks for reporting this! I can confirm it is in fact a bug. I'll see what I can do to support SSR ASAP. 👍🏼

cbarratt commented 1 year ago

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

BtheGit commented 1 year ago

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.

MoustaphaDev commented 1 year ago

@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

pijusz commented 1 year ago

Is there any progress on this issue? Still getting it.

jaydanurwin commented 1 year ago

I have it on my todo list today to dig into this actually. Progress is being made over on the next branch!

jaydanurwin commented 1 year ago

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?

jaydanurwin commented 1 year ago

Scratch that, you're right. It's building and running in dev but it won't serve once it's deployed.

jaydanurwin commented 1 year ago

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.