May 08, 2026

Killing the White Screen of Death: Error Boundaries in Next.js

By Paresh Prajapati • Lead Architect

Killing the White Screen of Death: Error Boundaries in Next.js

The Fragility of the React Tree

One of the most dangerous behaviors of a React application is how it handles unhandled JavaScript exceptions. By default, if a single component throws an error (e.g., trying to read .map() on an undefined API response), React instantly unmounts the entire component tree. For a B2B SaaS user looking at an intricate dashboard, a failing weather widget will cause the entire screen to go completely blank—the dreaded "White Screen of Death."

At Smart Tech Devs, we architect our frontends for resiliency. A failure in a non-critical component should never crash the core application. We solve this by leveraging Error Boundaries within the Next.js App Router.

Architecting Isolation with `error.tsx`

Next.js 13+ introduced a file-system-based routing convention that makes Error Boundaries incredibly elegant to implement. By creating an error.tsx file in a specific route segment, you automatically wrap that segment and its children in a React Error Boundary.

Step 1: Creating a Granular Error Boundary

If the invoices widget fails, we only want the invoices section to show an error state. The sidebar, header, and other widgets must remain active.


// app/dashboard/invoices/error.tsx
"use client"; // Error components MUST be Client Components

import { useEffect } from 'react';

export default function InvoicesError({
    error,
    reset,
}: {
    error: Error & { digest?: string };
    reset: () => void;
}) {
    useEffect(() => {
        // Log the error to an external service like Sentry
        console.error("Invoice Widget Failed:", error);
    }, [error]);

    return (
        <div className="p-4 border border-red-500 bg-red-50 rounded-md">
            <h2 className="text-red-700 font-bold">Failed to load invoices.</h2>
            <p className="text-sm text-red-600 mb-4">We encountered a network issue.</p>
            
            {/* The 'reset' function attempts to re-render the failed segment */}
            <button 
                onClick={() => reset()} 
                className="bg-red-600 text-white px-4 py-2 rounded shadow"
            >
                Try Again
            </button>
        </div>
    );
}

The Fallback Hierarchy

The beauty of the App Router is its nested hierarchy. If an error occurs in app/dashboard/invoices/page.tsx, Next.js will look for the nearest error.tsx file. If it finds one in the invoices folder, it replaces only that specific page component with the error UI.

For global, catastrophic failures, you should always include a global-error.tsx file in your root app/ directory. This acts as the absolute last line of defense, ensuring that even if your root layout crashes, the user is presented with a branded, professional "Something went wrong" page instead of a raw browser error.

Conclusion

In enterprise software, failure is inevitable, but catastrophic UI crashes are optional. By strategically placing Next.js Error Boundaries around complex or risky data-fetching components, you quarantine errors, protect the user experience, and build a SaaS platform that degrades gracefully under pressure.

Paresh Prajapati
Lead Architect, Smart Tech Devs