Keep request data out of module-scope state
nuxt/ssr/no-request-data-in-module-statesrc/rule-packs/nuxt/rules/nuxt/no-request-data-in-module-state.ts Upstream docs Run this rule
pnpm nuxt doctor --rules nuxt/ssr/no-request-data-in-module-state
Why it matters
On the server, Nuxt imports the app bundle once and creates a new Vue app and Nuxt app for every request, but module-scope values are not recreated. A ref, reactive object, or plain object declared at module scope, or in the plain <script> block of a component, is shared by every request. When setup, a composable, a plugin, or route middleware writes the route, request headers, cookies, or the request event into it during server rendering, concurrent requests overwrite each other and one user's data can render for another. Doctor skips state created inside setup or the composable, useState, writes in onMounted, behind import.meta.client, or in event handlers, client-only files, apps that turn SSR off, and helpers it cannot trace to Vue or Nuxt. Map, Set, and reassigned module variables are reported by nuxt/ssr/no-module-scope-request-state.
Recommended fix
Create state per request in local variables or with setup-local ref() or reactive(). Keep credentials and request objects in server-only request context. Nuxt writes useState() to nuxtApp.payload.state, so use it only for serializable values intended for the browser and safe to send in the hydration payload.
Useful links
Example
Create request state with useState
Before
const activeFilter = ref<string>()
export function useFilters() {
activeFilter.value = useRoute().query.filter as string
return activeFilter
}
After
export function useFilters() {
const activeFilter = useState<string | undefined>('active-filter')
activeFilter.value = useRoute().query.filter as string
return activeFilter
}