Repository navigation
Description
I'm recapping a discussion I just had with @alxhub and @tbosch. This is mostly Tobias' design.
I'm submitting a ... (check one with "x")
[x] feature request
Current behavior
Typically when a page is pre-rendered, such as with Universal, the Angular application bootstraps in the browser, then blows away the pre-rendered content, and replaces it with newly created nodes. The DOM tree that is pre-rendered is often very similar, or identical to the DOM tree created on the front end, but there are issues that make it difficult for Angular to take over the existing DOM tree (otherwise referred to as DOM hydration).
The actual handoff of destroying the old DOM and showing the new DOM is pretty fast and seamless, so it's not necessarily a major UX issue in and of itself. Where it becomes problematic is in cases like ads that load in iframes (which is pretty much all display ads). If these ad iframes are pre-rendered -- which is a business requirement for many publishers -- and the iframe gets moved in the DOM, the iframe will refresh. This causes some ad networks to suspect abuse, as if publishers are trying to sneak more ad views.
Why Not Use the Already-Rendered DOM?
One issue is that with asynchronicity+conditional DOM (i.e. *ngIf="data | async"), the tree in the client may be rendered before the condition is truthy, whereas the pre-rendered version may have the full tree with async data resolved.
Another challenge is that text nodes are not able to be selected by CSS selectors, which would mean the renderer would have to rely on child ordering in order to associate pre-rendered nodes with client-rendered nodes (which is not always correct). Similar challenge goes for elements in an *ngFor, the order must be assumed to be identical.
The renderer would also be responsible for cleaning up pre-rendered orphan nodes. i.e. if 30 items in an *ngFor were pre-rendered, but only 20 were rendered in the client, the additional 10 nodes would need to be removed to prevent unexpected behavior.
Proposal: Optional, explicit, partial DOM Hydration
Allow setting a user-specified attribute on elements to associate the pre-rendered version with client-rendered version. If the renderer comes to a node that it can't associate with an existing node, it will blow away the node and re-create it. The developer would be responsible for setting these ids on the elements they care about. Example:
import { HydrateDomConfig, NgModule } from '@angular/core';
@NgModule({
providers: [
{
provide: HydrateDomConfig,
useValue: {
hydrate: true, // default false for backwards compat
attribute: 'pid', // default 'id'
}
}
]
})Component:
@Component({
template: `
<div pid="header">
<header-ad pid="header-ad"></header-ad>
<div>
<!-- this will get blown away and re-created since it lacks attribute -->
</div>
</div>
`
})This design allows the DomRenderer to traverse the DOM tree and match elements for each branch starting at the root until it can't go any deeper, at which point it would blow away the descendants and re-create them.
Text nodes would all be destroyed and re-created with this design, as well as any node that doesn't have the set attribute, pid.
I don't expect that the rendering would be user-perceivable, other than if there are discrepancies between pre-rendered and client-rendered DOM, but that's a concern even without this feature.
@tbosch & @alxhub please feel free to add anything I missed (or misrepresented).
Activity
pxwise commentedon Dec 14, 2016
Ad wipeout on client bootstrap is a real world issue for us using universal and this hydration proposal should solve it. Our current workaround gets us partway there, moving server DOM into the same position in client rendered DOM but does not outsmart ad verification services that watch for DOM mutations, hiding the ad upon client bootstrap.
Big thumbs up for opt-in hydration.
PatrickJS commentedon Dec 14, 2016
LGTM, Keep in mind this is a huge problem with all js frameworks so for Angular to have a solution is a great win for when comparing different SSR solutions. The real solution is having Ads actually work together with js frameworks but that will never happen, other than AMP. I can see how it would be implemented by rewriting selecting root element and providing a different render path.
For the |async you definitely have that problem with Promises due to the microtask while Observables do return synchronously. On the client, we can assume there will be batched data sent from the server to the client which gives us all of the results immediately. For Universal, we need to have the data available to the client synchronously anyways to reuse the server Http responses
jeffbcross commentedon Dec 14, 2016
An alternative design would be to provide an alternate renderer that would extend
DomRenderer, rather than modifyingDomRendererto behave differently depending on thehydratevalue.PatrickJS commentedon Dec 15, 2016
+1 for DomHydrationRenderer
IgorMinar commentedon Dec 16, 2016
Two things:
playground commentedon Dec 19, 2016
+1 either solution will be beneficial and vital for us to move forward.
tbosch commentedon Dec 28, 2016
We can still make the work generically, when using auto generated ids based on the place in the element hierarchy (and not based on the creation order).
I think we should think this through as well before we decide.
FahadMullaji commentedon Jan 10, 2017
is HydrateDomConfig part of @angular/core? I get error when I try to compile the application.
error TS2305: Module '"/home/fahad/Workspace/siteCuriouss/node_modules/@angular/core/index"' has no exported member 'HydrateDomConfig'.
DzmitryShylovich commentedon Jan 10, 2017
@FahadMullaji it's only a proposal.
josephliccini commentedon Aug 29, 2017
This proposal is super exciting!
Has any thought been given to pre-rendered lazy routes? We can achieve pre rendered lazy routes via https://github.com/angular/universal/blob/master/modules/ng-module-map-ngfactory-loader/README.md
Is it possible for the hydration to occur after lazy route is fetched and then rendering begins?
Just curious on thoughts here.
Universal has been great and straightforward to use so far; thanks to everyone involved!
mcferren commentedon Sep 24, 2017
What couples the ComponentRef obj to the Dom string (cmpref.changeDetectorRef.rootNodes[0] & cmpref.hostView.rootNodes[0] ect)? Is there an explicit reference(i.e. dom string selector) or does this binding live in memory? Is there a possibility of calling createComponent with an existing dom node as an argument?
168 remaining items
PatrickJS commentedon Feb 17, 2024
can we close this issue. there's no reason to keep this open
princ09 commentedon Feb 17, 2024
princ09 commentedon Feb 17, 2024
denisyilmaz commentedon Feb 17, 2024
princ09 commentedon Feb 17, 2024
denisyilmaz commentedon Feb 17, 2024
bierzorutas commentedon Feb 17, 2024
princ09 commentedon Feb 17, 2024
Thanks @bierzorutas and @denisyilmaz for your input.
@denisyilmaz that font flickering is happening because of font-display : swap property. That can be another issue with hydration.
I would love to hear from contributor and developer of Angular, please have look into this code. Let me know what I am missing here.
alxhub commentedon Mar 30, 2024
Closing as resolved :)
Note that the original issue is not describing partial hydration, but a workaround for the issues with the destructive "hydration" that Angular had originally, to allow partial reuse of some of the DOM during the client-side bootstrapping.
Partial hydration is indeed on our roadmap, but that's not what this issue tracks :)
PatrickJS commentedon Mar 30, 2024
yes, the future merging with Wiz is amazing.
Everything I wanted for Angular Universal is finally happening and it's exciting to see 🎉
There are more things, I'll share in the future, that are only possible with Resuming frameworks and Signals so I'm glad Angular can be part of that 🙌
I want to thank everyone who believed in me with Angular Universal and the vision I had for it.
Angular has a bright future ahead 🥂
JeanMeche commentedon Mar 30, 2024
naveedahmed1 commentedon Mar 30, 2024
Wow, what a historic moment! Farewell, dear issue! 🎉 It feels like saying goodbye to an old friend we've been following since 2016. The bond we've formed over the years, the ups and downs, the anticipation... it's been quite the journey! 😄 Though I'll miss our regular updates, I'm excited for the next chapter in Angular's evolution. Cheers to the closure and onwards to partial hydration! 🚀 #Goodbye2016Issue #HelloPartialHydration
angular-automatic-lock-bot commentedon Apr 30, 2024
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.