Skip to content

Projecting projected content through an ng-template causes Cannot read properties of null (reading 'parentElement') during hydration annotation #56750

Closed

Description

@henry-alakazhang

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

core

Is this a regression?

No

Description

A bit of a a mouthful of a ticket name, but this was the best way I could summarise it. If you project an ng-content into an ng-content that's in an ng-template (including for ngIf or @if blocks), hydration will fail with Cannot read properties of null (reading 'parentElement'). This has occurred in a few places in my codebase which has a large component library featuring some complex components and variants of those components.

For example, consider a component with this template:

<ng-content select="app-dropdown-trigger"></ng-content>
  <!-- this would normally be dynamic -->
@if(false) {
  <ng-content select="app-dropdown-content"></ng-content>
}

Using this component directly and projecting content into the ngIf works fine and the component hydrates as expected. (note: app-dropdown-content also contains an ng-content here, so this is two levels of projection)

However, if you add a wrapper component that makes a special case of it and projects into the app-dropdown-content, with a template like this:

<app-dropdown>
  <app-dropdown-trigger>More</app-dropdown-trigger>
  <app-dropdown-content>
    <ng-content />
  </app-dropdown-content>
</app-dropdown>

Using this new component causes the server to fail with Cannot read properties of null (reading 'parentElement'). This throws an actual error during SSR annotation and doesn't return a response.

This only happens when hydration is enabled on the element. Skipping hydration on the inner component (app-dropdown, in this case) doesn't fix the issue, but skipping hydration on the outer component does.

Please provide a link to a minimal reproduction of the bug

https://github.com/henry-alakazhang/hydration-form-repro/tree/content-content-issue

Please provide the exception or error you saw

Cannot read properties of null (reading 'parentElement')

    at navigateBetween (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:14843:52)
    at calcPathBetween (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:14890:18)
    at calcPathForNode (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:14943:16)
    at appendSerializedNodePath (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:37251:35)
    at conditionallyAnnotateNodePath (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:37463:9)
    at serializeLView (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:37374:9)
    at annotateHostElementForHydration (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:37502:21)
    at serializeLView (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:37401:17)
    at annotateHostElementForHydration (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:37502:21)
    at annotateComponentLViewForHydration (~/hydration-form-repro/node_modules/@angular/core/fesm2022/core.mjs:37079:16

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

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

Angular: 18.0.5
... animations, cdk, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.2
@angular-devkit/build-angular   18.0.2
@angular-devkit/core            18.0.6
@angular-devkit/schematics      18.0.6
@angular/cli                    18.0.6
@angular/localize               18.0.1
@angular/ssr                    18.0.6
@schematics/angular             18.0.6
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.6

Anything else?

No response

Activity

  1. added
    area: core
    on Jun 28, 2024
  2. added this to the needsTriage milestone on Jun 28, 2024
  3. sibranation commented on Jul 10, 2024

    @sibranation

    got the same issue here

    image
  4. sibranation commented on Jul 17, 2024

    @sibranation

    solved by adding this in ts

    @ContentChildren('horizontalCardCustomContent', { descendants: true, read: ElementRef }) horizontalCardCustomContent!: QueryList<ElementRef>;
      @ContentChildren('horizontalCardCustomCta', { descendants: true, read: ElementRef }) horizontalCardCustomCta!: QueryList<ElementRef>;
      @ContentChildren('horizontalCardCustomImageContent', { descendants: true, read: ElementRef }) horizontalCardCustomImageContent!: QueryList<ElementRef>;
    
      contentFlags = {
        customContent: false,
        customCta: false,
        customImageContent: false,
      };
      
      ngAfterContentInit() {
        this.contentFlags.customContent = this.horizontalCardCustomContent.length > 0;
        this.contentFlags.customCta = this.horizontalCardCustomCta.length > 0;
        this.contentFlags.customImageContent = this.horizontalCardCustomImageContent.length > 0;
      }
    

    and use this in html

       <ng-container *ngIf="contentFlags.customImageContent">
          <hd-base-hc-custom-image-content>
            <ng-content select="horizontal-card-custom-image-content"></ng-content>
          </hd-base-hc-custom-image-content>
        </ng-container>
    
        <ng-container *ngIf="contentFlags.customCta">
          <hc-hd-base-hc-custom-cta>
            <ng-content select="horizontal-card-custom-cta"></ng-content>
          </hc-hd-base-hc-custom-cta>
        </ng-container>
    
        <ng-container *ngIf="contentFlags.customContent">
          <hc-hd-base-hc-custom-content>
            <ng-content select="horizontal-card-custom-content"></ng-content>
          </hc-hd-base-hc-custom-content>
        </ng-container>
    
  5. added a commit that references this issue on Aug 14, 2024
    f241b50
  6. added
    P4
    on Aug 14, 2024
  7. modified the milestones: needsTriage, Backlog on Aug 14, 2024
  8. AndrewKushnir commented on Aug 14, 2024

    @AndrewKushnir
    Contributor

    @henry-alakazhang thanks for reporting the issue and providing a repro. I've created PR #57383 with the fix. The PR will go through code review and testing procedures and will be included into one of the future releases. Thank you.

  9. added a commit that references this issue on Aug 15, 2024
    286012f
  10. added a commit that references this issue on Aug 15, 2024
    d4449fc
  11. added a commit that references this issue on Aug 30, 2024
    e9c9b45
  12. angular-automatic-lock-bot commented on Sep 15, 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.

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

Metadata

Metadata

Assignees

Labels

P4area: corecore: hydrationstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions