-
Notifications
You must be signed in to change notification settings - Fork 314
feat: support global unique ID generation with SSR compatibility #3607
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
base: ospp-2025/tiny-vue-nuxt
Are you sure you want to change the base?
feat: support global unique ID generation with SSR compatibility #3607
Conversation
…Vue 3.5+ useId integration - Implement a global unique ID generation mechanism to ensure consistent IDs across client and server rendering, preventing hydration mismatches. - Maintain backward compatibility with existing usage scenarios; no breaking changes introduced. - For Vue 3.5 and above, leverage the official `useId` API to align with framework conventions and reduce maintenance overhead. - Fallback gracefully to custom ID generation logic in earlier Vue versions. - Added warnings for SSR environments when no ID provider is configured to avoid potential hydration issues.
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review (1)
Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing Touches🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
@@ -0,0 +1,80 @@ | |||
import type { InjectionKey, Ref } from 'vue' | |||
import { inject, getCurrentInstance, unref } from 'vue' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TinyVue同时需要兼容vue2和vue3,所以这里所有vue相关的api都需要从vue-common中引入
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TinyVue同时需要兼容vue2和vue3,所以这里所有vue相关的api都需要从vue-common中引入
get
feat: ✨ 支持SSR兼容的全局唯一ID生成及与Vue 3.5+
useId
的集成动机与背景
在服务端渲染(SSR)场景下,我们原有的
guid()
工具函数依赖Math.random()
生成ID。这会导致服务端和客户端生成的ID不一致,从而在Vue/Nuxt等框架中引发“水合不匹配 (hydration mismatch)”错误。这个问题严重影响了组件在同构应用中的稳定性和开发体验。为了彻底解决此问题,并与Vue生态系统的最新进展保持一致,我们引入了一个全新的、SSR安全的唯一ID生成机制。
实现描述
本次更新的核心是新增了
@opentiny/vue-hooks
包下的useId
组合式函数,旨在提供一个稳定、可靠且向后兼容的ID生成方案。技术方案、流程可详见文档:https://www.yuque.com/yuqueyonghu5l1o0n/hvdsrk/bviuxw7zawxov2uv?singleDoc# 《全局唯一 ID 新方案》
全新的
useId
钩子:provide/inject
机制 (ID_INJECTION_KEY
) 在应用级别共享ID生成器状态。在SSR环境下,开发者必须显式提供一个ID上下文,以确保服务端和客户端生成的ID序列完全一致,从而避免水合问题。如果未提供,将在服务端打印警告提示。useId
API,以实现最佳的框架集成和未来维护性。对于旧版本,则平滑降级至我们自己的实现。useId
被设计为guid()
的无缝替代品。我们已经将内部受影响的组件(如Autocomplete
,CollapseItem
,Dropdown
,Popover
,Tooltip
等)从guid()
切换到了useId
,确保了功能的一致性且无破坏性变更。安全性增强:
@opentiny/utils
中的random
函数,在客户端环境下使用更安全的globalThis.crypto.getRandomValues
API来生成随机数。代码重构:
xss
库的导入方式,以解决潜在的模块互操作性问题。使用方法 (针对SSR应用)
在SSR应用(如Nuxt项目)中使用TinyVue时,你需要在应用的入口处配置ID生成器,以确保ID的稳定性:
测试截图或GIF

下图为修改前的全局id在ssr场景下的id不一致报错:
下图为vue3.5之后的效果

下图为vue3.5之前的效果:


控制台也无水合错误