Skip to content

refactor(core): Makes @defer(hydrate ...) runtime tree-shakable - #68333

Merged
alxhub merged 1 commit into
angular:mainfrom
SkyZeroZx:refactor/core-hydration
May 1, 2026
Merged

alxhub merged 1 commit into
angular:mainfrom
SkyZeroZx:refactor/core-hydration

Conversation

@SkyZeroZx

@SkyZeroZx SkyZeroZx commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

This commit updates @defer logic related to incremental hydration to be tree-shakable.

If hydrate triggers are used in a @defer block, the compiler emits a single top-level call to ɵɵenableIncrementalHydrationRuntime, placed once per create block before the first ɵɵdefer that requires it.

As a result, the incremental hydration runtime is only included in the bundle when hydrate is explicitly used.

Considering that the current default behavior of provideClientHydration enables incremental hydration.

Inspired by #52042

@angular-robot angular-robot Bot added the area: core Issues related to the framework runtime label Apr 22, 2026
@ngbot ngbot Bot added this to the Backlog milestone Apr 22, 2026
@SkyZeroZx
SkyZeroZx marked this pull request as ready for review April 22, 2026 21:06
@pullapprove
pullapprove Bot requested a review from crisbeto April 22, 2026 21:06
@JeanMeche
JeanMeche requested review from thePunderWoman and removed request for crisbeto April 22, 2026 21:54
@thePunderWoman

Copy link
Copy Markdown
Contributor

I haven't had a chance to look at this yet, but we should probably make sure this works properly when an incremental hydration block is inside a non-incremental hydration defer block. You should definitely add that test case.

@SkyZeroZx
SkyZeroZx force-pushed the refactor/core-hydration branch from 2df61f7 to ea832a3 Compare April 22, 2026 23:11
@Component({
template: `
@defer (on idle) {
@defer (hydrate on idle) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is within the same template. So it may not adequately test this use case. I would be interested to see a nested @defer(hydrate on idle) where the nested defer is inside a separate component. This immediate case wouldn't work anyway as there's nothing to defer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just updated

@AndrewKushnir
AndrewKushnir self-requested a review April 23, 2026 17:18
@SkyZeroZx
SkyZeroZx force-pushed the refactor/core-hydration branch from ea832a3 to 1a3093a Compare April 23, 2026 17:54
if (rf & 1) {
$r3$.ɵɵdomTemplate(0, MyApp_Defer_0_Template, 1, 0)(1, MyApp_DeferPlaceholder_1_Template, 2, 0);
$r3$.ɵɵdefer(2, 0, null, null, 1, null, null, null, null, 1);
$r3$.ɵɵdefer(2, 0, null, null, 1, null, null, null, null, 1, $r3$.ɵɵenableIncrementalHydrationRuntime);

@AndrewKushnir AndrewKushnir Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be great to generate a call to an extra side-effectful function as a separate top-level call like this:

$r3$.ɵɵenableIncrementalHydrationRuntime();
$r3$.ɵɵdefer(2, 0, null, null, 1, null, null, null, null, 1);

In this case the function should be present only once in a "create" block (before the first ɵɵdefer instruction in that "create" block), even if there are multiple defer blocks that require hydration code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good option, i updated

This commit updates `@defer` logic related to incremental hydration to be tree-shakable.

If hydrate triggers are used in a `@defer` block, the compiler emits a single top-level call to `ɵɵenableIncrementalHydrationRuntime`, placed once per create block before the first `ɵɵdefer` that requires it.

As a result, the incremental hydration runtime is only included in the bundle when hydrate is explicitly used.
@SkyZeroZx
SkyZeroZx force-pushed the refactor/core-hydration branch from 1a3093a to 065031a Compare April 23, 2026 20:18

@thePunderWoman thePunderWoman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for this!

reviewed-for: fw-general, size-tracking, public-api

@JeanMeche JeanMeche added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Apr 30, 2026
@thePunderWoman thePunderWoman added target: major This PR is targeted for the next major release and removed target: patch This PR is targeted for the next patch release labels May 1, 2026
@alxhub
alxhub merged commit 1172150 into angular:main May 1, 2026
21 checks passed
@alxhub

alxhub commented May 1, 2026

Copy link
Copy Markdown
Member

This PR was merged into the repository. The changes were merged into the following branches:

@angular-automatic-lock-bot

Copy link
Copy Markdown

This pull request 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.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Jun 1, 2026
@SkyZeroZx
SkyZeroZx deleted the refactor/core-hydration branch November 15, 2026 16:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants