Skip to content

fix(ssr): handle initial selected state for select with v-model + v-for/v-if option #13487

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

Merged
merged 2 commits into from
Jun 18, 2025

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Jun 17, 2025

close #13486

Summary by CodeRabbit

  • Tests

    • Added new test cases to verify server-side rendering of <select multiple> elements with <optgroup> containing dynamic <option> elements using v-for and v-if directives.
  • Bug Fixes

    • Improved support for complex child structures within <optgroup>, ensuring correct handling of loops and conditionals during server-side rendering.

Copy link

coderabbitai bot commented Jun 17, 2025

Walkthrough

The update enhances SSR code generation for <select multiple v-model> elements containing <optgroup> with nested <option> elements, especially when rendered via v-for and v-if. The SSR transform logic now recursively processes element, loop, and conditional nodes under <optgroup>. Corresponding test cases are added to verify correct SSR output.

Changes

File(s) Change Summary
packages/compiler-ssr/src/transforms/ssrVModel.ts Introduced processSelectChildren helper to recursively handle element, FOR, and IF nodes under <select> and <optgroup>; replaced inline recursion with this helper.
packages/compiler-ssr/tests/ssrVModel.spec.ts Added tests for SSR output of <optgroup> with nested <option> elements using v-for and v-if directives.

Sequence Diagram(s)

sequenceDiagram
    participant Template as Template AST
    participant SSRTransform as SSR VModel Transform
    participant SSRCode as SSR Codegen

    Template->>SSRTransform: Encounter <select multiple v-model>
    SSRTransform->>SSRTransform: Find <optgroup> with children
    loop For each child in <optgroup>
        alt Element node
            SSRTransform->>SSRTransform: processOption(child)
        else FOR node
            SSRTransform->>SSRTransform: For each FOR child, processOption(child)
        else IF node
            SSRTransform->>SSRTransform: For each IF branch, processOption(branch child)
        end
    end
    SSRTransform->>SSRCode: Generate SSR code with correct selected attribute logic
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure <option> inside <optgroup> has correct selected attr in SSR output (#13486)
Correct SSR code generation for <optgroup> with dynamic <option> children (#13486)

Poem

In the garden of code, where options do bloom,
Optgroups now flourish, dispelling the gloom.
With loops and conditions, each petal unfurled,
SSR renders true in this digital world.
🐇✨
Hopping through branches, the rabbit delights—
All options selected, the future is bright!


📜 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 2b4aa83 and b1894b9.

📒 Files selected for processing (2)
  • packages/compiler-ssr/__tests__/ssrVModel.spec.ts (1 hunks)
  • packages/compiler-ssr/src/transforms/ssrVModel.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/compiler-ssr/tests/ssrVModel.spec.ts
  • packages/compiler-ssr/src/transforms/ssrVModel.ts
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
  • GitHub Check: continuous-release
  • GitHub Check: test / lint-and-test-dts
  • GitHub Check: test / unit-test-windows
  • GitHub Check: test / e2e-test
  • GitHub Check: continuous-release
✨ 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

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.4 kB 34.6 kB
vue.global.prod.js 159 kB 58.5 kB 52.2 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.7 kB 18.2 kB 16.7 kB
createApp 54.6 kB 21.3 kB 19.4 kB
createSSRApp 58.9 kB 23 kB 21 kB
defineCustomElement 59.6 kB 22.9 kB 20.9 kB
overall 68.7 kB 26.4 kB 24.1 kB

Copy link

pkg-pr-new bot commented Jun 17, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: b1894b9

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/compiler-ssr/src/transforms/ssrVModel.ts (1)

68-78: Duplicate traversal logic – consider extracting a shared helper

The new block repeats almost exactly the traversal logic that already exists in processChildren (lines 174-185). Duplicating this walker increases maintenance cost and the risk of future divergence.

A small refactor can DRY the code and keep the recursion rules in one place:

-    } else if (plainNode.tag === 'optgroup') {
-      plainNode.children.forEach(option => {
-        if (option.type === NodeTypes.ELEMENT) {
-          processOption(option as PlainElementNode)
-        } else if (option.type === NodeTypes.FOR) {
-          option.children.forEach(c => processOption(c as PlainElementNode))
-        } else if (option.type === NodeTypes.IF) {
-          option.branches.forEach(b =>
-            b.children.forEach(c => processOption(c as PlainElementNode)),
-          )
-        }
-      })
+    } else if (plainNode.tag === 'optgroup') {
+      // re-use the generic walker so the traversal rules stay in one place
+      processChildren(plainNode.children)
     }

That change would require hoisting processChildren outside the <select> branch (or defining a module-level helper) but would eliminate the duplicate code and keep the traversal strategy consistent.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 532cfae and 2b4aa83.

📒 Files selected for processing (2)
  • packages/compiler-ssr/__tests__/ssrVModel.spec.ts (1 hunks)
  • packages/compiler-ssr/src/transforms/ssrVModel.ts (1 hunks)
⏰ 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 / unit-test-windows
🔇 Additional comments (1)
packages/compiler-ssr/__tests__/ssrVModel.spec.ts (1)

170-225: 👍 Great addition of edge-case coverage

The two new cases (v-for and v-if inside <optgroup>) hit the exact scenarios the bug fix targets and safeguard against regressions. Snapshot assertions also verify that selected handling and text interpolation remain correct.

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

@skirtles-code skirtles-code left a comment

Choose a reason for hiding this comment

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

Should we also handle nesting? e.g. v-if inside v-for?

Maybe processChildren could be moved to a scope where it can be called from processOption? I think that would handle the recursion and remove the duplication of the v-for/v-if logic.

@edison1105
Copy link
Member Author

@skirtles-code Updated~

@edison1105 edison1105 merged commit 1552095 into main Jun 18, 2025
16 checks passed
@edison1105 edison1105 deleted the edison/fix/ssrOptGroup branch June 18, 2025 12:54
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: ssr
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<option> inside <optgroup> won't have selected attr in the SSR output
2 participants