NUXT0084Keep request state out of module scope
Why it happens
On the server, Nuxt imports each app module once and reuses it for every SSR request. A module-scope let, Map, or Set that a store action, composable, plugin, or component setup writes during a request is shared by all concurrent requests: one user's fetched data, store instance, or in-flight promise becomes visible to the next request. An in-flight promise that closes over this in a Pinia action fills the first request's store, while later requests await it and keep their own store empty.
Fix
Keep per-request state in useState() or the Pinia store state. For values that cannot live in payload state, such as in-flight promises, key them by the Nuxt app with a WeakMap so each SSR request gets its own entry. Constant lookup tables and caches of pure values, such as Intl formatters, can stay at module scope.
Example
Key in-flight requests by the Nuxt app
Before
let currencyPromise: Promise<void> | null = null
export const useSettings = defineStore('settings', {
state: () => ({ currencies: [] as string[] }),
actions: {
async loadCurrencies() {
currencyPromise ??= (async () => {
this.currencies = await $fetch('/api/currencies')
})()
await currencyPromise
},
},
})
After
const currencyPromises = new WeakMap<NuxtApp, Promise<void>>()
export const useSettings = defineStore('settings', {
state: () => ({ currencies: [] as string[] }),
actions: {
async loadCurrencies() {
const nuxtApp = useNuxtApp()
let promise = currencyPromises.get(nuxtApp)
if (!promise) {
promise = (async () => {
this.currencies = await $fetch('/api/currencies')
})()
currencyPromises.set(nuxtApp, promise)
}
await promise
},
},
})
Verify the fix
Run only this rule after editing so the report stays focused on the diagnostic you are closing:
pnpm nuxt doctor --extends auto,nuxt/strict --rules nuxt/ssr/no-module-scope-request-state