#36428·nuxt

Tree-shaking `onServerPrefetch` from the client build breaks `useId()` after hydration (outside `useAsyncData`)

Author: zsilbiCreated Sep 28, 2026Updated Sep 28, 2026
Labels4.x5.x

Environment

  • 5.0.0-29841792.9ceeba8c and also 4.6.0-29842981.c8eae485
  • Vue: 3.5.42 and Vue 3.6.0-rc.9
  • @nuxt/icon: 2.5.1
  • Node: 24.15.0
  • Builder: Vite
  • Only a production build is affected (nuxi build, then preview). nuxi dev is fine.

Reproduction

Any component that calls onServerPrefetch directly (not through useAsyncData), rendered before a component that uses useId():

xml
<!-- components/Prefetching.vue -->
<script setup lang="ts">
onServerPrefetch(async () => {
  // any server-only work
})
</script>

<template><span>prefetching</span></template>
xml
<!-- components/LabelledInput.vue -->
<script setup lang="ts">
const id = useId()
</script>

<template>
  <label :for="id">Name</label>
  <input :id="id" name="name">
</template>
xml
<!-- app.vue -->
<template>
  <Prefetching />
  <LabelledInput />
</template>
  1. Run nuxi build, then nuxi preview.
  2. Open the page with a full load, not a client-side navigation.
  3. Compare the label's for with the input's id after hydration.

A real-world trigger: @nuxt/icon's CSS-mode icon component (NuxtIconCss) calls onServerPrefetch in its setup. So every CSS icon rendered before a form shifts the ids of that form.

Describe the bug

Nuxt tree-shakes onServerPrefetch out of the client bundle by default. It is in optimization.treeShake.composables.client.vue.

Vue treats a component that registered a server-prefetch hook as an async boundary when it numbers useId(). It calls markAsyncBoundary(instance) when isAsyncSetup || instance.sp (runtime-core, setupStatefulComponent).

With the call tree-shaken, instance.sp is set on the server but not in the browser. So the server counts one more boundary for each such component, and every useId() after it gets a different value on the server and in the browser.

After hydration of a production build, some elements keep the id from the server render and others get the id the browser computed. The references between them break. What we saw:

  • <label for="v-0-0-22-4"> next to <input id="v-0-1-4">. Clicking the label does not focus the input, and the input has no accessible name (axe: label, critical).
  • <input id="v-0-1-9" aria-describedby="v-0-0-22-9-help">. The description is lost.
  • <button type="submit" form="v-0-0-22-5"> next to <form id="v-0-0-5">. Clicking the button submits nothing, because the button names a form that does not exist.
  • The aria-controls of Reka UI collapsibles pointed at ids that no element has (axe: aria-valid-attr-value, critical).

In our app, the 22 in v-0-0-22-… was exactly the number of onServerPrefetch components (21 CSS icons in a sidebar plus one other component) rendered before the page content.

Expected

useId() gives the same value on the server and in the browser, whatever calls onServerPrefetch earlier in the tree. The hook's callback never runs in the browser anyway.

Additional context

  • nuxt/nuxt#30289 reported the same effect for useAsyncData. PR #30343 fixed only that path: it sets instance.sp = [] in the browser. Any other caller of onServerPrefetch still hits the problem, for example a module component like NuxtIconCss, app code, or a library.
  • vuejs/core#12591 describes the root cause and is still open. In that thread, the Vue team notes that the compiler cannot find every onServerPrefetch call. So this is hard to fix on the Vue side alone.
  • Nuxt's own nuxt-root.vue calls onServerPrefetch in its onErrorCaptured handler during a server render. We did not test it, but an error page could shift ids the same way.

Possible fixes:

  1. Do not tree-shake onServerPrefetch in the client build. The call stays, the callback never runs in the browser, and the cost is one small function reference.
  2. Or, in the client build, replace the call with a stub that marks the instance the way #30343 does (instance.sp ||= []), instead of removing it.

Workaround we use now: a small inline module that keeps onServerPrefetch in the client build:

typescript
// nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    (_options, nuxt) => {
      const client = nuxt.options.optimization.treeShake.composables.client
      if (client.vue) {
        client.vue = client.vue.filter(name => name !== 'onServerPrefetch')
      }
    },
  ],
})

With this module, all ids match after hydration. The labels, the descriptions, the aria-controls and the form= references all resolve again.

Logs

shell-script