You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Components created with createComponent function are not being hydrated properly #61231
changed the title [-]createComponent is not being hydrated[/-][+]Components created with createComponent function are not being hydrated properly[/+]on May 9, 2025
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.
@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.
@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!
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.
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.
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.
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:
Then, I am using host and hostDirectives together with multiple input signals (set via setInput) in the component to apply directives, classes, attributes, etc.
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.
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.
Activity
[-]createComponent is not being hydrated[/-][+]Components created with createComponent function are not being hydrated properly[/+]thePunderWoman commentedon May 9, 2025
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.
thePunderWoman commentedon May 9, 2025
@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.
davidbusuttil commentedon May 9, 2025
Ready-I modified the original repro. Thanks a lot for seeing into it!
thePunderWoman commentedon May 9, 2025
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.
davidbusuttil commentedon May 9, 2025
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.
thePunderWoman commentedon May 9, 2025
The other problem you'd run into here is that
createComponentalways creates a root component, which is the equivalent to whatbootstrapApplicationdoes. That's likely not what you'd want.davidbusuttil commentedon May 9, 2025
Exactly. I am using
createComponentinstead ofViewContainerRef.createComponentsolely for the purpose that the former accepts ahostElementparameter, which allows me to mount the component on a valid HTML tag (for examplep). This way I get:instead of:
Then, I am using
hostandhostDirectivestogether with multipleinputsignals (set viasetInput) in the component to apply directives, classes, attributes, etc.thePunderWoman commentedon May 9, 2025
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.
davidbusuttil commentedon May 9, 2025
In fact, I am getting
ng-versionon each node.I know that Angular supports classes as component selectors: Types of Selectors.
I will try something different and revert.
davidbusuttil commentedon May 9, 2025
I created this repo: https://stackblitz.com/edit/stackblitz-starters-9nxu5lux.
Here, I am using
ViewContainerRef.createComponent. LikecreateComponent, they both haveng-versionandngh.These are being hydrated properly and there are no duplicates.
In theory,
ViewContainerRef.createComponentcan be used instead ofcreateComponent. Although you can passprojectableNodesas a parameter from an AST generated usingparseTemplate, I don’t know how theirinputcan be bound.