Closed maxwellpopescu closed 3 years ago
Hi @maxwellpopescu ! Thanks for your submitted issue. The issue here seems to be that the hook is responding faster than Sanitys query indexer can catch up with committed patches. For listeners (https://www.sanity.io/docs/listening) Sanity support setting a flag to only be notified once a change is also visible in the query layer. For webhooks this flag is not yet available, so there are two possible solutions here.
I'll add your solution into this project so users don't have to do that manually in their serverless function.
I have been using the sanity-algolia client for a while but I kept facing weird issues like the published change not being reflected in the algolia index.
Today I started using the 'visibility' function and noticed some problematic behavior when toggling a document's "isHidden" field on and off and publishing.
A lot of the time, the document fetched by sanity-algolia's webhookSync function did not have the updated isHidden value
So it seems that when the serverless function receives a webhook notification, webhookSync immediately fetches the document from Sanity but this fetched data still needs to be updated by sanity. To confirm this, I added a simple timer to delay the document fetching by 5 seconds:
This solved the issue, and so my algolia index is now being updated properly.
My useCdn is set to false, so I am unsure why Sanity is sending out stale data. Perhaps the webhook notification is sent before the changes are saved? A 5 second timer is a bit of a hack, so I am hoping there is a better way to deal with this issue.