Nuxt

nuxt/no-module-scope-state

Never create ref() or reactive() state at module scope in a Nuxt app; use useState or a Pinia store.

poważnastatycznefe-nuxt

Dlaczego

A module is loaded once per server process and shared by every request it serves. export const user = ref(null) in a composable file therefore holds the last visitor's data for the next one: one user can see another user's name, cart or token in server-rendered HTML, and the value never gets freed.

Poprawka

Create state inside a function that runs per request: export const useUser = () => useState('user', () => null), or a Pinia store (defineStore), which Nuxt creates per request. A plain <script> block of a .vue file is module scope too; <script setup> is per component. Client-only files (*.client.ts plugins) may keep module state.

Przykłady

// bad (composables/user.js)
export const user = ref(null)
// good
export const useUser = () => useState('user', () => null)

Źródła

Fakty

Identyfikator
nuxt/no-module-scope-state
Waga
poważna
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
vue jsx
Tylko w projektach z
nuxt
Uczy jej skill
fe-nuxt Nuxt apps
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain nuxt/no-module-scope-state