Skip to content

Server-side rendering ​

During SSR, component setup and templates execute on the server without window, and the browser later hydrates over the server's HTML. Two dedicated rules catch browser-only globals and non-deterministic values that break hydration.

In short

  • Enable SSR checks: set "safe-ts": { "vue": { "ssr": true } } in package.json (enabled automatically when nuxt is installed).
  • Guard browser APIs & timestamps: move window, localStorage, new Date(), and Math.random() into onMounted, which runs only in the browser after hydration.
jsonc
"safe-ts": { "vue": { "ssr": true } }

Projects depending on nuxt get both rules automatically.

❌ Rejected (vue-ssr-browser-global, vue-hydration-mismatch)

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

const width = ref(window.innerWidth);
const shownAt = ref(new Date().toLocaleTimeString());
</script>

<template>
  <p>{{ width }}px at {{ shownAt }}</p>
</template>

window doesn't exist on the server (vue-ssr-browser-global). new Date() gives the server one time and the browser another, so the HTML the server sent doesn't match what the browser renders (vue-hydration-mismatch). onMounted only runs in the browser, after hydration:

✅ Accepted

vue
<script setup lang="ts">
import { onMounted, ref, shallowRef } from "vue";
import type { Option } from "@evobug_com/safe-ts-std/result";
import { useListener } from "@evobug_com/safe-ts-vue/listen";

const width = ref(0);
// Unknown on the server: set in the browser, after hydration.
const shownAt = shallowRef<Option<string>>(undefined);

onMounted(() => {
  width.value = window.innerWidth;
  shownAt.value = new Date().toLocaleTimeString();
  useListener(window, "resize", () => {
    width.value = window.innerWidth;
  });
});
</script>

<template>
  <p>{{ width }}px<template v-if="shownAt !== undefined"> at {{ shownAt }}</template></p>
</template>

useListener in onMounted still stops when the component unmounts. (These two examples aren't run by the documentation tests, whose project doesn't render on a server.)

Rules on this page: vue-ssr-browser-global, vue-hydration-mismatch

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