fix(core, web): load firebase-app.js before component bundles to avoid WebKit import race#18443
Conversation
…d WebKit import race
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. |
|
Hi @Yusufihsangorgel, thanks for the contribution and detailed description. LGTM! |
Description
Fixes #18436.
On Flutter web,
Firebase.initializeApp()intermittently throwsReferenceError: Cannot access 'SDK_VERSION' before initializationin WebKit-based browsers (Safari, WKWebView, and every iOS browser), leaving apps that await it beforerunApp()on a blank page. The linked issue includes production Sentry data: 968 events across 941 users in ~2 months, 100% of them WebKit.Root cause
_initializeCore()injects one script per registered service and awaits them all in parallel withFuture.wait. Each injection performs a dynamicimport(), and every component bundle (firebase-auth.js,firebase-messaging.js, …) statically imports from the sharedfirebase-app.js. With N services registered, N concurrent dynamic imports converge on that shared module, and WebKit has a long-standing module-evaluation defect in exactly this situation: it can hand back a namespace whose top-level bindings are still in the temporal dead zone. Readingfirebase.SDK_VERSIONright afterwards then throws. V8 and SpiderMonkey evaluate the shared dependency correctly, which matches the zero non-WebKit events in the report.Fix
Load
firebase-app.js(thecoreservice) first and only then fan out to the component bundles in parallel — option 3 from the issue. This keeps the parallel loading of component bundles (no measurable startup cost) while guaranteeing the shared dependency has fully evaluated before any dependent bundle's import starts, which removes the race entirely.The per-service injection logic is unchanged — it is only extracted into a local
injectServiceclosure so the core service and the remaining services go through the identical path.Testing
dart analyzeanddart formatclean.flutter test --platform chromepasses (existing Trusted Types + exception suites).firebase-app.jshas evaluated).Related Issues
Fixes #18436 (likely also the root cause behind the older WebKit reports #13107 / #11243).
Checklist