🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Issue with groq query not working in Next.js application, but works in browser and Studio

9 replies
Last updated: Mar 28, 2021
Hi! I’m having an issue where the groq query works fine directly in the browser, also from the studio in vision, but not from the nextjs application, both serverside/clientside. There it just returns an empty array. CORS are set and all envs are loaded - everything seems to work, but have a hard time debugging. Any tips?
Mar 26, 2021, 1:31 PM
Hi User. Are you using asynchronous code to await the query?
Mar 26, 2021, 1:49 PM
Yes, pretty much following the different examples I’ve seen. Also trying different ways of connecting to Sanity. Currently trying with the helpers from next-sanity.
Mar 26, 2021, 1:50 PM
I have a working site using graphql, but since I have som issues with resolving references in Portable Text, I’m trying with groq instead.
Mar 26, 2021, 1:51 PM
You could try logging out props to see if anything (and if so, what) comes through. Or, feel free to paste a snippet of your code here to this thread.
Mar 26, 2021, 2:02 PM
I had a similiar problem using Next.js and getStaticProps, turned out that using template literal strings to include a variable in my groq-request was not the right way to do things. Got it working using the $parameter instead. Could that be it?Look here for code example:
Mar 26, 2021, 2:33 PM
Thanks for the input, but I’m down to the simplest of queries, with no variables/parameters. If I paste the generated url for the query in a new browser, it works fine. So I’m guessing it’s a configuration issue for the fetch client. But it works fine for graphql queries, and the other things I’ve tried is taken from docs and examples.
Would like to paste some code but just not at the computer.
Mar 26, 2021, 3:07 PM
Tried also both at the getStaticProps and in the client on mount.
Mar 26, 2021, 3:07 PM
Just wanted to say I had a look, and when adding the token for sanity, the query works fine. The reason I stumbled is because the query works fine without the token when pasting the query link directly in the browser. Also when no error messages are giving, it makes it difficult to debug. Thanks for chiming in
user A
user S
Mar 28, 2021, 8:21 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?