Skip to content

Remove fragments whose first or last child renders nothing - #968

Merged
schultek merged 6 commits into
schultek:mainfrom
BiAksoy:fix/empty-fragment-edge-removal
Nov 12, 2026
Merged

schultek merged 6 commits into
schultek:mainfrom
BiAksoy:fix/empty-fragment-edge-removal

Conversation

@BiAksoy

@BiAksoy BiAksoy commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Description

When the first or last child of a fragment is a fragment that renders nothing, such as Component.empty() or Component.fragment([]), the client renderer loses track of the fragment's nodes. We ran into it with a routed page built as Component.fragment([header, table, pager]), where the pager returns const Component.empty() when there is only one page. After navigating away, the new page was inserted and the old page's nodes stayed below it.

On main:

  • Replacing Component.fragment([Component.empty(), h1]) leaves the <h1> in the DOM ('<p>Replacement</p><h1>A</h1>').
  • Replacing Component.fragment([h1, Component.empty()]) fails the assertion "Non-empty attached fragments must have a valid last child reference." and also leaves the <h1>. With asserts disabled, removeChildren starts its walk from a null node and removes nothing (from reading the code; the tests run with asserts on).
  • An element added after such a fragment lands before the fragment's nodes ('Start<p>End</p><h1>A</h1>' instead of 'Start<h1>A</h1><p>End</p>'), and moving the fragment trips the same assertion.

Cause

DomRenderFragment.firstChildNode and lastChildNode look only at the first and last child, and return null when that child is an empty fragment:

web.Node? get firstChildNode {
if (_firstChild case final firstChild?) {
if (firstChild is DomRenderFragment) {
return firstChild.firstChildNode;
}
return firstChild.node;
}
return null;
}
web.Node? get lastChildNode {
if (_lastChild case final lastChild?) {
if (lastChild is DomRenderFragment) {
return lastChild.lastChildNode;
}
return lastChild.node;
}
return null;
}

removeChildren and move then treat the fragment as empty, or walk back from a null last node:

if (firstChildNode == null) {
// If fragment is empty, nothing to remove.
return;
}
assert(lastChildNode != null, 'Non-empty attached fragments must have a valid last child reference.');
web.Node? currentNode = lastChildNode;

and getRealNodeOf falls back to the fragment's previous sibling, so a node inserted after the fragment goes in before its nodes:

web.Node? getRealNodeOf(DomRenderObject? after) {
if (after is DomRenderFragment) {
if (after.lastChildNode case final node?) {
return node;
} else {
return getRealNodeOf(after.previousSibling);
}
}

Changes

The first commit makes both getters walk the fragment's children through their sibling links, past children that have no node, up to _firstChild / _lastChild.

That walk depends on the sibling links, which attachChild only updated when it inserted a child's node. It skipped them when a child fragment was moved, and when the child's node was already in place:

if (child case DomRenderFragment(isAttached: true)) {
child.move(this, targetNode, afterNode);
return;
}
try {
final childNode = child.node;
if (childNode.previousSibling == afterNode && childNode.parentNode == targetNode) {
return;
}

A child attached in front also got nextSibling = null, since after was null:

final next = after?.nextSibling;
child.previousSibling = after;
after?.nextSibling = child;
child.nextSibling = next;
next?.previousSibling = child;

So [h1] rebuilt as [Component.empty(), h1], or a keyed reorder that moves an empty fragment to an edge, still lost the fragment's nodes with the first commit alone. The second commit moves the first and last child tracking from DomRenderFragment into MultiChildDomRenderObject. It also links the child on every path through attachChild, unlinking it from its old position first. removeChild and DomRenderFragment.remove use the same unlink step.

The third commit links the child before attachChild resolves the node to place it after. During a keyed reorder, a child whose slot did not change (same index, same previous sibling) is not attached again, so its links can still point at the old position of the child being moved. When that sibling is an empty fragment, getRealNodeOf(after) walks back through those links and can resolve to the moved child itself. Children [b S, span X, empty 1, empty 2, empty 3, b E] rebuilt as [b S, empty 3, empty 1, empty 2, span X, b E] trip "Child node should have been placed after the specified node." and leave the <span> behind on the next replacement. This also happens on main. With the child linked first, everything in front of it is already in its final order when the walk runs. The same commit reads firstChildNode once before the loops in move and removeChildren, since the getter now walks past leading empty children.

Tests

New browser tests in packages/jaspr/test/client/fragments/fragments_browser_test.dart cover an empty fragment first, last, both, nested inside an edge child, and around the only child of a nested fragment. For each case, the tests replace the fragment, insert a sibling after it, and move it as a keyed fragment. Four more cover an empty fragment that reaches an edge through a rebuild (added first or last, or moved first or last by key), followed by an insertion and a replacement. Two cover a hydrated fragment with empty first and last children, and the same kind of fragment moved to another parent with a GlobalKey. One more covers the keyed reorder described above.

dart test -p chrome test/client/fragments/fragments_browser_test.dart in packages/jaspr:

lib result
main (413ad06) 10 passed, 17 failed
first commit 23 passed, 4 failed (empty fragment added first, moved first, moved last; keyed reorder)
first and second commit 26 passed, 1 failed (keyed reorder)
this PR 27 passed

Five of the new tests already pass on main: the four keyed moves other than "empty last" (the sibling moved past the fragment ends up in the right place either way) and inserting after a fragment whose last child is not empty. They are there to guard against regressions.

dart test (vm and chrome) passes in packages/jaspr (382 passed, 3 skipped) and packages/jaspr_router (20 passed). dart analyze --fatal-infos and dart format --set-exit-if-changed report nothing.

Not in this PR: replacing a fragment that is itself the child of another fragment fails the assertion "Cannot remove fragment from a different parent." and leaves its nodes in place. It happens on main too, only with asserts enabled, and has a different cause (the assertion in removeChildren), so I'll open a separate issue for it.

Type of Change

  • 🛠️ Bug fix

Ready Checklist

  • I've read the Contribution Guide.
  • In case this PR changes one of the core packages, I've modified the respective CHANGELOG.md file using
    the semantic_changelog format.
  • I updated/added relevant documentation (doc comments with ///).
  • I added myself to the AUTHORS file (optional, if you want to).

The first and last node lookups of a fragment walk its children's sibling
links, which were only updated when a child's nodes were inserted. After a
keyed move, or a child attached in front of existing ones, the links skipped
children, so an empty fragment that ended up at an edge still hid the
fragment's nodes.
@BiAksoy
BiAksoy requested a review from schultek as a code owner November 3, 2026 14:50
Copilot AI balanced review requested due to automatic review settings November 3, 2026 14:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Package Version Report

The following packages have been updated:
jaspr_test : 0.24.0-beta.0 -> 0.24.0-beta.1
jaspr : 0.24.0-beta.0 -> 0.24.0-beta.1
jaspr_builder : 0.24.0-beta.0 -> 0.24.0-beta.1
jaspr_cli : 0.24.0-beta.0 -> 0.24.0-beta.1
jaspr_router : 0.10.0-beta.0 -> 0.10.0-beta.1

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Hot reload leaves obsolete descendants linked to the surviving root, retaining detached DOM trees across reloads; root child tracking must be cleared and covered by a repeated-reload regression test.

1 open issue

Review effort: Balanced

@schultek schultek left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution, the fix looks good.

The one Copilot comment seems accurate, after this is fixed I will approve.

The root render object survives a reload while every render object below it is replaced without being removed from its parent. Since the root now tracks its first and last child, the replaced ones stayed linked behind the new children, together with their detached DOM nodes, and each reload added another set.
@BiAksoy

BiAksoy commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in a63eb47, with a test for repeated reloads. Also merged main to clear the conflict.

@schultek
schultek requested a balanced review from Copilot November 12, 2026 15:06

@schultek schultek left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the fragment-boundary and child-link bookkeeping fixes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@schultek
schultek merged commit 4d4949f into schultek:main Nov 12, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants