Skip to content
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

fix(nuxt): only warn for useId if there is no readable attribute #25770

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/nuxt/src/app/composables/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function useId (key?: string): string {

if (!instance) {
// TODO: support auto-incrementing ID for plugins if there is need?
throw new TypeError('[nuxt] `useId` must be called within a component.')
throw new TypeError('[nuxt] `useId` must be called within a component setup function.')
}

nuxtApp._id ||= 0
Expand All @@ -29,9 +29,6 @@ export function useId (key?: string): string {
const instanceIndex = key + ':' + instance._nuxtIdIndex[key]++

if (import.meta.server) {
if (import.meta.dev && instance.vnode.type && typeof instance.vnode.type === 'object' && 'inheritAttrs' in instance.vnode.type && instance.vnode.type.inheritAttrs === false) {
console.warn('[nuxt] `useId` is not compatible with components that have `inheritAttrs: false`.')
}
const ids = JSON.parse(instance.attrs[ATTR_KEY] as string | undefined || '{}')
ids[instanceIndex] = key + ':' + nuxtApp._id++
instance.attrs[ATTR_KEY] = JSON.stringify(ids)
Expand All @@ -48,6 +45,10 @@ export function useId (key?: string): string {
if (ids[instanceIndex]) {
return ids[instanceIndex]
}

if (import.meta.dev && instance.vnode.type && typeof instance.vnode.type === 'object' && 'inheritAttrs' in instance.vnode.type && instance.vnode.type.inheritAttrs === false) {
console.warn('[nuxt] `useId` might not work correctly with components that have `inheritAttrs: false`.')
}
}

// pure client-side ids, avoiding potential collision with server-side ids
Expand Down