angular / angular/angularfire

`beforeAuthStateChanged` imported from `@angular/fire/auth` makes `ng build` fail during route extraction

オープン
#3,748 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
comp: auth comp: ssr comp: zones type: bug
主要言語
TypeScript
スター
7.8k
フォーク
2.2k
平均マージ
22時間 28分
マージ済み PR(30日)
6

説明

### Description

`@angular/fire/auth` wraps `beforeAuthStateChanged` as `ɵzoneWrap(_beforeAuthStateChanged, true)` (`src/auth/firebase.ts` line 61). With that flag, the wrapper registers a pending task as soon as a callback is passed and clears it only when the callback first fires or the returned unsubscribe runs (`src/zones.ts`). This callback only fires when a sign-in or sign-out happens, so an app that registers the hook at startup holds the pending task open and `ApplicationRef.isStable` never becomes true for a visitor who is not signing in or out.

`ng build` route extraction waits on app stability, so any server-rendered app that registers this hook in its app config cannot build. Extraction hangs for about 30 seconds, then:

```
AbortError: Routes extraction was aborted.
TimeoutError: The operation was aborted due to timeout
```

### Steps to reproduce

1. Fresh `ng new --ssr` Angular 21 app, `ng add @angular/fire@next`.
2. In `app.config.ts`, register `beforeAuthStateChanged` inside `provideAppInitializer`, imported from `@angular/fire/auth`. Registering it at startup is exactly what an SSR token-cookie sync does (docs/auth.md, Server-side Rendering section).
3. `ng build` fails with the error above (34 seconds in my run).
4. Change only the import to `firebase/auth`. The identical code builds in under 5 seconds.

### Expected behavior

Registering the hook does not permanently block app stability. `onMessage` in `src/messaging/firebase.ts` wraps the same shape of API, a callback that may never fire, with the flag set to false, which still zone-wraps the callback and only skips the pending task. The same flag here looks like the fix, pending a check that nothing relies on the blocking behavior. `onLog` in `src/app/firebase.ts` has the same shape and is worth sweeping at the same time.

### Workarounds in the wild

Our own sample already imports `beforeAuthStateChanged` from `firebase/auth` (`sample/src/app/auth/auth.component.ts`), and the rewritten SSR docs section in #3740 documents that import as the way around this bug.

### Versions

Reproduced on an Angular 21 app against the @angular/fire 21 canary. The wrapper is unchanged on current `main`.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

説明されているアプリ設定で SSR のルート抽出に失敗する問題を再現し、その後 src/auth/firebase.ts の 61 行目と src/zones.ts の保留中タスクの挙動を調査します。src/messaging/firebase.ts と src/app/firebase.ts の関連するラッパーを比較し、auth hook を登録しても ng build の完了が妨げられなくなったことを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
angular, typescript
領域
authentication, build-system
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
活発
明瞭さ
明確に書かれている
初心者へのやさしさ
76/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。