Visitar URL original
docs: add hydration guide by jessicajaniuk · Pull Request #49665 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20260503140702/https://github.com/angular/angular/pull/49665
Skip to content

docs: add hydration guide#49665

Closed
jessicajaniuk wants to merge 1 commit intoangular:mainfrom
jessicajaniuk:hydration-guide
Closed

docs: add hydration guide#49665
jessicajaniuk wants to merge 1 commit intoangular:mainfrom
jessicajaniuk:hydration-guide

Conversation

@jessicajaniuk
Copy link
Copy Markdown
Contributor

This adds a guide for how to use hydration in Angular.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Documentation content changes

Does this PR introduce a breaking change?

  • Yes
  • No

@jessicajaniuk jessicajaniuk added area: docs target: major This PR is targeted for the next major release labels Mar 31, 2023
@ngbot ngbot Bot added this to the Backlog milestone Mar 31, 2023
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 31, 2023 •

Deployed aio for 69b21fc to: https://ng-dev-previews-fw--pr-angular-angular-49665-yy4d3k9k.web.app

Note: As new commits are pushed to this pull request, this link is updated after the preview is rebuilt.

Copy link
Copy Markdown
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Looks great 👍

Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
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.

Keep in mind that adding ngSkipHydration to your root application component would effectively disable hydration for your entire application.

@jessicajaniuk It should do it now (once PR #49675 lands) :)

@jessicajaniuk jessicajaniuk force-pushed the hydration-guide branch 6 times, most recently from eb33f36 to 7132ffd Compare April 3, 2023 19:03
@jessicajaniuk jessicajaniuk marked this pull request as ready for review April 3, 2023 19:04
@jessicajaniuk jessicajaniuk added the action: review The PR is still awaiting reviews from at least one requested reviewer label Apr 3, 2023
@pullapprove pullapprove Bot requested a review from mgechev April 3, 2023 19:04
@jessicajaniuk jessicajaniuk requested review from MarkTechson and removed request for mgechev April 3, 2023 19:11
Copy link
Copy Markdown
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @jessicajaniuk 👍

Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
Comment thread aio/content/guide/hydration.md Outdated
This adds a guide for how to use hydration in Angular.
@jessicajaniuk jessicajaniuk added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Apr 3, 2023
@jessicajaniuk
Copy link
Copy Markdown
Contributor Author

Caretaker: this is ready for merge. Any feedback from @MarkTechson or @kara will be addressed in follow up PRs.

Copy link
Copy Markdown
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

LGTM, some nits for a follow-up


## Why is hydration important?

Hydration improves application performance by avoiding extra work to re-create DOM nodes. Instead, Angular tries to match existing DOM elements to the applications structure at runtime and reuses DOM nodes when possible. This results in a performance improvement that can be measured using Core Web Vitals (CWV) statistics, such as reducing the First Input Delay (FID) and Largest Contentful Paint (LCP), as well as Cumulative Layout Shift (CLS). Improving these numbers also affects things like SEO performance.
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.

Nit: consider linking to the web.dev definitions for LCP, CLS, etc, as some folks may not be familiar (e.g. https://web.dev/lcp/)


Hydration improves application performance by avoiding extra work to re-create DOM nodes. Instead, Angular tries to match existing DOM elements to the applications structure at runtime and reuses DOM nodes when possible. This results in a performance improvement that can be measured using Core Web Vitals (CWV) statistics, such as reducing the First Input Delay (FID) and Largest Contentful Paint (LCP), as well as Cumulative Layout Shift (CLS). Improving these numbers also affects things like SEO performance.

Without hydration enabled, server side rendered applications may see a flicker as the page initially loads due to that re-rendering that occurs. Angular will destroy and re-render the application, which will also negatively impact all of the Core Web Vitals mentioned. The re-rendering means an additional delay for every one of the statistics, and a potential for layout shift with all of the imagery. Hydration prevents that flickering from happening.
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.

Nit: would suggest softening the language around expected CWV diff, since it will depend on the case, eg:

Suggested change
Without hydration enabled, server side rendered applications may see a flicker as the page initially loads due to that re-rendering that occurs. Angular will destroy and re-render the application, which will also negatively impact all of the Core Web Vitals mentioned. The re-rendering means an additional delay for every one of the statistics, and a potential for layout shift with all of the imagery. Hydration prevents that flickering from happening.
Without hydration enabled, Angular will destroy and re-render the application's DOM, which may result in a visible UI flicker. This re-rendering can negatively impact Core Web Vitals like LCP and cause a layout shift. Enabling hydration allows the existing DOM to be re-used and prevents a flicker.


This is because Angular is unaware of these DOM changes and cannot resolve them during the hydration process. Angular will expect a certain structure, but it will encounter a different structure when attempting to hydrate. This mismatch will result in hydration failure and throw a DOM mismatch error ([see below](#errors)).

It is best to refactor your component to avoid this sort of DOM manipulation. Try to use Angular APIs to do this work, if you can. If you cannot refactor this behavior, use the `ngSkipHydration` attribute ([described below](#ngskiphydration)) until you can refactor into a hydration friendly solution.
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.

Nit: It seems like fixing DOM manipulation is buried a bit in the guide. Intuitively, I'd expect this to be in the top level instructions for enabling hydration, as you're very likely to encounter this situation when migrating. Maybe it makes sense to add a final step for "replace direct DOM manipulation with Angular constructs or 'ngSkipHydration'", with a link to the limitations section below?

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've added a bit of a stronger alert talking about this in the follow up PR. PTAL

Comment thread aio/content/navigation.json

<a id="limitations"></a>

## Limitations
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.

Nit: "limitations" sounds a bit scary, esp since parallel DOM structure is a baseline expectation in other SSR frameworks. Would something like "Compatibility" or "Constraints" work?

@dylhunn
Copy link
Copy Markdown
Contributor

dylhunn commented Apr 4, 2023

This PR was merged into the repository by commit 31f4efe.

@dylhunn dylhunn closed this in 31f4efe Apr 4, 2023
@jessicajaniuk jessicajaniuk deleted the hydration-guide branch April 5, 2023 15:01
@angular-automatic-lock-bot
Copy link
Copy Markdown

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.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators May 6, 2023
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 merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants