Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    NNext.js

    Next.js foundations

    • What is Next.js?
    • The App Router: folders are routes
    • Dynamic routes
    • Client-side navigation

    Server, client and data

    • Server and Client Components
    • Fetching data on the server
    • loading, error and not-found
    • Caching and revalidation

    Mutations, APIs and security

    • Server Actions
    • Route Handlers: building an API
    • Proxy (formerly Middleware)
    • Authentication and protecting pages

    Going to production

    • Metadata and SEO
    • Images, fonts and performance
    • Environment variables and deploying to Vercel
    • Project: a course catalogue
    Next.js

    Server, client and data · Lesson 7 of 16

    loading, error and not-found

    Special files for loading and error states, and streaming.

    9 minutes1 games

    Next.js turns Suspense and error boundaries into simple files inside any folder:

    • loading.tsx: shown instantly while the folder's page loads its data.
    • error.tsx: shown if the page throws, with a retry button.
    • not-found.tsx: shown when you call notFound().
    • app/courses/
      • layout.tsx
      • loading.tsx → <Suspense fallback>
      • error.tsx → <ErrorBoundary> ("use client")
      • not-found.tsx → notFound()
      • page.tsx

    Next.js wraps page.tsx in these boundaries automatically, and the layout stays visible.

    TypeScript
    // app/courses/loading.tsx
    export default function Loading() {
      return <div className="animate-pulse">Loading courses…</div>
    }
    
    // app/courses/error.tsx: must be a Client Component
    "use client"
    export default function CoursesError({ error, reset }: { error: Error & { digest?: string }; reset: () => void }) {
      return (
        <div>
          <p>Couldn't load the courses.</p>
          <button onClick={() => reset()}>Try again</button>
        </div>
      )
    }

    Streaming with Suspense

    TypeScript
    // The header shows immediately; the slow reviews stream in when ready
    import { Suspense } from "react"
    
    export default function CoursePage() {
      return (
        <main>
          <CourseHeader />                      {/* fast */}
          <Suspense fallback={<p>Loading reviews…</p>}>
            <Reviews />                         {/* slow async Server Component */}
          </Suspense>
        </main>
      )
    }

    Important

    Streaming means the server sends HTML in chunks: ready parts first, a fallback in place of each Suspense, and the real content over the same connection once its data is ready. Users see something quickly even when part of the page is slow.

    Order the lines

    Order what the user sees opening a page with loading.tsx and Suspense around the reviews.

    1. Click the lines in the right order

    Interview question

    Why must error.tsx be a Client Component, and what doesn't it catch?

    Learn more from

    • Next.js docs: loading.jsnextjs.org
    • Next.js docs: error.jsnextjs.org
    • Next.js docs: Error handlingnextjs.org
    • react.dev: <Suspense>react.dev

    Played 0 of 1 games

    Sign in to save your progress and earn XP.

    PreviousFetching data on the serverNextCaching and revalidation
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.