Repository navigation
Backport zone wrapper fixes and initializeBrowserOrServerApp to 21.1.x - #3848
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)
tyler-reitz
left a comment
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.
|
Thanks for the review, Tyler. The flaky specs you mentioned are a known behavior that's been in this repo for a while now. |
…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. 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. It would otherwise hold the application unstable forever once its observer is 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. (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)
4d69ddc to
16842ed
Compare
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.