fix(lib): stop re-subscribing global error listeners on every render#1338
Open
abhay-codes07 wants to merge 1 commit into
Open
fix(lib): stop re-subscribing global error listeners on every render#1338abhay-codes07 wants to merge 1 commit into
abhay-codes07 wants to merge 1 commit into
Conversation
ErrorTrackingProvider wraps the whole app and keyed its window "error" and "unhandledrejection" listener effect on trackError. trackError is rebuilt on every render because it closes over posthog, session and pathname, so the two global listeners were torn down and re-added on every render of the provider, including on each navigation and session tick. Hold the latest trackError in a ref and register the listeners once for the provider's lifetime, dispatching through the ref so reports still use current posthog/session/pathname context.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ErrorTrackingProvideris mounted at the root of the app (app/layout.tsx) and installs globalwindow"error"and"unhandledrejection"listeners in an effect keyed ontrackError:useErrorTrackingreturns a freshtrackErroron every render (it is a plain closure overposthog,sessionandpathname, not memoized), so the dependency changes every render. That tears down and re-adds both global listeners on every render of a provider that wraps the entire app, including on each navigation and session refresh. It is wasted work on a hot path, and it leaves a small window on each cycle where a synchronously thrown error can land between theremoveEventListenerand the followingaddEventListener.Fix
Keep the latest
trackErrorin a ref and register the listeners once for the provider's lifetime, dispatching through the ref:The listeners are now added once and removed on unmount, and each report still reads the current
posthog/session/pathnamecontext through the ref. No functional change to what gets tracked, just no more churn.