Skip to content

Components created with createComponent function are not being hydrated properly #61231

Open

Description

@davidbusuttil

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

core

Is this a regression?

No

Description

Components created with the createComponent function are being generated on the server and have ng-version and ngh.

When the client is loaded and tries to hydrate, it regenerates the components, resulting in duplicate content.

Setting ngSkipHydration helps to not have duplicate content, but directives do not work.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-mtwt34g2

Please provide the exception or error you saw

None

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

Angular CLI: 19.2.7
Node: 20.19.1
Package Manager: npm 10.8.2
OS: linux x64

Angular: 19.2.6
... common, compiler, compiler-cli, core, platform-browser
... platform-browser-dynamic, platform-server, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1902.7
@angular-devkit/build-angular   19.2.7
@angular-devkit/core            19.2.7
@angular-devkit/schematics      19.2.7
@angular/cli                    19.2.7
@angular/ssr                    19.2.7
@schematics/angular             19.2.7
rxjs                            7.8.2
typescript                      5.8.3

Anything else?

No response

Activity

  1. changed the title [-]createComponent is not being hydrated[/-] [+]Components created with createComponent function are not being hydrated properly[/+] on May 9, 2025
  2. added
    area: core
    on May 9, 2025
  3. modified the milestone: needsTriage on May 9, 2025
  4. thePunderWoman commented on May 9, 2025

    @thePunderWoman
    Contributor

    Thanks for the report. I have suspicions that this pattern is resulting in a similar behavior to #50543 where the content is detached when the hydration process is occurring, which results in a failure to hydrate. I'll do some investigating.

  5. thePunderWoman commented on May 9, 2025

    @thePunderWoman
    Contributor

    @davidbusuttil While your repro is helpful, it's far from a minimal reproduction, which is what we typically ask for. You should be able to create one with just your home component and not including all the rest of it.

  6. davidbusuttil commented on May 9, 2025

    @davidbusuttil
    Author

    @davidbusuttil While your repro is helpful, it's far from a minimal reproduction, which is what we typically ask for. You should be able to create one with just your home component and not including all the rest of it.

    Ready-I modified the original repro. Thanks a lot for seeing into it!

  7. thePunderWoman commented on May 9, 2025

    @thePunderWoman
    Contributor

    So this is a different situation than what I thought it might be. You're seeing ngh attributes on nodes but the id is 0, with no actual _nghData in the footer. It's just an empty object. Looking further, when we attempt to serialize the component view for the components you created with createComponent, there's no data on those components to serialize, which is what is producing the empty object.

    So at the moment, I'm sorry to say this pattern is incompatible with hydration.

  8. davidbusuttil commented on May 9, 2025

    @davidbusuttil
    Author

    So this is a different situation than what I thought it might be. You're seeing ngh attributes on nodes but the id is 0, with no actual _nghData in the footer. It's just an empty object. Looking further, when we attempt to serialize the component view for the components you created with createComponent, there's no data on those components to serialize, which is what is producing the empty object.

    So at the moment, I'm sorry to say this pattern is incompatible with hydration.

    Thanks a lot for looking into it. It was my last hope for creating a CMS that accepts HTML that can be parsed into components, which benefit from directives such as NgClass, NgOptimizedImage, and RouterLink (and RouterLinkActive by extension). I had a look at what is being done on angular.dev, and in docs-viewer, it is just innerHtml at the moment.

  9. thePunderWoman commented on May 9, 2025

    @thePunderWoman
    Contributor

    The other problem you'd run into here is that createComponent always creates a root component, which is the equivalent to what bootstrapApplication does. That's likely not what you'd want.

  10. davidbusuttil commented on May 9, 2025

    @davidbusuttil
    Author

    Exactly. I am using createComponent instead of ViewContainerRef.createComponent solely for the purpose that the former accepts a hostElement parameter, which allows me to mount the component on a valid HTML tag (for example p). This way I get:

    <p>
      <span>Text</span>
    </p>
    

    instead of:

    <app-paragraph>
      <p>
        <app-span><span>Text</span></app-span>
      </p>
    </app-paragraph>
    

    Then, I am using host and hostDirectives together with multiple input signals (set via setInput) in the component to apply directives, classes, attributes, etc.

  11. thePunderWoman commented on May 9, 2025

    @thePunderWoman
    Contributor

    Right, but in your original repo, you were using it to arbitrarily create things like headers, paragraphs, etc. That would result in each one of those nodes being a root component with all the attributes applied to your typical root node in an application. So each of those nodes is considered a root node, which would affect how Angular treats them.

  12. davidbusuttil commented on May 9, 2025

    @davidbusuttil
    Author

    In fact, I am getting ng-version on each node.

    I know that Angular supports classes as component selectors: Types of Selectors.

    I will try something different and revert.

  13. davidbusuttil commented on May 9, 2025

    @davidbusuttil
    Author

    I created this repo: https://stackblitz.com/edit/stackblitz-starters-9nxu5lux.

    Here, I am using ViewContainerRef.createComponent. Like createComponent, they both have ng-version and ngh.

    These are being hydrated properly and there are no duplicates.

    In theory, ViewContainerRef.createComponent can be used instead of createComponent. Although you can pass projectableNodes as a parameter from an AST generated using parseTemplate, I don’t know how their input can be bound.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area: corecore: hydration

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions