Skip to content

Skipping hydration on i18n nodes breaks hydration #57105

Closed

Description

@henry-alakazhang

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

core

Is this a regression?

No

Description

When enabling hydration + i18n support, adding i18n to elements that have hydration skipped breaks hydration on the client side due to missing root nodes.

The actual error thrown varies based on where the skipHydration attribute and i18n nodes are:

Attributes on the same component:

<app-text i18n ngSkipHydration> Text </app-text>

throws ERROR Error: ASSERTION ERROR: Expected root i18n node during hydration [Expected=> null != null <=Actual]

ngSkipHydration attribute on the parent of an i18n node:

<app-text ngSkipHydration>
  <div i18n>Text</div>
</app-text>

throws TypeError: rootNode is null

ngSkipHydration hostbound attribute on the parent of an i18n node: (ie. app-unhydrated-text has @HostBinding('attr.ngSkipHydration'))

<app-unhydrated-text>
  <div i18n>Title!</div>
</app-unhydrated-text>

throws NG0502: During hydration Angular was unable to locate a node using the "firstChild" path, starting from the <app-unhydrated-text ngskiphydration="true">…</app-unhydrated-text> node.

The only combination that works is applying i18n outside the area that's skipped:

<div i18n>
  <app-text ngSkipHydration> Text </app-text>
</div>

This only applies to i18n nodes in the same template as the elements which have hydration skipped. i18n nodes deeper into the tree are also fine (ie. here, app-has-i18n has i18n tags in its template)

<app-has-i18n ngSkipHydration />

Please provide a link to a minimal reproduction of the bug

https://github.com/henry-alakazhang/angular-i18n-skiphydration-repro

Please provide the exception or error you saw

No response

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

Angular CLI: 18.1.1
Node: 18.20.3
Package Manager: yarn 1.22.19
OS: darwin arm64

Angular: 18.1.1
... animations, cli, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic
... platform-server, router, ssr

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1801.1
@angular-devkit/build-angular   18.1.1
@angular-devkit/core            18.1.1
@angular-devkit/schematics      18.1.1
@schematics/angular             18.1.1
rxjs                            7.8.1
typescript                      5.5.4
zone.js                         0.14.8

Anything else?

I fiddled a bit with the code and adding an if (isInSkipHydrationBlock()) guard inside prepareprepareI18nBlockForHydrationImpl in hydration/i18n.ts stops the issue from happening (and the page seems to render / hydrate correctly). Don't have the context of the conventions/though to know whether that's the right solution and/or would have any side effects

Activity

  1. added
    area: i18n
    area: core
    on Jul 24, 2024
  2. modified the milestone: needsTriage on Jul 24, 2024
  3. added
    P3
    on Jul 24, 2024
  4. modified the milestones: needsTriage, Backlog on Jul 24, 2024
  5. henry-alakazhang commented on Jul 25, 2024

    @henry-alakazhang
    ContributorAuthor

    Noting that the same Expected root i18n node during hydration happens when adding i18n to a node that's disconnected from the DOM and, similarly, adding a guard in prepareI18nBlockForHydration seems to prevent the error.

    Something like this seems to fix both use cases for me: freelancer@01c43b0

  6. self-assigned this
    on Aug 7, 2024
  7. AndrewKushnir commented on Aug 8, 2024

    @AndrewKushnir
    Contributor

    @henry-alakazhang thanks for reporting the issue and providing a repro, I've created a PR #57299 with the fix.

  8. henry-alakazhang commented on Aug 8, 2024

    @henry-alakazhang
    ContributorAuthor

    @AndrewKushnir nice, thanks! Will the PR also handle similar errors with disconnected nodes, or should I create a separate issue for that?

    eg. this:

    @Component({
      imports: [NgIf],
      selector: 'app-projected',
      template: `
        <ng-container *ngIf="false">
          <ng-content></ng-content>
        </ng-container>
      `,
      standalone: true,
    })
    export class ProjectedComponent {}
    <app-projected>
      <div i18n>This content is not projected</div>
    </app-projected>

    also throws ASSERTION ERROR: Expected root i18n node during hydration [Expected=> null != null <=Actual].

  9. AndrewKushnir commented on Aug 8, 2024

    @AndrewKushnir
    Contributor

    @henry-alakazhang

    Will the PR also handle similar errors with disconnected nodes, or should I create a separate issue for that?

    No, the scenario that you've mentioned would not be covered by the fix, unfortunately. Please create a new ticket with a repro and we'll perform further investigation. Thank you.

  10. added 3 commits that reference this issue on Aug 8, 2024
    d0a736e
    810f76f
    5558e27
  11. added a commit that references this issue on Aug 9, 2024
    45212c7
  12. added a commit that references this issue on Aug 30, 2024
    cfa8aa1
  13. angular-automatic-lock-bot commented on Sep 9, 2024

    @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.

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

Metadata

Metadata

Assignees

Labels

P3area: corearea: i18nbugcore: hydrationstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions