Skip to content

Backport zone wrapper fixes and initializeBrowserOrServerApp to 21.1.x - #3848

Open
armando-navarro wants to merge 4 commits into
angular:21.1.xfrom
armando-navarro:browser-or-server-app-21
Open

armando-navarro wants to merge 4 commits into
angular:21.1.xfrom
armando-navarro:browser-or-server-app-21

Conversation

@armando-navarro

@armando-navarro armando-navarro commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #3826
Refs #3843
Refs #3828
Refs #3841

This pull request carries four cherry-picks from main onto 21.1.x, so they ship together in 21.1.0.

Commits, in order

Commit From main Change
7874a4e c593ecd (#3827) Twelve wrapped calls stop holding the app unstable
82f50b0 74abd5a (#3844) initializeBrowserOrServerApp
c37218c 916435a (#3829) A handler passed as an observer object is wrapped like a function, and a wrapped promise settles after teardown
4d69ddc cf39fe6 (#3842) 25 synchronous Firestore functions are no longer zone wrapped

Where #3829 differs from main

#3827, #3844 and #3842 apply unchanged. #3829 had two conflicts:

src/zones.ts takes exactly #3829's change.

What an app on 21 will notice

Checked on this branch

I plan to use rebase and merge, so each cherry-pick stays its own commit on 21.1.x.

…#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)
@armando-navarro armando-navarro added bump: minor Size of the change: a new capability with no breaking change comp: app-check App Check (src/app-check). comp: auth Authentication (src/auth). comp: core FirebaseApp DI core (src/app). comp: database Realtime Database (src/database). comp: remote-config Remote Config (src/remote-config). comp: ssr Server-side rendering, hydration, @angular/ssr interop. comp: zones Change detection / zone.js / zoneless. type: feature New capability or enhancement. labels Nov 18, 2026
@armando-navarro armando-navarro added this to the 21.1.0 milestone Nov 18, 2026
…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)
@armando-navarro armando-navarro changed the title feat(app): add initializeBrowserOrServerApp for browser & server apps (21.1.x) Backport zone wrapper fixes and initializeBrowserOrServerApp to 21.1.x Nov 18, 2026
@armando-navarro armando-navarro added the comp: firestore Cloud Firestore (src/firestore). label Nov 18, 2026

@tyler-reitz tyler-reitz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bump: minor Size of the change: a new capability with no breaking change comp: app-check App Check (src/app-check). comp: auth Authentication (src/auth). comp: core FirebaseApp DI core (src/app). comp: database Realtime Database (src/database). comp: firestore Cloud Firestore (src/firestore). comp: remote-config Remote Config (src/remote-config). comp: ssr Server-side rendering, hydration, @angular/ssr interop. comp: zones Change detection / zone.js / zoneless. type: feature New capability or enhancement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants