Skip to content

fix(ssr): render hidden correctly #13125

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
Open

Conversation

chirokas
Copy link
Contributor

@chirokas chirokas commented Mar 31, 2025

Add support for hidden="until-found"

This PR fixes the issue where Vue treats the hidden attribute as a boolean, causing "until-found" to be incorrectly rendered.

export const isBooleanAttr: (key: string) => boolean = /*@__PURE__*/ makeMap(
specialBooleanAttrs +
`,async,autofocus,autoplay,controls,default,defer,disabled,hidden,` +

Example

Summary by CodeRabbit

  • New Features
    • Enhanced support for the hidden attribute to handle both boolean and string values consistently across server-side rendering, hydration, and static compilation.
  • Bug Fixes
    • Resolved hydration warnings related to various valid hidden attribute values.
  • Tests
    • Added extensive test coverage for mixed boolean and string usage of the hidden attribute in SSR and hydration.
  • Documentation
    • Clarified attribute classification and behavior for overloaded boolean attributes.

Copy link

github-actions bot commented Mar 31, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.3 kB 34.5 kB
vue.global.prod.js 159 kB 58.5 kB 52.1 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.6 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.8 kB 23 kB 20.9 kB
defineCustomElement 59.5 kB 22.8 kB 20.8 kB
overall 68.6 kB 26.4 kB 24.1 kB

Copy link

pkg-pr-new bot commented Mar 31, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 77903a0

@edison1105
Copy link
Member

/ecosystem-ci run

@edison1105 edison1105 added has workaround A workaround has been found to avoid the problem 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Apr 1, 2025
@vue-bot
Copy link
Contributor

vue-bot commented Apr 1, 2025

📝 Ran ecosystem CI: Open

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

@edison1105 edison1105 added the ready to merge The PR is ready to be merged. label Apr 1, 2025
@edison1105
Copy link
Member

LGTM

@skirtles-code
Copy link
Contributor

skirtles-code commented Jun 2, 2025

isBooleanAttr is also used in stringifyStatic.ts, to handle this edge case:

That example renders correctly with 4 <span :hidden="false"> elements, but once it gets to 5 it triggers a static VNode and hits the edge case.

This works correctly on main.

Copy link

coderabbitai bot commented Jun 4, 2025

Walkthrough

The changes introduce explicit support for "overloaded boolean attributes," focusing on the hidden attribute. The updates refine attribute handling during SSR, hydration, and type definitions, ensuring correct rendering and mismatch detection for boolean, string, and empty string values. Corresponding tests and type adjustments are added.

Changes

Files/Paths Change Summary
packages/shared/src/domAttrConfig.ts Moves hidden from boolean to overloaded boolean attributes; adds isOverloadedBooleanAttr and isBooleanAttrValue utilities.
packages/runtime-dom/src/jsx.ts Narrows hidden type in HTMLAttributes from Booleanish to `boolean
packages/runtime-core/src/hydration.ts Updates hydration mismatch logic to handle overloaded boolean attributes using new utilities.
packages/server-renderer/src/helpers/ssrRenderAttrs.ts Extends SSR attribute rendering to support overloaded boolean attributes with new utility functions.
packages/compiler-dom/src/transforms/stringifyStatic.ts Extends static stringification to skip overloaded boolean attributes bound to false.
packages/runtime-core/__tests__/hydration.spec.ts Adds hydration tests for combined boolean/string hidden attribute values.
packages/server-renderer/__tests__/ssrRenderAttrs.spec.ts Adds SSR rendering tests for combined boolean/string hidden attribute scenarios.
packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts Adds test verifying removal of overloaded boolean attributes bound to false during static stringification.

Sequence Diagram(s)

sequenceDiagram
    participant SSR as SSR Renderer
    participant DOM as DOM Element
    participant Hydration as Hydration Logic

    SSR->>DOM: Render element with hidden attribute (true/false/string/empty)
    DOM->>Hydration: Element with rendered hidden attribute
    Hydration->>DOM: Check attribute (isBooleanAttr/isOverloadedBooleanAttr)
    Hydration->>DOM: Compare actual vs expected using isBooleanAttrValue/includeBooleanAttr
    Note right of Hydration: No mismatch warning if values align
Loading

Poem

In the warren, attributes gleam,
"hidden" now dances in a boolean-string dream.
Overloaded, it hops—sometimes true, sometimes found,
With empty or string, it leaps all around!
SSR and hydration, in perfect sync,
This bunny’s code is sharper than you think!
🐇✨


📜 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 2ed535b and 77903a0.

📒 Files selected for processing (2)
  • packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts (1 hunks)
  • packages/compiler-dom/src/transforms/stringifyStatic.ts (2 hunks)
🔇 Additional comments (3)
packages/compiler-dom/src/transforms/stringifyStatic.ts (2)

29-29: LGTM: Import addition is correct.

The import of isOverloadedBooleanAttr is necessary for the new functionality and properly placed with other related imports.


344-350: LGTM: Logic extension correctly handles overloaded boolean attributes.

The extended conditional logic properly includes overloaded boolean attributes (like hidden) alongside regular boolean attributes when determining whether to skip attributes bound to 'false' during static stringification. This ensures consistent behavior between regular and overloaded boolean attributes and aligns with the PR objective of fixing hidden attribute rendering in SSR.

packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts (1)

392-407: LGTM: Comprehensive test coverage for overloaded boolean attributes.

The test case correctly verifies that overloaded boolean attributes bound to false are removed during static stringification. The test:

  • Follows the established pattern of existing boolean attribute tests
  • Uses the appropriate threshold to trigger optimization
  • Correctly expects the hidden attribute to be omitted when bound to false
  • Provides adequate coverage for the new functionality

This ensures the changes in stringifyStatic.ts work as intended.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.

Comment on lines +87 to +89
if (isOverloadedBooleanAttr(key) && isBooleanAttrValue(value)) {
return includeBooleanAttr(value) ? ` ${key}` : ``
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I've been giving this some more thought and I'm concerned about this part.

The functions ssrRenderAttrs and ssrRenderDynamicAttr handle the general case. They're able to render any attribute as a string.

But the compiler then has special cases to improve performance when sufficient information is available at compile time. Boolean attributes are rendered using ssrIncludeBooleanAttr (an aliased version of includeBooleanAttr), while other 'safe' attributes are rendered using ssrRenderAttr.

The change proposed here is adding extra logic to ssrRenderAttr, so that it handles more cases. I think that defeats the purpose of having a separate function. We already have functions to handle the general case, this function is intended to handle just one, very specific case.

In my opinion, this case should be handled separately in the compiler, rather than altering ssrRenderAttr.

For example, it might be possible to use SSR_RENDER_DYNAMIC_ATTR in ssrTransformElement.ts to handle the overloaded boolean case. It would also be possible to do this with a new helper, but I'm not sure it's worth it just for the hidden attribute.

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. has workaround A workaround has been found to avoid the problem 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.

4 participants