Skip to content

Conversation

@linzhe141
Copy link
Contributor

@linzhe141 linzhe141 commented Dec 8, 2025

close #14173

This is a follow-up to this PR #13560.

The following issue examples are just a sufficiently complex case to describe the current issues with patchKeyedChildren + async component wrapper, this maby a edge case.

this pr playground

issue playground

Summary by CodeRabbit

  • Bug Fixes

    • Improved placeholder resolution for nested/wrapped async components so Suspense handles complex component hierarchies and nested placeholders more reliably during rendering and updates.
  • Tests

    • Added coverage for nested async components rendered via v-for with additional wrapper layers, validating initial render and subsequent list and item updates.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 8, 2025

Walkthrough

Replaces a direct fallback to an anchor vnode placeholder with an internal resolver that recursively inspects component wrapper VNodes to locate async component placeholders; adds a test exercising Suspense + v-for with nested async wrappers and list updates.

Changes

Cohort / File(s) Summary
Runtime: async placeholder resolution
packages/runtime-core/src/renderer.ts
Added internal helper resolveAsyncComponentPlaceholder(anchorVnode: VNode) that returns anchorVnode.placeholder or recursively inspects wrapper component subTrees to locate a placeholder; replaced direct fallback (`anchorVNode.el
Tests: Suspense + async wrappers
packages/runtime-core/__tests__/components/Suspense.spec.ts
Added a new test that renders multiple asynchronously wrapped components inside <Suspense> with v-for and keys, then updates the list and item data to assert correct DOM order and reactive updates through nested wrappers.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review focus:
    • Correctness and termination of resolveAsyncComponentPlaceholder recursion (nested wrapper traversal, null handling).
    • Ensure all renderer anchor-resolution paths behave correctly for anchors, placeholders, and wrapped components.
    • Validate test determinism and coverage for the linked issue (#14173).

Possibly related PRs

Suggested labels

scope: suspense

Poem

🐇 I hopped through wrappers, quiet and light,
Searched for the placeholder hidden from sight.
Keys held their places, order stayed true,
A tiny rabbit fix — and rendering too. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the core fix: ensuring correct anchor element resolution for unresolved async components nested deeper in component wrappers.
Linked Issues check ✅ Passed The code changes directly address the linked issue #14173 by fixing anchor element resolution for deeply wrapped async components in v-for loops with Suspense.
Out of Scope Changes check ✅ Passed All changes are tightly scoped to fixing the async component anchor element issue: internal helper function and test case with no unrelated modifications.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 13995cb and 82a13bb.

📒 Files selected for processing (1)
  • packages/runtime-core/__tests__/components/Suspense.spec.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/runtime-core/__tests__/components/Suspense.spec.ts (3)
packages/runtime-core/src/components/Suspense.ts (1)
  • Suspense (131-142)
packages/runtime-core/src/vnode.ts (1)
  • Fragment (63-68)
packages/runtime-test/src/serialize.ts (1)
  • serializeInner (22-33)
🔇 Additional comments (1)
packages/runtime-core/__tests__/components/Suspense.spec.ts (1)

2363-2455: New nested async + Suspense + v-for test accurately covers the #14173 regression

The new test constructs a realistic chain of async wrappers (CompAsyncWrapper, CompWrapper) around a Suspense-wrapped v-for of async components (CompAsyncSetup), then exercises:

  • Initial resolution of all async layers,
  • Replacing the outer keyed list (list update),
  • Mutating the inner items array (items update),

while asserting both ordering and content for each phase. The double await Promise.all(deps) pattern per phase matches the two async layers introduced by the local defineAsyncComponent helper and is consistent with similar nested-async tests earlier in this file. Overall, this is a focused, deterministic regression test that should robustly guard the fixed anchor-resolution behavior in patchKeyedChildren for this edge case.


Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Dec 8, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 103 kB (+87 B) 39 kB (+32 B) 35.1 kB (+11 B)
vue.global.prod.js 161 kB (+87 B) 59 kB (+35 B) 52.5 kB (-42 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 47 kB (+87 B) 18.4 kB (+28 B) 16.8 kB (+25 B)
createApp 55.2 kB (+83 B) 21.4 kB (+27 B) 19.6 kB (+6 B)
createSSRApp 59.5 kB (+87 B) 23.2 kB (+29 B) 21.1 kB (+27 B)
defineCustomElement 60.8 kB (+87 B) 23.2 kB (+28 B) 21.1 kB (+27 B)
overall 69.5 kB (+87 B) 26.6 kB (+27 B) 24.3 kB (-23 B)

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 8, 2025

Open in StackBlitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@14182
npm i https://pkg.pr.new/@vue/compiler-core@14182
yarn add https://pkg.pr.new/@vue/compiler-core@14182.tgz

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@14182
npm i https://pkg.pr.new/@vue/compiler-dom@14182
yarn add https://pkg.pr.new/@vue/compiler-dom@14182.tgz

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@14182
npm i https://pkg.pr.new/@vue/compiler-sfc@14182
yarn add https://pkg.pr.new/@vue/compiler-sfc@14182.tgz

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@14182
npm i https://pkg.pr.new/@vue/compiler-ssr@14182
yarn add https://pkg.pr.new/@vue/compiler-ssr@14182.tgz

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@14182
npm i https://pkg.pr.new/@vue/reactivity@14182
yarn add https://pkg.pr.new/@vue/reactivity@14182.tgz

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@14182
npm i https://pkg.pr.new/@vue/runtime-core@14182
yarn add https://pkg.pr.new/@vue/runtime-core@14182.tgz

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@14182
npm i https://pkg.pr.new/@vue/runtime-dom@14182
yarn add https://pkg.pr.new/@vue/runtime-dom@14182.tgz

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@14182
npm i https://pkg.pr.new/@vue/server-renderer@14182
yarn add https://pkg.pr.new/@vue/server-renderer@14182.tgz

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@14182
npm i https://pkg.pr.new/@vue/shared@14182
yarn add https://pkg.pr.new/@vue/shared@14182.tgz

vue

pnpm add https://pkg.pr.new/vue@14182
npm i https://pkg.pr.new/vue@14182
yarn add https://pkg.pr.new/vue@14182.tgz

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@14182
npm i https://pkg.pr.new/@vue/compat@14182
yarn add https://pkg.pr.new/@vue/compat@14182.tgz

commit: 82a13bb

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/runtime-core/src/renderer.ts (1)

2581-2602: Consider adding defensive safeguards for edge cases.

The recursive traversal logic correctly handles the wrapper component scenario described in issue #14173. However, consider adding safeguards:

  1. Null check for subTree: Line 2590 accesses asyncWrapper.subTree without verification. If a component exists but subTree is undefined (e.g., during initialization or edge cases), this could cause issues.

  2. Cycle detection or depth limit: While component trees should be acyclic, a depth limit or cycle detection would prevent potential infinite recursion if the component structure is unexpected.

Apply this diff to add defensive checks:

 function resolveAsyncComponentPlaceholder(anchorVnode: VNode) {
   // anchor vnode is a unresolved async component
   if (anchorVnode.placeholder) {
     return anchorVnode.placeholder
   }
 
   // anchor vnode maybe is a wrapper component has single unresolved async component
   const asyncWrapper = anchorVnode.component
   if (asyncWrapper) {
     const subTree = asyncWrapper.subTree
+    
+    // guard against missing subTree
+    if (!subTree) {
+      return null
+    }
 
     // wrapper that directly contains an unresolved async component
     if (subTree.placeholder) {
       return subTree.placeholder
     }
 
     // try to locate deeper nested async component placeholder
     return resolveAsyncComponentPlaceholder(subTree)
   }
 
   return null
 }

Alternatively, consider adding a depth parameter to prevent excessive recursion:

function resolveAsyncComponentPlaceholder(anchorVnode: VNode, depth: number = 0): HostNode | null {
  const MAX_DEPTH = 10 // reasonable limit for nested wrappers
  
  if (depth > MAX_DEPTH) {
    if (__DEV__) {
      warn('Maximum depth exceeded while resolving async component placeholder')
    }
    return null
  }
  
  if (anchorVnode.placeholder) {
    return anchorVnode.placeholder
  }
  
  const asyncWrapper = anchorVnode.component
  if (asyncWrapper?.subTree) {
    if (asyncWrapper.subTree.placeholder) {
      return asyncWrapper.subTree.placeholder
    }
    return resolveAsyncComponentPlaceholder(asyncWrapper.subTree, depth + 1)
  }
  
  return null
}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f51d3e2 and d62aff8.

📒 Files selected for processing (1)
  • packages/runtime-core/src/renderer.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/runtime-core/src/renderer.ts (1)
packages/runtime-core/src/vnode.ts (1)
  • VNode (160-257)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test / e2e-test
🔇 Additional comments (1)
packages/runtime-core/src/renderer.ts (1)

1999-1999: LGTM! Correctly handles nested async component placeholders.

The fallback chain (anchorVNode.el || resolveAsyncComponentPlaceholder(anchorVNode)) appropriately addresses the edge case where wrapper components contain unresolved async components. This ensures the correct anchor is found even when there are intermediate wrapper elements.

@edison1105 edison1105 added ready to merge The PR is ready to be merged. 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Dec 9, 2025
@edison1105
Copy link
Member

/ecosystem-ci run

@vue-bot
Copy link
Contributor

vue-bot commented Dec 9, 2025

📝 Ran ecosystem CI: Open

suite result latest scheduled
nuxt success success
language-tools failure success
pinia success success
router failure failure
vant success success
primevue success success
test-utils success success
vite-plugin-vue success success
quasar success success
radix-vue success success
vitepress success success
vue-i18n failure failure
vueuse success success
vue-macros failure failure
vue-simple-compiler success success
vuetify failure failure

})

// #14173
test('nested async components with v-for + only Suspense and async component wrappers', async () => {
Copy link
Contributor

@Tofandel Tofandel Dec 10, 2025

Choose a reason for hiding this comment

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

If you want an even smaller reproduction:

const items = ref([4, 5, 6]);
const App = {
  setup() {
    const loader = async () => defineComponent({
      props: {item: Number},
      setup(props) {
        return () => h('div', props.item)
      }
    });

    const LazyTest = defineAsyncComponent({
      loader: async () => defineComponent({
        setup(_, ctx) {
          const comp = defineAsyncComponent({
            loader: loader,
          })
          return () => h(comp, ctx.attrs)
        }
      }),
    })

    return () =>
      h(Suspense, null, {
        default: () =>
          h(
            Fragment,
            null,
            items.value.map(item =>
              h(LazyTest, {id: item, key: item}),
            ),
          ),
      })
  },
}

const root = nodeOps.createElement('div')
render(h(App), root)
await nextTick()
await Promise.all(deps)
await Promise.all(deps)

expect(serializeInner(root)).toBe(
  `<div>4</div><div>5</div><div>6</div>`,
)

items.value = [1,2,3]
await nextTick()
await Promise.all(deps)
await Promise.all(deps)
expect(serializeInner(root)).toBe(
  `<div>1</div><div>2</div><div>3</div>`,
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your review. This unit test case was designed to reproduce nested async component wrappers and nested suspense wrappers, so it was a bit complex. I've now simplified the HTML to make it look less complicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

<Suspense> changes the order of elements when using v-for with a given key and a wrapper component

4 participants