See Sanity in action 👀 Join us for a live product demo + Q&A →

Issue with following references in a Next.js site built off of the sanity-template-nextjs-clean template

13 replies
Last updated: Feb 27, 2023
Hi! Newbie to Sanity here. I am building a Next.js site off of the sanity-template-nextjs-clean template. However I am running into an issue with the query here. It seems that the same query when executed via the webapp isn't following references while if I use Vision from Studio it follows the reference successfully.
Query I am trying to run:

  *[_type == "home"][0]{
In Vision, showcaseExhibitions following the reference. While when i output the fetch() from Next.js it returns an array of nulls. If i remove the follow reference notation, I successfully the object for a reference from both approaches. Any help here? Running in a major wall here.
Feb 26, 2023, 1:41 AM
have you tried with just showcaseExhibitions-> ?
Feb 26, 2023, 1:44 AM
Turns out it is because my browser has auth to hit the API, while my next.js app isn't passing through the token.
Feb 26, 2023, 3:18 AM
So apparently to get the GROQ to follow references you need to be logged in.
Feb 26, 2023, 3:18 AM
Now the question is how to get this template to pass in token. It looks like it is supposed to get the token from Preview somehow
Feb 26, 2023, 3:19 AM
but that doesn't make much sense
Feb 26, 2023, 3:19 AM
You shouldn’t need a token to follow that reference. Is it possible that the documents in that array are drafts?
Feb 27, 2023, 5:07 PM
showcaseExhibitions should return an array of exhibitions if it follows the ref
Feb 27, 2023, 7:51 PM
Oh, I see now, you do need a token to query for that particular document since it’s on a non-root path. You can add one to your client config here .
Feb 27, 2023, 9:41 PM
Yep that's what I did! Thanks.
Feb 27, 2023, 9:41 PM
Ahh I accidentally subpathed
Feb 27, 2023, 9:42 PM
But know I know why this was happening. Thanks!
Feb 27, 2023, 9:42 PM
You’re welcome!
Feb 27, 2023, 9:48 PM – build remarkable experiences at scale

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

Categorized in