Skip to content

Hydration error when projecting multiple component nodes into different slots #53246

Closed

Description

@crisbeto

If we have a container component with the following template:

<ng-content select="foo"></ng-content>
<ng-content select="bar"></ng-content>

And we project the following nodes into it where both foo and bar are component host nodes:

<projector-cmp>
  <foo/>
  <bar/>
  <foo/>
</projector-cmp>

Currently the framework throws an error saying that the second <foo/> node can't be hydrated, whereas if the two <foo/> nodes are right after each other, it works fine:

<!-- Works -->
<projector-cmp>
  <foo/>
  <foo/>
  <bar/>
</projector-cmp>

On the other hand, if we change the markup so it's two <bar/> nodes and one <foo/>, the framework throws an error saying that it's "Trying to claim a node, which was claimed already.":

<projector-cmp>
  <foo/>
  <bar/>
  <foo/>
</projector-cmp>

The issue can be reproduced by adding the following test to hydration_spec.ts:

fit('', async () => {
  @Component({
    standalone: true,
    selector: 'projector-cmp',
    template: `
      <ng-content select="foo"></ng-content>
      <ng-content select="bar"></ng-content>
    `,
  })
  class ProjectorCmp {
  }

  @Component({selector: 'foo', standalone: true, template: ''})
  class FooCmp {
  }

  @Component({selector: 'bar', standalone: true, template: ''})
  class BarCmp {
  }

  @Component({
    standalone: true,
    imports: [ProjectorCmp, FooCmp, BarCmp],
    selector: 'app',
    template: `
      <projector-cmp>
        <foo/>
        <bar/>
        <foo/>
      </projector-cmp>
    `,
  })
  class SimpleComponent {
  }

  const html = await ssr(SimpleComponent);
  const ssrContents = getAppContents(html);

  expect(ssrContents).toContain('<app ngh');

  resetTViewsFor(SimpleComponent, ProjectorCmp);

  const appRef = await hydrate(html, SimpleComponent, [withDebugConsole()]);
  const compRef = getComponentRef<SimpleComponent>(appRef);
  appRef.tick();

  verifyHasLog(
      appRef, 'Angular hydrated 5 component(s) and 9 node(s), 0 component(s) were skipped');

  const clientRootNode = compRef.location.nativeElement;
  verifyAllNodesClaimedForHydration(clientRootNode);
  verifyClientAndSSRContentsMatch(ssrContents, clientRootNode);
});

Activity

  1. added
    area: core
    on Nov 29, 2023
  2. added this to the needsTriage milestone on Nov 29, 2023
  3. added
    P2
    on Nov 29, 2023
  4. modified the milestones: needsTriage, Backlog on Nov 29, 2023
  5. added a commit that references this issue on Nov 29, 2023
    6737ecb
  6. added a commit that references this issue on Nov 30, 2023
    4fc1581
  7. angular-automatic-lock-bot commented on Dec 31, 2023

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

  8. locked and limited conversation to collaborators on Dec 31, 2023
  9. added a commit that references this issue on Jan 23, 2024
    3f25b07
  10. added a commit that references this issue on Jan 26, 2024
    1f8143f
  11. added a commit that references this issue on Jan 26, 2024
    861e77c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2area: corecore: hydrationstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions