Issue with data not loading in preview mode due to a syntax issue in the query.

21 replies
Last updated: Apr 29, 2021
I’m using a catch-all route
[[…slug]].tsx
, and inside that page I have :

  const test = usePreviewSubscription(routeQuery, {
    params: { slug },
    initialData: data,
    enabled: preview && slug,
  });
If I log out
test
, in preview mode, it’s called 4 times.. but the last time
test.data
is null… anyone experienced the same thing?
Apr 12, 2021, 6:07 PM
There are some known instances where specific whitespaces in your query will cause the query to return nothing, when otherwise it would return something.
If you comment out the entire hook, does
data
have data in it?
Apr 12, 2021, 6:11 PM
Yes, then I have data coming from
getStaticProps
Apr 12, 2021, 6:11 PM
I’ve seen other people mentioning white space in query, but as far as I can see there are no whitespace in the query
Apr 12, 2021, 6:13 PM
Was looking for examples but yeah it’s most often in resolving references I think like
[] ->
needs to be
[]->
Apr 12, 2021, 6:13 PM
And using
select()
can return empty but usually just where you’ve used it.
Apr 12, 2021, 6:14 PM
And your Sanity client has an API token right, so it can read draft documents?
Apr 12, 2021, 6:14 PM
Yes, I think everything is in order otherwise.
Apr 12, 2021, 6:14 PM
I tried to simplify my query now, and it actually works..
Apr 12, 2021, 6:15 PM
Hm, allright.. then I atleast know somewhat where the issue is
Apr 12, 2021, 6:15 PM
Thanks for helping out 🙂
Apr 12, 2021, 6:16 PM
GL!
Apr 12, 2021, 6:17 PM
Thanks!
Apr 12, 2021, 6:17 PM
I'm experiencing the same as you
user R
. Were you able to fix it?
Apr 29, 2021, 1:07 PM
It’ll be a syntax issue in your query. It works server side but not in the preview client. Most common cause is additional trailing comma’s.

*[] {
  one: "one",
  two: "two", <-- this comma is a problem
}
Apr 29, 2021, 1:09 PM
It’ll be a syntax issue in your query. It works server side but not in the preview client. Most common cause is additional trailing comma’s.

*[] {
  one: "one",
  two: "two", <-- this comma is a problem
}
Apr 29, 2021, 1:09 PM
thanks Simeon, will check it out!
Apr 29, 2021, 1:10 PM
If you’re using
select()
my current understanding is that one won’t work at all in the preview client.
Strip out parts of your query line-by-line until the data loads as you expect – and you’ll find the offending line.
Apr 29, 2021, 1:11 PM
ah, turns out it was my own fault... was querying for a specific slug, but didn't pass it onto the component, so it worked server side but not client side
Apr 29, 2021, 1:16 PM
😄 easy done!
Apr 29, 2021, 1:16 PM
but thanks for the help, really appreciate your quick responses
Apr 29, 2021, 1:16 PM
haha yeah 😛
Apr 29, 2021, 1:16 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?