fix(auth): prevent null hydration on pagehide/reload#10123
Conversation
|
There was a problem hiding this comment.
Code Review
This pull request introduces lifecycle event handling for pagehide and pageshow in IndexedDBLocalPersistence to gracefully close the database and stop polling when the page is hidden, and resume polling when the page is shown. Feedback was provided to optimize the pageshow event listener by adding an if (this.isHiding) guard to prevent redundant polling timer restarts on initial page load.
macastelaz
left a comment
There was a problem hiding this comment.
Thanks for putting this fix together!
Description
This PR addresses an issue where Firebase Auth unexpectedly hydrates to a
nullstate (signing the user out) on page reload or when returning to a backgrounded/restored tab.The Problem
During
pagehide(e.g., page unload or reload), the browser can interrupt/abort active IndexedDB transactions. In WebKit/Safari, instead of throwing a catchable error when a transaction is aborted during unload, thegetAll()request on the object store resolves with an empty array ([]).Because
[]is truthy, the SDK's polling loop (_poll()) processes it, compares it to the in-memory cache, assumes the auth keys were deleted in another tab, and propagates anullAuth state.The Fix
pagehideandpageshowevent listeners toIndexedDBLocalPersistenceto set anisHidingflag, stop the polling timer, and close the IndexedDB connection during page transitions._openDb(),_withRetries(), and_poll()using theisHidingflag to prevent new database transactions from starting during pagehide, and discard any in-flight results.try/catchblock to prevent unhandled promise rejections if the database transaction is aborted.Closes #10041