Skip to content

fix(runtime-core): ensure correct anchor el for unresolved async components #13560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

linzhe141
Copy link
Contributor

@linzhe141 linzhe141 commented Jul 3, 2025

close #13559

When the asynchronous component is not resolved, Vue will use a placeholder to occupy the position of the rendering result. At this time, we need to get the actual placeholder el to ensure that the anchor point of the subsequent insertion operation is correct.

playground with this pr

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of placeholder elements for asynchronous components within Suspense boundaries to enhance rendering accuracy and update behavior.
  • Tests

    • Added tests verifying correct rendering and updates of multiple async components inside Suspense when rendered via a list.

Copy link

coderabbitai bot commented Jul 3, 2025

"""

Walkthrough

The patch introduces tracking of placeholder DOM elements for unresolved async components inside Suspense by adding a placeholder property to VNodes and assigning it during mounting. It updates the anchor determination logic to consider placeholder elements. A test was added to verify correct rendering and updating of multiple async components rendered with v-for and keys inside Suspense.

Changes

File(s) Change Summary
packages/runtime-core/src/vnode.ts Added internal placeholder property to VNode interface and updated cloneVNode to copy it
packages/runtime-core/src/renderer.ts Assigned placeholder element to initialVNode.placeholder in mountComponent; updated anchor logic in patchKeyedChildren to consider placeholder
packages/runtime-core/tests/components/Suspense.spec.ts Added test for rendering multiple async components inside Suspense with v-for and verifying updates on reactive data changes

Sequence Diagram(s)

sequenceDiagram
    participant Renderer
    participant SuspenseBoundary
    participant AsyncComponent
    participant PlaceholderNode
    participant DOM

    Renderer->>SuspenseBoundary: Mount component with async dependency
    SuspenseBoundary->>PlaceholderNode: Create and insert placeholder comment node
    SuspenseBoundary->>Renderer: Assign placeholder to vnode
    AsyncComponent-->>SuspenseBoundary: Async component resolves
    SuspenseBoundary->>DOM: Replace placeholder with resolved component DOM
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure elements rendered with v-for and key retain correct order when using async components and <Suspense> (#13559)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes detected.

Suggested labels

ready to merge, :hammer: p3-minor-bug

Poem

🐇 In shadows deep where async hides,
A placeholder now firmly abides.
Suspense no longer loses track,
Of bunnies hopping, front to back.
With v-for keys all in their place,
The warren’s order wins the race!
🌿✨
"""


📜 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 21e1407 and e23b690.

📒 Files selected for processing (2)
  • packages/runtime-core/src/renderer.ts (2 hunks)
  • packages/runtime-core/src/vnode.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/runtime-core/src/vnode.ts
  • packages/runtime-core/src/renderer.ts
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
  • GitHub Check: test / e2e-test
  • GitHub Check: test / unit-test-windows
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Jul 3, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+84 B) 38.4 kB (+37 B) 34.6 kB (+7 B)
vue.global.prod.js 159 kB (+84 B) 58.5 kB (+36 B) 52.1 kB (-31 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB (+84 B) 18.2 kB (+37 B) 16.7 kB (+20 B)
createApp 54.6 kB (+84 B) 21.2 kB (+35 B) 19.4 kB (+33 B)
createSSRApp 58.8 kB (+86 B) 22.9 kB (+33 B) 20.9 kB (+33 B)
defineCustomElement 59.5 kB (+84 B) 22.8 kB (+40 B) 20.8 kB (+29 B)
overall 68.6 kB (+84 B) 26.4 kB (+35 B) 24.1 kB (+29 B)

Copy link

pkg-pr-new bot commented Jul 3, 2025

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@13560

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@13560

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@13560

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@13560

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@13560

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@13560

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@13560

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@13560

@vue/shared

npm i https://pkg.pr.new/@vue/shared@13560

vue

npm i https://pkg.pr.new/vue@13560

@vue/compat

npm i https://pkg.pr.new/@vue/compat@13560

commit: e23b690

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)

1982-1990: Good fix for async component anchor determination, but consider additional safety checks.

The logic correctly handles the case where an async component hasn't resolved yet by using component.subTree.el instead of vnode.el. This is important because for unresolved async components, the vnode's el property may not yet point to the correct DOM element.

However, consider adding a null safety check for the subTree property:

        let anchor = parentAnchor
        if (nextIndex + 1 < l2) {
          const anchorVnode = c2[nextIndex + 1] as VNode
-         if (anchorVnode.component && !anchorVnode.component.asyncResolved) {
+         if (anchorVnode.component && !anchorVnode.component.asyncResolved && anchorVnode.component.subTree) {
            anchor = anchorVnode.component.subTree.el
          } else {
            anchor = anchorVnode.el
          }
        }

This ensures that subTree exists before accessing its el property, which could be important during the component's lifecycle.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c85f1b5 and f9ff6eb.

📒 Files selected for processing (1)
  • packages/runtime-core/src/renderer.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/runtime-core/src/renderer.ts (1)
packages/runtime-core/src/vnode.ts (1)
  • VNode (160-256)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
  • GitHub Check: test / e2e-test

@linzhe141 linzhe141 changed the title wip: . fix(runtime-core): ensure correct anchor el for unresolved async components Jul 3, 2025
@linzhe141 linzhe141 marked this pull request as draft July 3, 2025 16:09
@linzhe141 linzhe141 marked this pull request as ready for review July 4, 2025 01:33
@@ -1226,6 +1226,7 @@ function baseCreateRenderer(
if (!initialVNode.el) {
const placeholder = (instance.subTree = createVNode(Comment))
processCommentNode(null, placeholder, container!, anchor)
initialVNode.el = placeholder.el
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect, a component cannot have el before mounting. If it has an el, it would trigger hydration logic instead of mounting.

@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 Jul 5, 2025
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
2 participants