8000 fix(runtime-core): properly handle async component update before resolve by linzhe141 · Pull Request #11619 · vuejs/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(runtime-core): properly handle async component update before resolve #11619

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 7 commits into
base: main
Choose a base branch
from

Conversation

linzhe141
Copy link
Contributor
@linzhe141 linzhe141 commented Aug 15, 2024

close #11617

this pr playground

Summary by CodeRabbit

  • Bug Fixes

    • Improved the handling of async component updates to ensure updates are properly queued and applied after rendering, enhancing reliability in complex async scenarios.
  • Tests

    • Added a new test case to verify correct behavior when async components receive multiple prop updates before and after their async setup resolves.

Copy link
github-actions bot commented Aug 15, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+4 B) 38.2 kB (+3 B) 34.4 kB (+7 B)
vue.global.prod.js 159 kB (+4 B) 58.4 kB 52 kB (+59 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB (+6 B) 18.2 kB (+1 B) 16.6 kB (-3 B)
createApp 54.5 kB (+6 B) 21.2 kB (-1 B) 19.4 kB (+8 B)
createSSRApp 58.7 kB (+4 B) 22.9 kB 20.9 kB (+3 B)
defineCustomElement 59.3 kB (+4 B) 22.8 kB (-2 B) 20.8 kB (-5 B)
overall 68.6 kB (+4 B) 26.4 kB (+2 B) 24 kB (-6 B)

@linzhe141 linzhe141 marked this pull request as draft August 15, 2024 05:19
@linzhe141 linzhe141 marked this pull request as ready for review August 15, 2024 05:22
@@ -1438,7 +1438,7 @@ function baseCreateRenderer(
nonHydratedAsyncRoot.asyncDep!.then(() => {
// the instance may be destroyed during the time period
if (!instance.isUnmounted) {
componentUpdateFn()
instance.update()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
instance.update()
update()

@edison1105
Copy link
Member

Thank you for addressing the issue.
Could you please add a test case?

@edison1105 edison1105 added need test The PR has missing test cases. 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Aug 15, 2024
@@ -1438,7 +1438,7 @@ function baseCreateRenderer(
nonHydratedAsyncRoot.asyncDep!.then(() => {
// the instance may be destroyed during the time period
if (!instance.isUnmounted) {
componentUpdateFn()
update()
Copy link
Member
@edison1105 edison1105 Aug 15, 2024

Choose a reason for hiding this comment

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

There is another problem here. The update() is executed 2 times.
This behavior is not related to this PR. But it needs to be fixed.

The flow is as follows:

  1. When locateNonHydratedAsyncRoot is called, subComponent.asyncResolved is false, nonHydratedAsyncRoot has a value, and a then callback is registered.
  2. The then callback is triggered and calls update.
  3. We go back to step 1, but subComponent.asyncResolved is still false because the then at this line is registered later and executes after the previously registered then. The then callback is registered again.
  4. Same as step 2.
  5. Now subComponent.asyncResolved is true, and nonHydratedAsyncRoot is undefined.
  6. The remaining update logic runs.

In this process, the then callback gets registered twice, and update is called twice.
The following code can be used to prevent this behavior.

nonHydratedAsyncRoot.asyncDep!.then(() => {
  // the instance may be destroyed during the time period
  queuePostRenderEffect(()=>{
    if (!instance.isUnmounted) update()
  },parentSuspense)
})

@edison1105
Copy link
Member
edison1105 commented Aug 15, 2024

I'm pretty sure the previous fix is proper.
Is there any reason why you changed it?

@linzhe141
Copy link
Contributor Author

@edison1105 I tried to fix it again, maybe it should be handled like this

I'm pretty sure the previous fix is proper.我很确定之前的修复是正确的。 Is there any reason why you changed it?你有什么理由改变它吗?

You are right! I was wrong. I thought that when patching suspense, I had to confirm which component was pending, but this has nothing to do with pendingBranch.

@edison1105 edison1105 changed the title fix(runtime-core): fix nonHydratedAsyncRoot update fix(runtime-core): properly handle async component update before resolve Aug 15, 2024
@edison1105 edison1105 added ready for review This PR requires more reviews and removed need test The PR has missing test cases. labels Aug 15, 2024
Copy link
pkg-pr-new bot commented Jan 3, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-ssr

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

@vue/compiler-sfc

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 9215144

Copy link
coderabbitai bot commented May 10, 2025

Walkthrough

The changes introduce a new test case to verify async component prop updates within Suspense and modify the renderer's update logic. The renderer now schedules async component updates as post-render effects instead of invoking them synchronously, ensuring correct update sequencing when async dependencies resolve.

Changes

File(s) Change Summary
packages/runtime-core/tests/components/Suspense.spec.ts Added a test case to validate async component prop updates inside Suspense, including lifecycle checks.
packages/runtime-core/src/renderer.ts Modified async component update logic to queue updates as post-render effects instead of direct calls.

Sequence Diagram(s)

sequenceDiagram
    participant ParentComponent
    participant SuspenseBoundary
    participant AsyncChildComponent
    participant Renderer

    ParentComponent->>SuspenseBoundary: Renders AsyncChildComponent
    SuspenseBoundary->>AsyncChildComponent: Mounts with async setup
    AsyncChildComponent-->>SuspenseBoundary: Suspends until async resolves
    ParentComponent->>AsyncChildComponent: Updates prop (loading)
    AsyncChildComponent->>Renderer: Requests update (before resolve)
    Renderer->>Renderer: Queues update as post-render effect
    Renderer->>AsyncChildComponent: Applies update after async resolve
    AsyncChildComponent-->>ParentComponent: Emits updated state
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure async component inside Suspense receives prop updates and updates its state after async resolve (#11617)
Ensure updates are not indefinitely stalled and lifecycle hooks reflect prop changes in correct sequence (#11617)

Poem

A rabbit hopped through async haze,
Watching Suspense and updates' maze.
Now props flow swift, no longer late,
Post-render queues decide their fate.
Test cases cheer, bugs disappear—
The garden of updates is crystal clear!
🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 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 3cb4db2 and 9215144.

📒 Files selected for processing (2)
  • packages/runtime-core/__tests__/components/Suspense.spec.ts (2 hunks)
  • packages/runtime-core/src/renderer.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test / unit-test-windows
🔇 Additional comments (2)
packages/runtime-core/src/renderer.ts (1)

1446-1450: Improved async component update handling with queuePostRenderEffect

This change properly addresses the issue by scheduling the async component update as a post-render effect rather than executing it synchronously. Using queuePostRenderEffect ensures that updates happening before component resolution are correctly applied in the Vue rendering pipeline, and the added check prevents updates for unmounted instances.

packages/runtime-core/__tests__/components/Suspense.spec.ts (1)

2166-2238: Good test case for the async component update fix

This test effectively validates the renderer change by testing the scenario where an async component receives props updates before and after resolution. The test ensures:

  1. Props updates before resolution are properly captured
  2. The component correctly receives all updates in the right order
  3. The onUpdated hook is triggered for both state changes

The test design with the delayed loading state change and subsequent verification is comprehensive and directly tests the issue described in #11617.

✨ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 for review This PR requires more reviews scope: suspense
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Async Component inside Suspense doesn't receive prop update
2 participants
0