Nuxt
nuxt/no-browser-api-in-setup
Never touch window, document, localStorage or navigator at the top of script setup; it also runs on the server.
poważnastatycznefe-nuxt
Dlaczego
With server rendering, <script setup> runs first on the server, where window, document and localStorage do not exist: the page answers with a 500 error, or, when the access is guarded badly, the server and browser render different HTML and hydration throws the server's markup away.
Poprawka
Read browser state after mounting (onMounted(() => { theme.value = localStorage.getItem('theme') })), guard with if (import.meta.client), use a cookie that both sides can read (useCookie('theme')), or render the part only in the browser (<ClientOnly> or a *.client.vue component).
Przykłady
// bad
const theme = ref(localStorage.getItem('theme') ?? 'light')
// good
const theme = useCookie('theme', { default: () => 'light' })Źródła
Fakty
- Identyfikator
nuxt/no-browser-api-in-setup- Waga
- poważna
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
vue- Tylko w projektach z
nuxt- Uczy jej skill
fe-nuxtNuxt apps- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain nuxt/no-browser-api-in-setup