From f30a4072cdad1748b0dc46e92e905123ade4963a Mon Sep 17 00:00:00 2001 From: Andrew Kushnir Date: Thu, 16 Mar 2023 16:25:05 -0700 Subject: [PATCH] refactor(core): store segment heads for empty comtainers (#49455) This commit updates the logic to store references to the comment (anchor) node as a segment head, when a container is empty (no views). Previously, the logic verified whether a container is non-empty and only stored segment head in this case. The problem is that the ViewContainerRef later on uses the segment head to start the DOM lookup and as a result, the process can not be started, since we have `null`. Now the segment head reference would point to a comment node (the anchor node) and ViewContainerRef would be able to start the lookup process. For empty views, the segment head would become an anchor of the container (which is exactly what we need). PR Close #49455 --- packages/core/src/render3/instructions/element_container.ts | 5 +---- packages/core/src/render3/instructions/template.ts | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/core/src/render3/instructions/element_container.ts b/packages/core/src/render3/instructions/element_container.ts index 384ba50d8c2..34f263a9d8b 100644 --- a/packages/core/src/render3/instructions/element_container.ts +++ b/packages/core/src/render3/instructions/element_container.ts @@ -186,10 +186,7 @@ function locateOrCreateElementContainerNode( 'Unexpected state: hydrating an , ' + 'but no hydration info is available.'); - // If this container is non-empty, store the first node as a segment head, - // otherwise, this node is an anchor and segment head doesn't exist (thus `null`). - const segmentHead = ngContainerSize > 0 ? currentRNode : null; - setSegmentHead(hydrationInfo, index, segmentHead); + setSegmentHead(hydrationInfo, index, currentRNode); comment = siblingAfter(ngContainerSize, currentRNode)!; if (ngDevMode) { diff --git a/packages/core/src/render3/instructions/template.ts b/packages/core/src/render3/instructions/template.ts index 75136210fcc..a2a78ceb310 100644 --- a/packages/core/src/render3/instructions/template.ts +++ b/packages/core/src/render3/instructions/template.ts @@ -140,11 +140,8 @@ function locateOrCreateContainerAnchorImpl( const currentRNode = locateNextRNode(hydrationInfo, tView, lView, tNode)!; ngDevMode && validateNodeExists(currentRNode); + setSegmentHead(hydrationInfo, index, currentRNode); const viewContainerSize = calcSerializedContainerSize(hydrationInfo, index); - // If this container is non-empty, store the first node as a segment head, - // otherwise, this node is an anchor and segment head doesn't exist (thus `null`). - const segmentHead = viewContainerSize > 0 ? currentRNode : null; - setSegmentHead(hydrationInfo, index, segmentHead); const comment = siblingAfter(viewContainerSize, currentRNode)!; if (ngDevMode) {