Repository navigation
Backport zone wrapper fixes and initializeBrowserOrServerApp to 21.1.x - #3848
Open
armando-navarro wants to merge 4 commits into
Open
armando-navarro wants to merge 4 commits into
armando-navarro wants to merge 4 commits into
Conversation
…#3827) ɵzoneWrap holds a pending task from the moment a wrapped call gets a callback until that callback first runs. For onLog, onTokenChanged, the Database child listeners (onChildAdded on an empty node, the other three until a later change), off, enableLogging, and the rxfire streams stateChanges and auditTrail, nothing guarantees that first run, so ApplicationRef.isStable can stay false for good and an SSR build's route extraction times out, as angular#3748 reported for beforeAuthStateChanged. They keep logLevel WARN, so only the pending task changes. isSupported in remote-config is the twelfth: angular#3613 put its blockUntilFirst: false entry in the rxfire block, which exports no isSupported, so it never applied, and angular#3759 removed the dead entry. Server renders no longer wait for onChildAdded's first batch (onValue, object and list still wait), and off(ref, type, cb) still detaches nothing, because the wrapper replaces the callback it is given. fromRef takes the event to listen for, and one blockUntilFirst value cannot fit both uses: value always fires on attach, while a child event may never fire. A hand-written fromRef in overrides.ts wraps rxfire's fromRef both ways and picks by event, so fromRef(ref, ListenEvent.value) holds a server render like object(ref) and child events do not. Fixes angular#3826 (cherry picked from commit c593ecd)
…angular#3844) * feat(app): add initializeBrowserOrServerApp for browser & server apps One call inside provideFirebaseApp that returns the ordinary app in the browser and, on the server, a FirebaseServerApp for the visitor's ID token and App Check token, read from REQUEST_CONTEXT or passed in the settings. With neither token it returns the ordinary app. The server app is released when the render is garbage collected, as the releaseOnDeref setup in the guide does. releaseAfterRender opts into a release at render end or a fixed time after it. A token Firebase cannot parse now renders signed out with one warning instead of an HTTP 500. In dev mode a request with no request context is noted once. The App Check server warning now says which renders send no token. The Node specs can import the built library through a resolve hook. Fixes angular#3843 * fix(app): keep the default release working when an ngOnDestroy throws The object handed to the SDK as releaseOnDeref was kept alive by a DestroyRef.onDestroy closure. R3Injector.destroy runs every service's ngOnDestroy before it empties those closures, so a throw there left the object reachable from the injector, and any listener that kept the injector alive kept the server app alive with it. The object is now a providedIn: 'root' service, which the finally of destroy drops whatever threw before it. The timed mode still releases from DestroyRef.onDestroy and a throw before it still skips that release, as the docs say. New specs on the release path: the default release observed through forced collections, a shared app with a positive releaseAfterRender released once per render, a null JSON payload, the App Check half of the token comparison, and a throwing ngOnDestroy next to a retained injector reference. The test:node scripts pass --expose-gc for the forced collections. The resolve hook picks the exports entry the way Node does, the first key in the map's order that is an active condition or default. Refs angular#3844 (cherry picked from commit 74abd5a)
…apped promise settle after teardown (angular#3829) * fix(core): wrap a handler passed as an observer object like a function Firebase accepts a subscription handler either as a function or as an object with next, error and complete members (data-connect spells them onNext, onErr, onComplete). The zone wrapper only recognized the function, so an observer object reached the SDK untouched: no pending task held server rendering, inject() threw inside the handler, and in a zone.js app the handler ran outside the Angular zone, where the wrapper itself had moved the call. The object is now replaced by a copy whose function members are wrapped the same way, with this bound to the caller's object. onConfigUpdate takes only the object form and its handler fires only on a remote template change, so it joins the blockUntilFirst: false list along with onTokenChanged, whose first callback a first visit may never request. Both would otherwise hold the application unstable forever once their observers are wrapped. * fix(core): let a wrapped promise settle after its injector is destroyed When a wrapped call returns a Promise, the wrapper resolved its own promise through runInInjectionContext and NgZone.run. Neither reaches the caller: a .then callback runs as a later microtask after the injection context is torn down, and zone.js runs it in the zone where .then was attached, not where resolve was called. The only effect the two layers had was on teardown. runInInjectionContext throws once its injector is destroyed, so a Firebase promise settling after ApplicationRef.destroy() or a TestBed reset threw before resolve ran and the caller's promise never settled. The pending task still opens on the call and closes on settlement. * fix(core): wrap observer objects only for functions that take one The previous commit treated any object argument with a next, error or complete method as an observer. A model class with a complete() method, saved through setDoc with a converter, was replaced by a plain copy that lost its prototype, and its pending task was never released. tools/build.ts now reads Firebase's type declarations with the TypeScript compiler API and passes a fourth ɵzoneWrap argument, true, only to the 11 exports with an observer-shaped parameter. Every other function receives its object arguments untouched. A handler Firebase calls after the app is destroyed now runs directly, because entering a destroyed injector throws NG0205. Handlers passed as functions already hit this on main. (cherry picked from commit 916435a)
…ngular#3842) Query and reference builders (where, orderBy, query, collection, doc and the rest), field values (increment, deleteField, vector, sum), the snapshot and query comparators, and setLogLevel only build or compare values. In @angular/fire/firestore and its lite entry point, the zone wrapper did nothing for them except print a false injection-context warning and, in zone.js apps, trigger an unneeded change detection pass when called inside an injection context but outside the Angular zone. No export or type changes. getFirestore, initializeFirestore, connectFirestoreEmulator, writeBatch and the cache index functions stay wrapped: they register listeners or start the Firestore client. New specs record every task each unwrapped function schedules, for all 38 null entries, and fail if a null entry has no sample call. Fixes angular#3841 (cherry picked from commit cf39fe6)
tyler-reitz
approved these changes
Nov 18, 2026
tyler-reitz
left a comment
Collaborator
There was a problem hiding this comment.
Range-diffs match main apart from the two #3829 conflicts you listed, and a build on this branch regenerates the firebase.ts files with no diff.
Not blocking: on the first run, two of the new specs failed in Windows Chrome headless ("functions re-exported without the zone wrapper each one returns synchronously and schedules no work" and "setDoc hands a converter the model instance itself and releases its pending task"). The job stayed green because that step has continue-on-error on Windows. A re-run passed and main passes them on Windows, so it looks like a flake, but nothing would flag it if it started failing for real.
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.
Refs #3826
Refs #3843
Refs #3828
Refs #3841
This pull request carries four cherry-picks from
mainonto21.1.x, so they ship together in 21.1.0.Commits, in order
main7874a4ec593ecd(#3827)82f50b074abd5a(#3844)initializeBrowserOrServerAppc37218c916435a(#3829)4d69ddccf39fe6(#3842)Where #3829 differs from
main#3827, #3844 and #3842 apply unchanged. #3829 had two conflicts:
tools/build.ts:mainalso importsReplaceResultfromreplace-in-file, an import build!: require Angular 22 and compile with TypeScript 6 #3801 added that nothing on21.1.xuses. Only thetypescriptimport is kept.src/zones.spec.ts: feat(zones)!: deliver Observable read values as microtasks #3833 created this file onmain, so it is new on this branch. It leaves out feat(zones)!: deliver Observable read values as microtasks #3833's two "delivery timing" specs and the imports only they used, because feat(zones)!: deliver Observable read values as microtasks #3833 is breaking and stays onmain.src/zones.tstakes exactly #3829's change.What an app on 21 will notice
onLog,onTokenChangedand the Realtime Database child listeners, no longer hold the app unstable, so a server render does not wait for them.@angular/fire/appexportsinitializeBrowserOrServerApp, one call insideprovideFirebaseAppthat builds the Firebase app in the browser and the server app during server rendering.onSnapshot(ref, { next }), holds server rendering until its first value, runs inside the Angular zone and can callinject(), like a handler passed as a function.NG0205.where,doc,collection,queryand the other synchronous Firestore functions no longer print the injection-context warning.Checked on this branch
src/*/firebase.tsfile with no diff, on TypeScript 5.9.3 and firebase 12.18.0.I plan to use rebase and merge, so each cherry-pick stays its own commit on
21.1.x.