From d201dd76ef2d7449481025d08320c76b86807aa9 Mon Sep 17 00:00:00 2001 From: Alex Morris Date: Thu, 19 Nov 2026 17:48:16 +0000 Subject: [PATCH] fix(core): do not drop nested @defer child blocks while parent hydrates In `getParentBlockHydrationQueue`, PR #58722 breaks out of the ancestor loop before calling `hydrationQueue.unshift(currentBlockId)` when it encounters an ancestor `@defer` block that is already hydrating. Because of that, `hydrationQueue` never contains the hydrating ancestor, only the unhydrated descendants below it. `triggerHydrationFromBlockName` still contains a `hydrationQueue.shift()` added in PR #60203 under the assumption that the hydrating parent sat at index 0. When `parentBlockPromise` is non-null, that shifts off the first unhydrated child block instead: in a 2-level nested `@defer` block it empties the queue and drops the replay event, and in a 3-level hierarchy (immediate inside interaction inside immediate) it drops the middle block so both inner blocks stay dehydrated. Remove the stale `hydrationQueue.shift()` call and add regression specs in `incremental_hydration_spec.ts`. --- packages/core/src/defer/triggering.ts | 6 - .../test/incremental_hydration_spec.ts | 113 ++++++++++++++++++ 2 files changed, 113 insertions(+), 6 deletions(-) diff --git a/packages/core/src/defer/triggering.ts b/packages/core/src/defer/triggering.ts index 85528500fe6..0a62df6fd5c 100644 --- a/packages/core/src/defer/triggering.ts +++ b/packages/core/src/defer/triggering.ts @@ -391,12 +391,6 @@ export async function triggerHydrationFromBlockName( const {parentBlockPromise, hydrationQueue} = getParentBlockHydrationQueue(blockName, injector); if (hydrationQueue.length === 0) return; - // It's possible that the hydrationQueue topmost item is actually in the process of hydrating and has - // a promise already. In that case, we don't want to destroy that promise and queue it again. - if (parentBlockPromise !== null) { - hydrationQueue.shift(); - } - // The hydrating map in the registry prevents re-triggering hydration for a block that's already in // the hydration queue. Here we generate promises for each of the blocks about to be hydrated populateHydratingStateForQueue(dehydratedBlockRegistry, hydrationQueue); diff --git a/packages/platform-server/test/incremental_hydration_spec.ts b/packages/platform-server/test/incremental_hydration_spec.ts index c0ead5fa2ca..a15aacd5a59 100644 --- a/packages/platform-server/test/incremental_hydration_spec.ts +++ b/packages/platform-server/test/incremental_hydration_spec.ts @@ -27,6 +27,7 @@ import { ɵresetIncrementalHydrationRuntimeForTests as resetIncrementalHydrationRuntimeForTests, signal, ɵTimerScheduler as TimerScheduler, + Type, ViewChild, ViewChildren, ViewContainerRef, @@ -3562,6 +3563,118 @@ describe('platform-server partial hydration integration', () => { ); }); + describe('nested @defer blocks during incremental hydration', () => { + const envProviders = [{provide: APP_ID, useValue: 'custom-app-id'}]; + const hydrationFeatures = () => [withIncrementalHydration(), withEventReplay()]; + let clientInnerInitCount = 0; + + @Component({ + selector: 'inner-block', + template: `hydrated`, + }) + class InnerBlock { + private readonly isServer = isPlatformServer(inject(PLATFORM_ID)); + + constructor() { + if (!this.isServer) { + clientInnerInitCount++; + } + } + } + + async function ssrAndHydrate(component: Type) { + const html = await ssr(component, {envProviders, hydrationFeatures}); + resetTViewsFor(component, InnerBlock); + clientInnerInitCount = 0; + + const doc = getDocument(); + const appRef = await prepareEnvironmentAndHydrate(doc, html, component, { + envProviders, + hydrationFeatures, + }); + appRef.tick(); + await appRef.whenStable(); + return {doc, appRef}; + } + + it('should hydrate nested immediate -> interaction -> immediate @defer blocks without dropping the middle block', async () => { + @Component({ + selector: 'app', + imports: [InnerBlock], + template: ` + @defer (hydrate on immediate) { +
+ @defer (hydrate on interaction) { +
+ @defer (hydrate on immediate) { + + } +
+ } +
+ } + `, + }) + class NestedImmediateApp {} + + const {doc, appRef} = await ssrAndHydrate(NestedImmediateApp); + await allPendingDynamicImports(); + appRef.tick(); + await appRef.whenStable(); + + expect(clientInnerInitCount).toBe(1); + verifyNodeWasHydrated(doc.querySelector('.outer')!); + verifyNodeWasHydrated(doc.querySelector('.middle')!); + verifyNodeWasHydrated(doc.querySelector('.inner-content')!); + }); + + it('should hydrate and replay click on inner @defer block triggered while outer @defer is hydrating', async () => { + const shouldHydrate = signal(false); + const clicks = signal(0); + + @Component({ + selector: 'app', + imports: [InnerBlock], + template: ` + @defer (hydrate when shouldHydrate()) { +
+ @defer (hydrate on interaction) { + + } +
+ } + `, + }) + class NestedClickWhileHydratingApp { + shouldHydrate = shouldHydrate; + onClick() { + clicks.update((n) => n + 1); + } + } + + const {doc, appRef} = await ssrAndHydrate(NestedClickWhileHydratingApp); + const btn = doc.getElementById('inner-btn')!; + expect(clientInnerInitCount).toBe(0); + expect(clicks()).toBe(0); + + // Start hydrating the outer block, then click the inner block while it is still pending. + shouldHydrate.set(true); + appRef.tick(); + btn.click(); + + await allPendingDynamicImports(); + appRef.tick(); + await appRef.whenStable(); + + expect(clientInnerInitCount).toBe(1); + expect(clicks()).toBe(1); + verifyNodeWasHydrated(doc.querySelector('.outer')!); + verifyNodeWasHydrated(btn); + }); + }); + describe('misconfiguration', () => { it('should log a warning when incremental hydration is disabled in SSR setup', async () => { @Component({