Skip to content

About

Minimal repro: @defer (hydrate ...) never triggers in a routed component (Angular 22)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Repro: @defer (hydrate ...) never triggers in a routed component (Angular 22)

Minimal ng new --ssr app on Angular 22.0.8 (CLI 22.0.7). A @defer (hydrate on viewport) block placed inside a routed component never hydrates — the block's content is server-rendered but its client-side code never runs, so no IntersectionObserver is ever registered and the block stays dehydrated for the lifetime of the page.

This is a regression of #59997 (fixed by #60099 in 19.2), reintroduced by the newer ɵɵenableIncrementalHydrationRuntime() gating.

Run

npm install
npm run build
NG_ALLOWED_HOSTS=localhost node dist/ih-repro/server/server.mjs

Open http://localhost:4000/lazy (or /eager), scroll to the bottom.

  • Expected: the box turns green — HYDRATED — ngOnInit ran.
  • Actual: the box stays red — NOT HYDRATED — ngOnInit never ran.

document.querySelectorAll('[ngh]').length stays 1 (the block is still dehydrated) and no IntersectionObserver is ever constructed.

Both routes reproduce: /lazy uses loadComponent, /eager uses a statically imported component:. Only the root App component is exempt.

Control

src/app/app.html contains a commented-out @defer (hydrate on viewport) block. Uncomment it and rebuild. That block is off-screen and never hydrates itself, but its presence in the eagerly-bundled root component makes the routed blocks start hydrating — both routes go green.

Cause

ɵɵenableIncrementalHydrationRuntime() is emitted by the compiler into the create block of the component that owns the hydrate block (insertIncrementalHydrationRuntime, packages/compiler/src/template/pipeline). Until it runs, _runIncrementalHydrationBootstrap is () => {}.

runIncrementalHydrationBootstrap() is invoked exactly once, from the APP_BOOTSTRAP_LISTENER registered by withIncrementalHydration(), and ApplicationRef._loadComponent fires those listeners immediately after the root component's first tick().

A routed component is instantiated after that listener — the router's own initial navigation is itself kicked off from a bootstrap listener. So the runtime is switched on too late, gatherDeferBlocksCommentNodes / processAndInitTriggers never run, and no trigger is ever registered. There is no retry.

It is not only routed components

insertIncrementalHydrationRuntime inserts the enable op before the first defer op in each view unit, not once per component. A hydrate block inside a control-flow branch (@if, @for) therefore has its enable instruction in that embedded view's create block, and it only runs when the branch is instantiated — so a block behind an @if whose condition flips asynchronously misses the one-shot bootstrap even in an eagerly bootstrapped component.

Why there is no router-level workaround

withEnabledBlockingInitialNavigation() would order route activation ahead of the bootstrap listener. Angular discourages combining it with hydration — provideEnabledBlockingInitialNavigationDetector() console.warns NG05001 from an ENVIRONMENT_INITIALIZER; it does not throw — but the real problem is that it does not help: per the view-unit emission above, the enable instruction still lands too late.

The only workaround found is the control above: park a dummy hydrate block in the root component to force the runtime on.

Note also that setViewportTriggers / setImmediateTriggers in 22.0.x no longer carry the appRef.whenStable() wrapper that #60099 added.

About

Minimal repro: @defer (hydrate ...) never triggers in a routed component (Angular 22)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages