Skip to content

Commit

Permalink
fix(nuxt): only warn for useId if attrs were not rendered (#25770)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 13, 2024
1 parent 37d24ee commit a1c1fda
Showing 1 changed file with 5 additions and 4 deletions.
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

0 comments on commit a1c1fda

Please sign in to comment.