psteinroe / supabase-cache-helpers

A collection of framework specific Cache utilities for working with Supabase.
https://supabase-cache-helpers.vercel.app
MIT License
516 stars 32 forks source link

useSubscriptionQuery() does not update subscription when hook parameters change #524

Open adamstoffel opened 2 weeks ago

adamstoffel commented 2 weeks ago

Describe the bug useSubscriptionQuery() and useSubscription() setup the Supabase Realtime subscription inside of a useEffect hook with an empty dependencies array.

This means that any change to the parameters passed (e.g. the table name, events, schema, filter, etc.) will NOT cause the actual subscription to be updated and the subscription will continue to notify events only for the first set of parameters passed.

To Reproduce

  1. Setup a subscription with a filter which is derived from a setState variable.
  2. Update the state variable to a new value (e.g. with input field)
  3. Make changes in the DB which should only show up for the old or new filters
  4. See that it always reports changes for the oldest filter param.

Expected behavior Changes to: channelName, event, schema, table, or filter parameters should cause the subscription to be reset (e.g. unsubscribe and resubscribe).

Additional context The problem is particularly acute with the usage of filters since this are good use cases for this to be dynamic (e.g. what entity is currently used for active content in the UI). Perhaps only using channelName as the dependency would be sufficient if documentation reflected the necessity of updating channelName when you want

psteinroe commented 1 week ago

Thanks for opening the issue! I like the idea of using the channel name to reset the subscriptions. Would you be willing to contribute this fix and a documentation? 🙏🏼