-
Notifications
You must be signed in to change notification settings - Fork 324
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
Open
codermmy
wants to merge
4
commits into
opentiny:ospp-2025/tiny-vue-nuxt
Choose a base branch
from
codermmy:ospp-2025/tinyvue-nuxt-fix
base: ospp-2025/tiny-vue-nuxt
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "@opentiny/vue-vite-import", | ||
"version": "0.1.0", | ||
"description": "Vite plugin for auto import components", | ||
"author": "OpenTiny", | ||
"license": "MIT", | ||
"keywords": [ | ||
"vite", | ||
"vite-plugin", | ||
"import" | ||
], | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
"dev:saas": "pnpm create:icon-saas && pnpm build:entry && pnpm -C examples/vue3 dev:saas", | ||
"dev2": "pnpm build:entry && pnpm -C examples/vue2 dev", | ||
"dev2:saas": "pnpm create:icon-saas && pnpm build:entry && pnpm -C examples/vue2 dev:saas", | ||
"dev:nuxt": "pnpm build:entry && pnpm -C examples/nuxt dev", | ||
"// ---------- 启动官网文档 ----------": "", | ||
"site": "pnpm build:entry && pnpm -C examples/sites start", | ||
"site:open": "pnpm build:entry && pnpm -C examples/sites start:open", | ||
|
@@ -89,7 +90,11 @@ | |
"// ---------- e2e自动化测试 ----------": "", | ||
"test:e2e": "pnpm test:e2e3", | ||
"test:e2e2": "pnpm -C examples/vue2 test:e2e --project=chromium", | ||
"test:e2e3": "pnpm -C examples/vue3 test:e2e --project=chromium", | ||
"test:e2e3": "CI=true pnpm -C examples/vue3 test:e2e --project=chromium", | ||
"test:component": "pnpm -C examples/sites start & sleep 10 && CI=true pnpm -C examples/vue3 test:e2e --project=chromium -g", | ||
"test:button": "pnpm -C examples/sites start & sleep 10 && pnpm -C examples/vue3 test:e2e --project=chromium -g \"app/button\" && pnpm -C examples/vue3 exec playwright show-report", | ||
"test:alert": "pnpm -C examples/sites start & sleep 10 && CI=true pnpm -C examples/vue3 test:e2e --project=chromium -g \"app/alert\"", | ||
"show-report": "pnpm -C examples/vue3 exec playwright show-report", | ||
"// ---------- playwright下载chromium、firefox等浏览器内核 ----------": "", | ||
"install:browser": "pnpm -C examples/vue3 install:browser", | ||
"// ---------- e2e测试代码生成器 ----------": "", | ||
|
@@ -147,7 +152,7 @@ | |
"shx": "^0.3.4", | ||
"typescript": "catalog:", | ||
"vite": "catalog:", | ||
"vue": "^3.4.38", | ||
"vue": "^3.5.13", | ||
"vue-tsc": "^1.6.5" | ||
}, | ||
"pnpm": { | ||
|
@@ -156,10 +161,10 @@ | |
"[email protected]": "patches/[email protected]" | ||
}, | ||
"overrides": { | ||
"@vue/compiler-sfc@3": "3.4.38", | ||
"@vue/runtime-core@3": "3.4.38", | ||
"@vue/runtime-dom@3": "3.4.38", | ||
"@vue/shared@3": "3.4.38", | ||
"@vue/compiler-sfc@3": "3.5.13", | ||
"@vue/runtime-core@3": "3.5.13", | ||
"@vue/runtime-dom@3": "3.5.13", | ||
"@vue/shared@3": "3.5.13", | ||
"cropperjs": "1.6.2", | ||
"echarts": "5.4.1", | ||
"follow-redirects": "1.14.8", | ||
|
@@ -175,10 +180,10 @@ | |
"[email protected]": "2.7.10", | ||
"[email protected]": "2.6.14", | ||
"[email protected]": "2.7.10", | ||
"vue@3": "3.4.38", | ||
"vue@3": "3.5.13", | ||
"vue2": "npm:[email protected]", | ||
"vue2.7": "npm:[email protected]", | ||
"vue3": "npm:vue@3.4.38" | ||
"vue3": "npm:vue@3.5.13" | ||
}, | ||
"packageExtensions": { | ||
"[email protected]": { | ||
|
@@ -198,7 +203,7 @@ | |
}, | ||
"vite-plugin-dts": { | ||
"peerDependencies": { | ||
"vue": "^3.4.38" | ||
"vue": "^3.5.13" | ||
} | ||
}, | ||
"vite-plugin-md": { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { hooks as Vue } from '@opentiny/vue-common' | ||
import { computedEager } from '@vueuse/core' | ||
import { isServer } from '@opentiny/utils' | ||
|
||
const { inject, getCurrentInstance, unref } = Vue | ||
export interface TyIdInjectionContext { | ||
prefix: string | number | ||
current: number | ||
} | ||
interface useIdParams { | ||
nameSpace?: string | ||
length?: number | ||
deterministicId?: Vue.Ref<string> | string | ||
} | ||
/** | ||
* 用于Vue provide/inject的注入键,共享ID生成器状态。 | ||
*/ | ||
export const ID_INJECTION_KEY: Vue.InjectionKey<TyIdInjectionContext> = Symbol('tiny-vue-id-injection') | ||
|
||
/** | ||
* 默认的ID注入上下文,当provide未提供时使用。 | ||
*/ | ||
const defaultIdInjection: TyIdInjectionContext = { | ||
prefix: Math.floor(Math.random() * 1000000), | ||
current: 0 | ||
} | ||
|
||
/** | ||
* 获取ID注入上下文的组合式函数,会尝试从组件树中注入,若失败则返回默认上下文。 | ||
*/ | ||
export const useIdInjection = (): TyIdInjectionContext => { | ||
return getCurrentInstance() ? inject(ID_INJECTION_KEY, defaultIdInjection) : defaultIdInjection | ||
} | ||
|
||
/** | ||
* 生成唯一的ID,支持SSR,避免水合不匹配。 | ||
* | ||
* @param {useIdParams} options - 配置选项。 | ||
* @param {string} [options.nameSpace] - ID命名空间。 | ||
* @param {number} [options.length] - ID中数字部分的长度。 | ||
* @param {Ref<string> | string} [options.deterministicId] - 一个确定的ID,如果提供,则直接返回该ID。 | ||
* @returns {string} 生成的唯一ID。 | ||
*/ | ||
let useIdFromVue: (() => string) | null = null | ||
export const useId = ({ nameSpace = '', length = 8, deterministicId }: useIdParams): string => { | ||
// 判断vue自身是否有useId(vue3.5+) | ||
const hasVueUseId = parseFloat(Vue.version) >= 3.5 | ||
// 如果vue自身有useId,则优先使用vue自身的useId | ||
if (!useIdFromVue && hasVueUseId) { | ||
try { | ||
// eslint-disable-next-line dot-notation | ||
useIdFromVue = (Vue as any)?.['useId'] || null | ||
} catch (e) { | ||
useIdFromVue = null | ||
} | ||
} | ||
const idFromVue = useIdFromVue ? useIdFromVue() : null | ||
const idInjection = useIdInjection() | ||
// 在SSR期间,如果使用的是默认ID注入上下文,会发出警告。 | ||
// 随机前缀会导致客户端与服务端ID不一致,从而引发水合错误。 | ||
if (isServer && idInjection === defaultIdInjection) { | ||
console.warn( | ||
'IdInjection', | ||
`Looks like you are using server rendering, you must provide a id provider to ensure the hydration process to be succeed | ||
usage: app.provide(ID_INJECTION_KEY, { | ||
prefix: number, | ||
current: number, | ||
})` | ||
) | ||
} | ||
|
||
const current = idInjection.current++ | ||
const currentStr = String(current) | ||
// 如果数字长度小于指定长度,前面补0 | ||
const paddedCurrent = | ||
idFromVue || (currentStr.length >= length ? currentStr : '0'.repeat(length - currentStr.length) + currentStr) | ||
const idRef = computedEager(() => unref(deterministicId) || `${nameSpace}id-${idInjection.prefix}-${paddedCurrent}`) | ||
return idRef.value | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
代码的committ记录有问题,为啥会有ssr之外的代码呢
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.
嗯嗯,诶,没想到这个pr没关掉,因为最近要换电脑,所以把当时本地一些跑nuxt项目时组件出现的小bug也commit的,本来是想后续撤回的,没想到一起在这里了,我到时候重新提一个pr