Vue · SSR safety

VUE0042Do not initialize SSR-rendered state from a browser read in setup

Flags ref(), reactive() and computed() state whose initial value comes from a browser read during setup of server-rendered Vue code, directly or through a helper.

Why it happens

The server cannot read localStorage, navigator.onLine or the viewport, so it renders the fallback, while the first client render starts from the browser value. Vue then logs a hydration mismatch and patches or rebuilds the subtree. A typeof window or import.meta.client guard prevents the server crash but is exactly what makes the two renders differ. Doctor follows the read through local variables and through functions in the same file or a relative import.

Fix

Initialize the state to a value the server can compute too, such as the prop default, and apply the browser value in onMounted(). In a Nuxt app, keep state the server must render in useState() or useCookie(), or render the subtree in <ClientOnly>.

Example

Apply stored layout after mount

Before

setup(props) {
  const stored = readLayout(props.storageKey) // reads localStorage when it exists
  const width = ref(stored.width ?? props.defaultWidth)
}

After

setup(props) {
  const width = ref(props.defaultWidth)
  onMounted(() => { width.value = readLayout(props.storageKey).width ?? width.value })
}

Verify the fix

Run only this rule after editing so the report stays focused on the diagnostic you are closing:

pnpm vite-doctor . --framework vue --rules vue/ssr/no-setup-state-from-browser-read
Copyright © 2026