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
ng-template gets rendered twice during hydration (when anchor comment node is not projected) #50543
Which @angular/* package(s) are the source of the bug?
platform-browser
Is this a regression?
No
Description
when hydration is enabled and a component uses ngTemplateOutlet to render a template passed from a parent e.g. via ContentChild then its getting rendered twice for the duration of the hydration.
So it looks the like this if 'abc' is the template content
received from server abc
during hydration abc abc <-- this looks sometimes a bit ugly
after hydration abc
So is it possible that the passed template gets just once rendered during the hydration process?
@michael-hein thanks for reporting the issue and providing a repro, this is very helpful.
I've performed the investigation and found out that what happens is that new nodes are being created for the template instead of reusing existing ones (hence, the duplication), but since you have the setTimeout in the code, it delays the time when hydration kicks off the cleanup operation (is depends on the ApplicationRef.isStable). As a result, the cleanup operation happens a bit later, thus leaving duplicated content visible. During the cleanup run, the SSR'ed content gets removed, since it was not claimed.
I further looked into why existing nodes were not picked up by the hydration and found out that the "anchor" comment node for the projected <ng-template> is considered "disconnected" from the DOM (thus hydration can not find it) and that results in the "regular rendering" codepath. In fact, the "anchor" comment node is really disconnected from the DOM tree, since there was no <ng-content> tag present in the ShellComponent component template. This is not a problem in a simple case, but for more complex scenarios and for hydration purposes, that "anchor" node should be present in live DOM.
To fix the problem, you can add the <ng-content /> tag to the ShellComponent component template. In this case, the comment node would be content-projected (added to the live DOM) and hydration will pick it up.
(adding P4 priority for now and leaving this ticket open to discuss further with the team whether we need to improve this behavior)
Reacted by michael-hein, Gary Barzola, Leblanc Meneses, Sam Lin and Sercan Bayrambey
changed the title [-]ng-template gets rendered twice during hydration[/-][+]ng-template gets rendered twice during hydration (when anchor comment node is not projected)[/+]on Jun 1, 2023
@AndrewKushnir I have tried the <ng-content /> without success. Perhaps I am not doing something wrong. In my scenario I have
<ng-template #template>
<ng-container *ngIf="googleBusiness">
<li>
Google Business info 1
</li>
<li>
Google Business info 2
</li>
</ng-container>
</ng-template>
Essentially I want to remove the component tag from being rendered since that component adds two
items to an existing list. I have added the <ng-content /> tag inside the ng-template, and outside. Do I have to do anything else to prevent the component being rendered twice?
@CarlosTorrecillas the problem that you describe might be different from the one reported here. Could you please try to update your app to v16.1.1 (most recent at this moment) and test the behavior once again? If the problem still exists, please create a minimal repro (in a form of a GitHub repository with a new ng new app) and submit a new ticket, we'll take a look. Thank you.
We are building a large ecommerce application using PrimeNG as well and are now running into this issue when we're using <ng-template />. Is there any plans from the Angular team to address this anytime soon? It's been almost 2 years.
Is there any update or planned milestone for improving hydration behavior in these scenarios? This issue affects some common patterns in SSR-heavy apps where conditional rendering is handled within ng-template.
Hey everyone. We were able to figure out a fix for this and it will be released with the patch that goes out next week Wednesday. Thanks for your patience!
Reacted by Troy Steuwer, michael-hein, Jeff Chan, J Mewes, Artak and Manuel Andres
Activity
dylhunn commentedon Jun 1, 2023
(@jessicajaniuk and @AndrewKushnir for visibility.)
AndrewKushnir commentedon Jun 1, 2023
@michael-hein thanks for reporting the issue and providing a repro, this is very helpful.
I've performed the investigation and found out that what happens is that new nodes are being created for the template instead of reusing existing ones (hence, the duplication), but since you have the
setTimeoutin the code, it delays the time when hydration kicks off the cleanup operation (is depends on theApplicationRef.isStable). As a result, the cleanup operation happens a bit later, thus leaving duplicated content visible. During the cleanup run, the SSR'ed content gets removed, since it was not claimed.I further looked into why existing nodes were not picked up by the hydration and found out that the "anchor" comment node for the projected
<ng-template>is considered "disconnected" from the DOM (thus hydration can not find it) and that results in the "regular rendering" codepath. In fact, the "anchor" comment node is really disconnected from the DOM tree, since there was no<ng-content>tag present in theShellComponentcomponent template. This is not a problem in a simple case, but for more complex scenarios and for hydration purposes, that "anchor" node should be present in live DOM.To fix the problem, you can add the
<ng-content />tag to theShellComponentcomponent template. In this case, the comment node would be content-projected (added to the live DOM) and hydration will pick it up.(adding P4 priority for now and leaving this ticket open to discuss further with the team whether we need to improve this behavior)
[-]ng-template gets rendered twice during hydration[/-][+]ng-template gets rendered twice during hydration (when anchor comment node is not projected)[/+]michael-hein commentedon Jun 2, 2023
thanks for the fast response and for looking into it. I really appreciate it :)
Yes the
setTimeoutwas only to better see it, that was rendered twice.With the
<ng-content />tag it works perfectly. Thank you!CarlosTorrecillas commentedon Jun 19, 2023
@AndrewKushnir I have tried the
<ng-content />without success. Perhaps I am not doing something wrong. In my scenario I haveAnd my component looks like:
Essentially I want to remove the component tag from being rendered since that component adds two
<ng-content />tag inside theng-template, and outside. Do I have to do anything else to prevent the component being rendered twice?AndrewKushnir commentedon Jun 19, 2023
@CarlosTorrecillas the problem that you describe might be different from the one reported here. Could you please try to update your app to v16.1.1 (most recent at this moment) and test the behavior once again? If the problem still exists, please create a minimal repro (in a form of a GitHub repository with a new
ng newapp) and submit a new ticket, we'll take a look. Thank you.14 remaining items
jwittekind commentedon Dec 6, 2024
+1 I also experience this issue with
<ng-content/>,<ng-template/>or custom templateRefs using@ContentChild.Please fix this issue
tsteuwer-accesso commentedon Apr 30, 2025
We are building a large ecommerce application using PrimeNG as well and are now running into this issue when we're using
<ng-template />. Is there any plans from the Angular team to address this anytime soon? It's been almost 2 years.artakvzn commentedon May 6, 2025
Is there any update or planned milestone for improving hydration behavior in these scenarios? This issue affects some common patterns in SSR-heavy apps where conditional rendering is handled within
ng-template.fix(core): Fixes template outlet hydration
fix(core): Fixes template outlet hydration
fix(core): Fixes template outlet hydration
fix(core): Fixes template outlet hydration
fix(core): Fixes template outlet hydration (#62012)
fix(core): Fixes template outlet hydration (angular#61989)
thePunderWoman commentedon Jun 12, 2025
Hey everyone. We were able to figure out a fix for this and it will be released with the patch that goes out next week Wednesday. Thanks for your patience!
tsteuwer-accesso commentedon Jun 12, 2025
Thank you @thePunderWoman !!!!
angular-automatic-lock-bot commentedon Jul 13, 2025
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.