← All writing
November 22, 2024·4 min read

The Firestore listener leak nobody talks about

onSnapshot + client-side routing = zombie subscriptions

onSnapshot is easy to add and easy to forget. In a SPA, forgetting means memory leaks and phantom writes.

Why it happens

You subscribe in useEffect, forget to return the unsubscribe, or return it but rely on component unmount that never fires during a tab-hide. In an SPA with 20+ realtime pages, that compounds fast.

A discipline that scales

Register every subscription against a route-scoped cleanup registry. When the route changes, drain the registry. When the tab hides for more than N seconds, pause listeners and resume on focus. Your Firestore bill will thank you.

FirebaseRealtime