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 6 of 16

    Fetching data on the server

    Async components, waterfalls, Promise.all, and data from Supabase.

    10 minutes2 games

    A Server Component can be async, so you fetch data with await right inside it: no useEffect, no loading state, no in-between API. This code never reaches the browser, so secret keys are safe.

    TypeScript
    // app/dashboard/page.tsx
    import { createClient } from "@/utils/supabase/server"
    
    export default async function Dashboard() {
      const supabase = await createClient()
      const { data: { user } } = await supabase.auth.getUser()
    
      // ❌ Waterfall: the second query waits for the first for no reason
      // const courses = await getCourses(user.id)
      // const badges = await getBadges(user.id)
    
      // ✅ Independent queries in parallel
      const [courses, badges] = await Promise.all([getCourses(user!.id), getBadges(user!.id)])
    
      return <p>{courses.length} courses · {badges.length} badges</p>
    }

    Sequential: courses (300ms) then badges (300ms) = 600ms

    Parallel with Promise.all: both together = 300ms

    Waterfalls are the most common cause of slow pages. Start all independent requests together.

    Important

    If two components on one page need the same data, request it in both: a fetch with the same URL and options is de-duplicated within one request. For database functions use React's cache() (const getUser = cache(async (id) => …)) so they run once per request.

    Don't create a route handler (/api/courses) for a Server Component to call: that's an extra HTTP request from the server to itself. Call the data function directly. Route handlers are for other clients (mobile apps, webhooks, client components).

    Quick question

    A page loads the user, then their courses (needs the user id), plus announcements (independent). Best order?

    Interview question

    What is a request waterfall, and how do you avoid it in Next.js?

    Exercise

    getCourses and getBadges each take 200 ms. Fetch them together and print 2 courses, 3 badges, then fast if the total took under 350 ms (otherwise slow).

    Expected output:
    2 courses, 3 badges
    fast
    JavaScript
    const wait = (ms, value) => new Promise((r) => setTimeout(() => r(value), ms))
    const getCourses = () => wait(200, ["React", "SQL"])
    const getBadges = () => wait(200, ["streak", "first-run", "boss"])
    
    const start = Date.now()
    const courses = await getCourses()
    const badges = await getBadges()
    console.log(courses.length + " courses, " + badges.length + " badges")
    console.log(Date.now() - start < 350 ? "fast" : "slow")

    Learn more from

    • Next.js docs: Fetching datanextjs.org
    • react.dev: cachereact.dev
    • Supabase docs: Server-side auth for Next.jssupabase.com
    • MDN: Promise.all()developer.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousServer and Client ComponentsNextloading, error and not-found
    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.