Issue with duplicate content in portfolio section, resolved by adjusting the groq query.

18 replies
Last updated: May 20, 2022
Hello, I'm seeing a duplicate in my
content section. The
'collection' appears once on the backend cms as it should but is now being duplicated in the array of data being passed to the frontend as seen here: This error occurred after I cleared the image field and added a new image. Does anyone know how I can fix this?
May 20, 2022, 12:14 PM
Can you share the groq query?
May 20, 2022, 12:17 PM
Sure its
const query = '*[_type == "portfolio"]';
May 20, 2022, 12:19 PM
Well that’s weird. I feel like there is an obvious reason but I’m not seeing it. Is your site rendering the Sanity response directly or are you manipulating it first? If you change the image again does it give you another item?
May 20, 2022, 12:27 PM
Do you use a token with your client?
May 20, 2022, 12:59 PM
Gut feeling: you are using the authenticated client, so you’re querying drafts as well, and that entry has unpublished changes.
May 20, 2022, 1:00 PM
user J
No, I just changed the image again and after I published the change, refreshed my site, and checked the response, the duplicate was no longer there.
May 20, 2022, 1:14 PM
user F
Yes, I do use a token but, I'm not sure how it would have unpublished changes because I hit the publish button after the image change.
May 20, 2022, 1:15 PM
Is there a way for me to avoid this in the future?
May 20, 2022, 1:15 PM
Try this:
const query = `*[_type == "portfolio" && !(_id in path("drafts.**"))]`;
May 20, 2022, 1:22 PM
What Cory said, otherwise you could also not use a token in the client, so you only get published content.
May 20, 2022, 1:30 PM
So this enables me to query the portfolio collection but, ignore anything in the drafts correct?
May 20, 2022, 1:30 PM
Yes. A draft is just a document whose
starts with
, so what Cory gave you is a good way to discard drafts from your results. But you should ask yourself why you’re querying drafts at all. They’re typically not wanted since, well, they’re drafts.
May 20, 2022, 1:31 PM
I don't have any need for drafts. I thought I needed to use the token to get access to the db from my client.
May 20, 2022, 1:32 PM
I guess it depends if your dataset is public or private. If it is public, than you shouldn’t need a token. If it is private, then yes, and in that case you need to do what Cory suggested.
May 20, 2022, 1:33 PM
That makes sense.
May 20, 2022, 1:34 PM
I'll have to keep it in that case because it's private data.
May 20, 2022, 1:35 PM
Thanks you guys for all of the help, I really appreciate it.
May 20, 2022, 1:35 PM
All good. 🙂 ❤️
May 20, 2022, 1:35 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?