Files
angular/packages/platform-server/test
arturovt 1c91efce17 fix(core): wait for app stability before cleaning up dehydrated views
This fixes a production crash seen on pages that dynamically create
non-defer components:

    TypeError: Cannot set properties of null (setting '__ngContext__')
    ...
    at locateOrCreateElementNodeImpl

The crash was traced to `triggerHydrationForBlockQueue()`. When a
`@defer` block finishes hydrating, it removes its own `PendingTasks`
entry and immediately calls `cleanupHydratedDeferBlocks()` ->
`cleanupDehydratedViews()`. That cleanup walks all LViews registered
with `ApplicationRef` and removes dehydrated views that aren't
associated with a defer block. This isn't safe while other async
hydration work is still pending.

For example, a dynamically created component can have its own
`PendingTasks` entry while its `ViewContainerRef.createComponent()`
call is still waiting to run. If an unrelated defer block finishes
first, its cleanup can remove that component's dehydrated view before
the component gets a chance to claim it. The later creation then sees
stale hydration state and can fail with the crash above.

The same race shows up with the router. A `@defer (hydrate ...)` block
in the app shell can hydrate while a lazy route is still loading. The
router outlet hasn't claimed its server-rendered DOM yet, so the
cleanup deletes it and the page is rebuilt from scratch, which looks
like a flicker on first paint. It also hits a `PendingTasks`-guarded
`createComponent()` that lives inside the hydrating block itself.

This was reproduced with tests in `incremental_hydration_spec.ts`: an
immediately hydrated `@defer` block next to an unrelated
`PendingTasks`-guarded `ViewContainerRef.createComponent()` whose
creation is delayed until after the defer block hydrates, a shell
`@defer` block next to a lazy route, and a `createComponent()` inside
the hydrating block. All of them fail with the original code and pass
with this fix.

The fix waits for `ApplicationRef.whenStable()` before running the
cleanup, but only when there are other pending tasks. This keeps the
usual case synchronous while making sure cleanup doesn't run while
other hydration work is still in progress. The app can be destroyed
while the cleanup waits, so the cleanup is skipped in that case, the
same way the bootstrap cleanup does it.

The wait is intentionally conditional. An unconditional `whenStable()`
introduces another subscription to the shared stability signal.
Because this subscription is created after this function removes its
own pending task, callers that are already waiting on `whenStable()`
can resume one microtask before this cleanup does. That changes the
existing timing and breaks the `immediate` and `should create an
IntersectionObserver...` tests. Checking `hasPendingTasks` first
avoids creating that extra subscription when there is nothing else to
wait for, preserving the existing behavior.

Fixes #70796

(cherry picked from commit 80dba4b997)
2026-09-21 18:55:50 +00:00
..