Nuxt.js SSR Rendering Error Fix
In this tutorial, you'll learn about Nuxt.js SSR Rendering Error Fix. We cover key concepts, practical examples, and best practices.
The Problem
Nuxt.js throws window is not defined or document is not defined during server-side rendering. SSR runs JavaScript on the server where browser APIs are unavailable. Components that access window, document, or browser-specific APIs break during SSR.
Quick Fix
Step 1: Check if the code runs only on the client
// Wrong — window accessed directly in setup
const width = window.innerWidth;
const height = document.body.clientHeight;
// Right — guard with process.client check
const width = process.client ? window.innerWidth : 0;
Expected output: The server uses fallback values; the client uses browser APIs.
Step 2: Use wrapper
<template>
<!-- Wrong — component breaks during SSR -->
<BrowserComponent />
<!-- Right — only rendered on client -->
<ClientOnly>
<BrowserComponent />
<template #fallback>
<div>Loading...</div>
</template>
</ClientOnly>
</template>
Expected output: The component only renders in the browser.
Step 3: Disable SSR for specific pages
// nuxt.config.js
export default defineNuxtConfig({
routeRules: {
'/analytics': { ssr: false },
},
});
Expected output: The /analytics page is client-side rendered only.
Step 4: Use onMounted for browser-only logic
<script setup>
// Wrong — runs during SSR
const height = document.body.clientHeight;
// Right — only runs in browser
import { ref, onMounted } from 'vue';
const height = ref(0);
onMounted(() => {
height.value = document.body.clientHeight;
});
</script>
Expected output: Browser APIs are only accessed after the component mounts.
Step 5: Configure ssr: false globally
// nuxt.config.js
export default defineNuxtConfig({
ssr: false, // disable SSR for the entire app
});
Expected output: The app becomes a client-side SPA with no SSR errors.
Step 6: Import browser-only libraries dynamically
// Wrong — static import breaks SSR
import Chart from 'chart.js';
// Right — dynamic import only on client
if (process.client) {
const Chart = await import('chart.js');
}
Expected output: Browser-only libraries are loaded only on the client.
Prevention
- Always check for
windowanddocumentbefore using them - Wrap browser-only components in
<ClientOnly> - Use
onMountedfor browser API access - Configure
ssr: falseon pages with heavy browser dependencies
Common Mistakes with ssr error
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world NUXTJS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro