Trouble with listening to changes on a single document in Sanity JS client
9 replies
Last updated: May 24, 2021
C
Trying to listen to changes on single document, getting any changed document
Using the
Sanity JS client.listen method, we’re trying to listen to changes on a single document but are receiving any changed document, not just the intended target with matched field as specified in the GROQ filter.
We’ve tried signatures both with a without the “params” parameter, but are getting the same issue with both of the following query setups that attempt to listen only to the ‘show’ with matching ‘slug’ field:
Query only:
Query and params:
Both approaches fire updates with any changed document.
Can anyone help with tips/direction on how to restrict listener updates to a single intended record? Thanks!
Using the
Sanity JS client.listen method, we’re trying to listen to changes on a single document but are receiving any changed document, not just the intended target with matched field as specified in the GROQ filter.
We’ve tried signatures both with a without the “params” parameter, but are getting the same issue with both of the following query setups that attempt to listen only to the ‘show’ with matching ‘slug’ field:
Query only:
const showQuery = `*[_type == "show" && slug == "${this.slug}"][0]{field1, field2, ...}`; sanity.client.listen(showQuery);
const showQuery = `*[_type == "show" && slug == $slug][0]{field1, field2, ...}`; const showParams = { slug: `${this.slug}` }; sanity.client.listen(showQuery, showParams);
Can anyone help with tips/direction on how to restrict listener updates to a single intended record? Thanks!
May 24, 2021, 2:03 PM
C
Listening to a single document by querying just on the ID does appear to work, ala:
But still not clear why other queries like the above mentioned that filtered on fields besides ID would not work in the same way, or would catch changes on non-matching documents.
const observerQuery = `*[_id == $id]`;
May 24, 2021, 3:47 PM
C
It appears using the slice operation
If true, this seems like a good “gotcha” to call out in the official documentation.
[0]in a listen query will cause it to fail to listen to a single desired document and instead catch all updates. Dropping the slice seems to be working…
If true, this seems like a good “gotcha” to call out in the official documentation.
May 24, 2021, 4:00 PM
J
I believe this is the intended functionality for listen queries as of now. I don't think it's a gotcha as it is mentioned here with regard to the listening queries.
https://www.sanity.io/docs/listening#query-65c01f82db74
Only the filter portion will work. I don't think you can do object projections with listen queries as of now. Which is essentially what happens when you add the
https://www.sanity.io/docs/listening#query-65c01f82db74
Only the filter portion will work. I don't think you can do object projections with listen queries as of now. Which is essentially what happens when you add the
[0]in your query.
May 24, 2021, 4:56 PM
C
The “gotcha” issue I think, from my perspective is that the slice isn’t simply ignored, but rather that the inclusion of the slice will alter functionality--the filter will no longer be respected, and you will receive update results that are unrelated/do not match the filter criteria.
May 24, 2021, 5:54 PM
J
Are you using the latest API versions?
May 24, 2021, 5:56 PM
C
Not 100% sure. Using @sanity/client v2.7.1, query XHR GETs look like:
https://XXXXXX.api.sanity.io/v1/data/listen/production?query= ….
(useCDN set to false for this specific use case)
https://XXXXXX.api.sanity.io/v1/data/listen/production?query= ….
(useCDN set to false for this specific use case)
May 24, 2021, 5:59 PM
C
sorry, that is partially correct, we are using @nuxtjs/sanity@0.7.1, which I believe is in turn using @sanity/client v2.7.1
May 24, 2021, 6:06 PM
J
Your sanity config should have the
apiVersionkey with the latest api. Sounds like your getting the old API bugs.Definitely upgrade your packages, Some pretty significant fixes since. I use Nuxt as well so I know that the latest version is around
.0.9.4. Might fix up some of your issues.
May 24, 2021, 6:08 PM
C
Thanks for the advice, will look into locking down the apiVersion (I believe we’ve currently left it undefined) and updating packages.
May 24, 2021, 6:11 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.