Skip to content

Incremental hydration: @defer (hydrate ...) never triggers in a routed component (regression of #59997) #69908

Closed

Description

@lowebackstrom

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

A @defer (hydrate ...) block placed inside a routed component never hydrates. The block's content is server-rendered, but its client-side code never runs — no IntersectionObserver is ever constructed, the [ngh] attributes are never cleared, and the block stays dehydrated for the lifetime of the page. There is no retry, so it never recovers.

Only a hydrate block in the root component works. Both loadComponent and statically imported component: routes reproduce, so this affects essentially every real application — incremental hydration silently does nothing.

It fails silently: no error, no warning, no dev-mode diagnostic. Content that is server-rendered still looks correct, so the only visible symptom is that anything requiring client-side code inside the block never appears. In our case it was a Google Map that rendered on client-side navigation but never on a direct page load.

Cause

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

let _runIncrementalHydrationBootstrap = () => {};

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

_loadComponent(componentRef) {
  this.attachView(componentRef.hostView);
  try { this.tick(); } catch (e) { this.internalErrorHandler(e); }
  this.components.push(componentRef);
  const listeners = this._injector.get(APP_BOOTSTRAP_LISTENER, []);
  listeners.forEach(listener => listener(componentRef));
}

A routed component is instantiated after that listener — the router's own initial navigation is itself kicked off from a bootstrap listener (if (injector.get(INITIAL_NAVIGATION) === 1) router.initialNavigation()). So the runtime is switched on too late, gatherDeferBlocksCommentNodes / processAndInitTriggers never run, and no trigger is registered.

Verified by instrumentation on the reproduction: document.createNodeIterator(_, SHOW_COMMENT, _) is only ever called by processTextNodeMarkersBeforeHydration, never by gatherDeferBlocksCommentNodes, and IntersectionObserver is constructed zero times. __nghDeferData__ is well-formed ({"d0":{"r":1,"s":2,"t":[2]}}) and the comment anchors and root elements all resolve correctly — the serialized data is fine, only trigger registration is missing.

Regression of #59997

#59997 reported this same failure and was fixed by #60099 ("ensures triggers fire properly with lazy loaded routes") in 19.2, which wrapped setViewportTriggers / setImmediateTriggers in appRef.whenStable(). That wrapper is no longer present in 22.0.x, and the newer ɵɵenableIncrementalHydrationRuntime gating reopens the same ordering hole one layer earlier — before elementTriggers is even populated, so re-adding whenStable() alone would not fix it.

No workaround via router config

withEnabledBlockingInitialNavigation() would order route activation ahead of the bootstrap listener, but it is explicitly rejected when hydration is enabled:

NG05001: Configuration error: found both hydration and enabledBlocking initial navigation in the same application, which is a contradiction.

The only workaround we found is to park a dummy @defer (hydrate ...) block in the root component purely to force the runtime on — at which point the routed blocks hydrate correctly. That is included as a one-line control in the reproduction.

Please provide a link to a minimal reproduction of the bug

https://github.com/lowebackstrom/angular-incremental-hydration-routed-repro

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

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

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

Then uncomment the @defer (hydrate on viewport) block in src/app/app.html, rebuild, and both routes go green.

Please provide the exception or error you saw

No error is produced. The failure is silent.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI       : 22.0.7
Angular           : 22.0.8
Node.js           : 24.18.0
Package Manager   : npm 12.0.1
Operating System  : darwin arm64

@angular/build            22.0.7
@angular/cli              22.0.7
@angular/common           22.0.8
@angular/compiler         22.0.8
@angular/compiler-cli     22.0.8
@angular/core             22.0.8
@angular/platform-browser 22.0.8
@angular/platform-server  22.0.8
@angular/router           22.0.8
@angular/ssr              22.0.7
rxjs                      7.8.2
typescript                6.0.3

Anything else?

Also reproduced in a production Angular 22.0.7 SSR app, where four @defer (hydrate on viewport) sections on a loadComponent route never hydrate on a direct page load but work correctly after client-side navigation (because the component is then created client-side and hydrate triggers do not apply).

A dev-mode warning when runIncrementalHydrationBootstrap runs while the runtime is still inactive but __nghDeferData__ is present in TransferState would have made this immediately diagnosable.

Activity

  1. added
    area: core
    on Jul 23, 2026
  2. added this to the needsTriage milestone on Jul 23, 2026
  3. lowebackstrom commented on Jul 23, 2026

    @lowebackstrom
    Author

    Two corrections and one addition to the report above, after digging further into the compiler side.

    Correction 1 — NG05001 is a warning, not a rejection

    I wrote that withEnabledBlockingInitialNavigation() is "explicitly rejected when hydration is enabled". That is wrong. provideEnabledBlockingInitialNavigationDetector() in packages/platform-browser emits NG05001 through console.warn from an ENVIRONMENT_INITIALIZER — it never throws, and the combination is permitted. Apologies for the inaccurate framing.

    The practical conclusion is unchanged: I tried that combination against the real app and it did not fix the problem. Correction 2 explains why.

    Correction 2 — the enable instruction is emitted per view unit, so the hole is deeper than "routed component"

    insertIncrementalHydrationRuntime inserts the enable op before the first defer op in each view unit, not once per component:

    function insertIncrementalHydrationRuntime(job) {
      for (const unit of job.units) {
        for (const op of unit.create) {
          if (op.kind === OpKind.Defer && op.flags !== null && (op.flags & 1) !== 0) {
            OpList.insertBefore(createEnableIncrementalHydrationRuntimeOp(op.sourceSpan), op);
            break;
          }
        }
      }
    }

    So when a hydrate block sits inside a control-flow branch — @if, @for — the enable instruction lands in that embedded view's create block and only executes when the branch is instantiated. In the production app this bug was found in, all four hydrate blocks are inside @if (producer() && !error()), where producer() resolves asynchronously. The enable therefore could not have run in time even if the route component were eager and initial navigation were blocking.

    That means the title understates it: it is not only routed components. Any hydrate block whose enclosing view is created after the first tick() — including a hydrate block behind an @if in an eagerly bootstrapped component whose condition flips asynchronously — misses the one-shot bootstrap. The minimal reproduction's /eager route already demonstrates the non-lazy half of this.

    Addition — the failure is permanent, and there is a memoization trap for any fix

    Two things worth having on record for whoever picks this up:

    No fallback trigger. shouldAttachRegularTrigger returns false when the block has hydrate triggers, was SSR'd, and incremental hydration is enabled — so the plain viewport trigger is suppressed too. A block that misses the bootstrap has neither trigger and never recovers on scroll, resize, or a later tick. This is why the symptom is a permanently dead block rather than a delayed one.

    DEHYDRATED_BLOCK_REGISTRY memoization. It is provided as useFactory: createDehydratedBlockRegistry, which delegates to _dehydratedBlockRegistryFactory — and that returns () => null until ɵɵenableIncrementalHydrationRuntime() swaps it. Since Angular memoizes environment-provider factories, any fix that causes that token to be injected before the enable instruction runs will permanently cache null, and triggerHydrationFromBlockName then throws on dehydratedBlockRegistry.hydrating. Today this is safe only because the enable op is emitted immediately before the first ɵɵdefer, which is the first consumer.

    Note on the earlier fix

    For what it is worth, #60099 wrapped setViewportTriggers / setImmediateTriggers in appRef.whenStable(). That wrapper is not present in 22.0.x, but re-adding it alone would not address this: the ordering hole is now one layer earlier, and elementTriggers is empty before that code is reached.

  4. SkyZeroZx commented on Jul 23, 2026

    @SkyZeroZx
    Contributor

    Based on what I've read (from the information provided), this could have been introduced by #68333.
    This occurred when hydration was enabled by default and changes were made to make it tree shakeable.

    I haven't yet confirmed if this is the exact root cause, but I'll take a look and see if I can fix it.

  5. added 2 commits that reference this issue on Jul 24, 2026
    1cea74f
    d5074b8
  6. added a commit that references this issue on Jul 31, 2026
    cfe2cda
  7. added a commit that references this issue on Jul 31, 2026
    aa6d318
  8. angular-automatic-lock-bot commented on Oct 19, 2026

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: core

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions