Skip to content

vue-ssr-browser-global ​

A browser global used while the component is set up or rendered, in an app that also renders on a server: window, document, localStorage, sessionStorage, navigator, location, matchMedia, requestAnimationFrame, innerWidth, innerHeight, getComputedStyle.

Why: with server-side rendering, <script setup>, composables and the template run on the server too, to produce the first HTML. There is no window there, so the server render crashes with ReferenceError: window is not defined. onMounted and event handlers only run in the browser. Guide: Vue.

Only reported when the app renders on a server: "safe-ts": { "vue": { "ssr": true } } in the nearest package.json that has a "safe-ts" field, or a project that depends on nuxt. (So the examples below aren't run by the documentation tests.)

❌ Rejected

vue
<script setup lang="ts">
import { ref } from "vue";

const theme = ref(localStorage.getItem("theme") ?? "light");
</script>

<template>
  <p>{{ theme }}</p>
</template>

✅ Accepted

vue
<script setup lang="ts">
import { onMounted, ref } from "vue";

const theme = ref("light");

onMounted(() => {
  theme.value = localStorage.getItem("theme") ?? "light";
});
</script>

<template>
  <p>{{ theme }}</p>
</template>

A typeof window !== "undefined" check doesn't silence it. It avoids the crash, but the server then renders one thing and the browser another, which is vue-hydration-mismatch's problem. typeof window itself is fine.

What the rule can't see: browser globals in a computed getter or a watcher with immediate: true, which also run on the server; and globals reached through globalThis.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra