Skip to content

fix(hydration): skip lazy hydration for patched components #13283

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

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented May 6, 2025

close #13255

I think if the component is patched before lazy hydration, there's no need to perform hydration anymore, because the el might have already changed and no longer matches the server output. In this scenario, lazy hydration should be skipped.

Summary by CodeRabbit

  • Bug Fixes
    • Improved hydration lifecycle management for async components to prevent redundant hydration after a component has been updated.
    • Added warning messages in development mode if hydration is skipped due to the component being patched.
  • New Features
    • Enabled reactive prop passing to async components to support hydration behavior based on dynamic values.
  • Tests
    • Added end-to-end test verifying hydration behavior when a component is patched before hydration occurs, ensuring proper warnings and UI updates.

Copy link

coderabbitai bot commented May 6, 2025

Walkthrough

This change updates the async component hydration logic by introducing a patched flag to prevent hydration after the component has already been patched. If hydration is attempted post-patch, a warning is issued in development mode and hydration is skipped. The component's name is included in the warning message for clarity.

Changes

Files Change Summary
packages/runtime-core/src/apiAsyncComponent.ts Added a patched flag to track if the component has been patched before hydration. Introduced logic to skip hydration and warn if already patched, using getComponentName for detailed warnings. No changes to exported/public APIs.
packages/vue/tests/e2e/hydration-strat-media.html, packages/vue/tests/e2e/hydrationStrategies.spec.ts Updated Comp component to accept a Boolean value prop and use it reactively. Added a new e2e test case "media query (patched before hydration)" to verify hydration behavior when component is patched before hydration, including checking for warnings and correct UI updates.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant AsyncComponentInstance
    participant HydrationStrategy

    Caller->>AsyncComponentInstance: Request hydration
    AsyncComponentInstance->>HydrationStrategy: __asyncHydrate()
    HydrationStrategy->>AsyncComponentInstance: performHydrate()
    alt Component already patched
        AsyncComponentInstance->>Caller: Warn and skip hydration
    else Not patched
        AsyncComponentInstance->>AsyncComponentInstance: Hydrate component
        AsyncComponentInstance->>AsyncComponentInstance: Set patched flag on update
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Prevent hydration failure and unresponsive props when component is patched before hydration (#13255)
Issue warning if hydration is skipped due to prior patching (#13255)
Include component name in warning for easier debugging (#13255)

Poem

A patch, a flag, a warning bright,
No more will hydration fail at night!
Async components now behave,
Even if props misbehave.
With names in logs and bugs set free,
A happy hop for devs and me!
🐇✨


📜 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 bc31190 and 24179af.

📒 Files selected for processing (2)
  • packages/vue/__tests__/e2e/hydration-strat-media.html (2 hunks)
  • packages/vue/__tests__/e2e/hydrationStrategies.spec.ts (1 hunks)
🔇 Additional comments (4)
packages/vue/__tests__/e2e/hydrationStrategies.spec.ts (1)

89-117: Well-designed test case that thoroughly verifies the PR behavior

This test case provides excellent coverage for the fix. It thoroughly validates the core functionality by:

  1. Setting up error and warning monitoring
  2. Patching the component before hydration occurs
  3. Verifying the UI updates correctly after patching
  4. Ensuring hydration still completes successfully
  5. Checking that the expected warning is logged
  6. Confirming no errors are thrown

The references to issue #13255 and explicit steps in the test (patch, resize, assertions) make the test's purpose clear and maintainable.

packages/vue/__tests__/e2e/hydration-strat-media.html (3)

18-20: Good addition of explicit props definition

Adding proper prop type definition improves component contract clarity and TypeScript type-checking. This change is necessary to support the test case for patched components.


21-21: Proper prop access in setup and render function

The component now correctly accesses the prop in the setup function and references it in the render function, enabling reactivity tracking which is essential for testing prop updates before hydration.

Also applies to: 28-28


45-46: Well-structured reactive state management

Creating and exposing the reactive show ref through the window object provides a clean interface for the test to trigger component updates. This change enables the test case to effectively verify the component's behavior when patched before hydration occurs.

✨ 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.

Copy link

github-actions bot commented May 6, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+37 B) 38.2 kB (+16 B) 34.4 kB (-16 B)
vue.global.prod.js 159 kB (+37 B) 58.4 kB (+14 B) 52 kB (+55 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB 18.2 kB 16.6 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.7 kB 22.9 kB 20.9 kB
defineCustomElement 59.3 kB 22.8 kB 20.8 kB
overall 68.6 kB 26.4 kB 24 kB

Copy link

pkg-pr-new bot commented May 6, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-ssr

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

@vue/compiler-sfc

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 24179af

@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. scope:hydration labels May 6, 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. scope:hydration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update of component prop before hydration causes hydration to fail
1 participant