Skip to content

Nuxt.js SSR Rendering Error Fix

DodaTech Updated 2026-06-24 3 min read

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 window and document before using them
  • Wrap browser-only components in <ClientOnly>
  • Use onMounted for browser API access
  • Configure ssr: false on pages with heavy browser dependencies

Common Mistakes with ssr error

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. 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

### What is the difference between ssr: false per page and globally?

Global ssr: false disables SSR for the entire application — every page is client-rendered. Per-page routeRules.ssr: false disables SSR only for specific routes, preserving SSR benefits for the rest of the app.

Can I use third-party Vue plugins with Nuxt SSR?

Most Vue plugins work with SSR if they check for browser APIs. Plugins that access window, document, or localStorage in their setup functions break during SSR. Use process.client guards or dynamic imports in these cases.

How do I debug SSR rendering errors?

Set debug: true in nuxt.config.js and check the terminal output for the full error stack trace. Nuxt shows which component and line caused the SSR failure. Use ssr: false temporarily to identify browser-only code.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro