diff --git a/src/diff/children.js b/src/diff/children.js
index 383b95cfdb..a6bc9e20d7 100644
--- a/src/diff/children.js
+++ b/src/diff/children.js
@@ -213,7 +213,7 @@ export function diffChildren(
// If the newParentVNode.__nextDom points to a dom node that is about to
// be unmounted, then get the next sibling of that vnode and set
// _nextDom to it
- newParentVNode._nextDom = newParentVNode._nextDom.nextSibling;
+ newParentVNode._nextDom = getLastDom(oldParentVNode).nextSibling;
}
unmount(oldChildren[i], oldChildren[i]);
@@ -326,3 +326,26 @@ function placeChild(
return oldDom;
}
+
+/**
+ * @param {import('../internal').VNode} vnode
+ */
+function getLastDom(vnode) {
+ if (vnode.type == null || typeof vnode.type === 'string') {
+ return vnode._dom;
+ }
+
+ if (vnode._children) {
+ for (let i = vnode._children.length - 1; i >= 0; i--) {
+ let child = vnode._children[i];
+ if (child) {
+ let lastDom = getLastDom(child);
+ if (lastDom) {
+ return lastDom;
+ }
+ }
+ }
+ }
+
+ return null;
+}
diff --git a/test/browser/fragments.test.js b/test/browser/fragments.test.js
index 2dbbee6aab..e6df094d73 100644
--- a/test/browser/fragments.test.js
+++ b/test/browser/fragments.test.js
@@ -2700,12 +2700,7 @@ describe('Fragment', () => {
render(