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`.
This commit is contained in:
Alex Morris
2026-11-20 05:20:30 -08:00
committed by Alex Rickabaugh
parent ee18d57563
commit d201dd76ef
2 changed files with 113 additions and 6 deletions
-6
View File
@@ -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);
@@ -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: `<span class="inner-content">hydrated</span>`,
})
class InnerBlock {
private readonly isServer = isPlatformServer(inject(PLATFORM_ID));
constructor() {
if (!this.isServer) {
clientInnerInitCount++;
}
}
}
async function ssrAndHydrate(component: Type<unknown>) {
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) {
<section class="outer">
@defer (hydrate on interaction) {
<div class="middle">
@defer (hydrate on immediate) {
<inner-block />
}
</div>
}
</section>
}
`,
})
class NestedImmediateApp {}
const {doc, appRef} = await ssrAndHydrate(NestedImmediateApp);
await allPendingDynamicImports();
appRef.tick();
await appRef.whenStable();
expect(clientInnerInitCount).toBe(1);
verifyNodeWasHydrated(doc.querySelector<HTMLElement>('.outer')!);
verifyNodeWasHydrated(doc.querySelector<HTMLElement>('.middle')!);
verifyNodeWasHydrated(doc.querySelector<HTMLElement>('.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()) {
<section class="outer">
@defer (hydrate on interaction) {
<button id="inner-btn" type="button" (click)="onClick()">
<inner-block />
</button>
}
</section>
}
`,
})
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<HTMLElement>('.outer')!);
verifyNodeWasHydrated(btn);
});
});
describe('misconfiguration', () => {
it('should log a warning when incremental hydration is disabled in SSR setup', async () => {
@Component({