Skip to content
RoutingChapter 10 / 34Intermediate4 min read

Loading, Error & Not Found UI

Next.js loading, error and not-found files: build an instant loading state with loading.tsx, an error boundary with error.tsx and a 404 page with notFound().

On this page

1. What are loading, error and not found UI?

Instead of writing isLoading and try catch by hand in every page, you put these files in the segment folder. Next.js turns them into React Suspense and Error Boundary wrappers.

2. Why does it matter?

Without special filesWith special files
Blank screen until the data arrivesloading.tsx shows a skeleton right away
One error crashes the whole apperror.tsx gives a fallback to only that segment
Empty page on a wrong IDnotFound() gives a proper 404 UI and status
Loading code in every pageOne file covers the whole segment

3. Where is it used?

FileWhen it appears
loading.tsxWhile the page data is loading
error.tsxWhen an unexpected error happens while rendering the segment
not-found.tsxWhen notFound() is called or no URL matches
global-error.tsxWhen the root layout itself fails

Real examples: a skeleton for a product page, a "Try again" button when a dashboard widget fails, and a 404 for a deleted blog post.

4. How does it work?

Next.js wraps the files of a segment in this order:

Component hierarchy of a segment
layout.tsx
template.tsx
error.tsx (Error Boundary)
loading.tsx (Suspense fallback)
not-found.tsx
page.tsx

Two things follow from this:

  • error.tsx does not catch errors from the layout of the same segment, because the layout sits above it. The error.tsx of the parent segment catches them.
  • While loading.tsx is showing, the layout stays interactive, so the user can still use the navbar and sidebar.

5. Code example

loading.tsx

tsx
export default function Loading() {
  return <div className="animate-pulse h-40 rounded bg-gray-200" />;
}

error.tsx

tsx
"use client";

import { useEffect } from "react";

export default function Error({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  useEffect(() => {
    console.error(error);
  }, [error]);

  return (
    <div>
      <h2>Something went wrong</h2>
      <button onClick={() => reset()}>Try again</button>
    </div>
  );
}

notFound() and not-found.tsx

tsx
import { notFound } from "next/navigation";

export default async function ProductPage({ params }: PageProps<"/products/[id]">) {
  const { id } = await params;
  const res = await fetch(`https://api.example.com/products/${id}`);
  if (res.status === 404) notFound();
  const product = await res.json();
  return <h1>{product.name}</h1>;
}
tsx
import Link from "next/link";

export default function NotFound() {
  return (
    <div>
      <h2>Product not found</h2>
      <Link href="/products">View all products</Link>
    </div>
  );
}

How does the code work?

PartMeaning
loading.tsxWraps the page in Suspense and shows a skeleton until the data arrives
"use client" in error.tsxError boundaries must be Client Components
error.digestA hash of the server error, used to match it with the server logs
reset()Clears the error state and renders the segment again
notFound()Stops rendering and shows the nearest not-found.tsx

Interview tip

"Who catches an error from the root layout?" app/global-error.tsx, and it must contain its own html and body tags because it replaces the root layout.

6. Common mistakes

Mistake: Forgetting use client in error.tsx

Why? An error boundary must be a Client Component, otherwise you get a build error.

Better: Write use client on the first line of error.tsx and global-error.tsx.

Mistake: Showing error.message to the user in production

Why? In production, the message of a server error is generic so that sensitive details do not leak.

Better: Show the user a friendly message and use the digest to find the real error in the server logs.

Mistake: Expecting the error.tsx in the same folder to catch a layout error

Why? error.tsx sits inside the layout of its own segment, not above it.

Better: Put error.tsx in the parent segment for layout errors, and use global-error.tsx for the root.

7. What to learn before and after

Final quick revision

PointDetail
loading.tsxSuspense fallback, an instant loading state
error.tsxA Client Component Error Boundary
not-found.tsxShown on notFound() or an unmatched URL
global-error.tsxErrors from the root layout, with its own html and body
Recoveryreset(), and retry() in 16.3+