sanity icon indicating copy to clipboard operation
sanity copied to clipboard

[nuxt] Could not access `sanity`. The only available runtime config keys on the client side are `public` and `app`.

Open simonhrogers opened this issue 1 year ago • 2 comments

Hi there – apologies in advance, as I don’t think this is a high-quality question, but can’t find anything addressing it through the docs or issues here. I’m hoping its a relatively straightforward misunderstanding that I’m building with?

I’m seeing this warning wherever I call useSanity in my Nuxt 1.13.1 app. It’s also followed me through every version of Nuxt 3 I’ve had installed from 3.0.0 all the way up.

I’d like to try and resolve it, as I think it might be tied up with hydration issues I’m experiencing with aspects it affects further down the chain. The strange thing is, if I console.log the config returned by useSanity, it’s correct and exactly as I’d expect.

I’ve seen the warning referenced in other open issues e.g. https://github.com/nuxt-modules/sanity/issues/920, and while the url builder is one such area I get this warning, I’ve since managed to avoid it by calling useSanity outside of a plugin.

So here are two areas I know the warning arises:

– useSanity called within a plugin – useSanityQuery called within a pinia store, e.g. stores/MainStore.js

The following starter could be used as a good indication of my setup, I will only have found ways to make Julian’s brilliant work dramatically worse :-)

https://github.com/jjjuulliiaann/sanity-nuxt-3-starter

[nuxt] Could not access `sanity`. The only available runtime config keys on the client side are `public` and `app`. See https://nuxt.com/docs/guide/going-further/runtime-config for more information.

simonhrogers avatar Sep 23 '24 11:09 simonhrogers

+1 I dont remember seeing this a couple weeks/months back. Its something new and as far as I understand, its happening because useSanityQuery tries to access runtimeConfig.sanity, which is not accessible during runtime, only .public is.

Im not sure why it was working before now that I think of it

gluharry avatar Oct 01 '24 09:10 gluharry

Looks like this warning was introduced in https://github.com/nuxt/nuxt/pull/26441 and was released in v3.12.0.

Without re-familiarising myself too much, I assume it's because we run something like this in useSanity

const $config = useRuntimeConfig()
defu($config.sanity, $config.public.sanity)

rdunk avatar Oct 02 '24 10:10 rdunk

yes, exactly.

you can ignore the warning for now, and we'll guard this to avoid accessing the key on client-side

danielroe avatar Nov 20 '24 20:11 danielroe