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

    Caching and revalidation

    "use cache", cacheLife and cacheTag, and refreshing data after changes.

    12 minutes2 games

    If every visit to the courses page queries the database, it's slow and costly. A cache stores the result and reuses it. The hard question is always when the stored copy is stale and must refresh.

    In Next.js 16 with Cache Components (cacheComponents: true in next.config.ts), anything not cached is fetched per request, and you explicitly mark what to cache with "use cache":

    TypeScript
    // lib/courses.ts
    import { cacheLife, cacheTag } from "next/cache"
    
    export async function getCatalog() {
      "use cache"
      cacheLife("hours")          // a built-in profile: refresh in the background every hour or so
      cacheTag("courses")         // a label we can invalidate later
      return db.course.findMany({ where: { published: true } })
    }
    TypeScript
    // app/admin/actions.ts: after an admin edits a course
    "use server"
    import { revalidatePath, updateTag } from "next/cache"
    
    export async function publishCourse(id: string) {
      await db.course.update({ where: { id }, data: { published: true } })
      updateTag("courses")          // expire everything tagged "courses" now (read-your-own-writes)
      revalidatePath("/courses")    // and re-render this path
    }
    FunctionWhat it does

    "use cache"

    cache a function's, component's or page's output

    cacheLife("hours")

    how long the copy stays fresh

    cacheTag("courses")

    a label for invalidation

    updateTag(tag)

    expire now from a server action; the user sees their change

    revalidateTag(tag, "max")

    mark stale; serve old until the new is ready

    revalidatePath("/courses")

    rebuild a specific path

    Important

    Next.js caching changed a lot between versions: in 14 fetch was cached by default, in 15 it stopped being, and in 16 "use cache" arrived as the explicit approach. When reading an article or answer, check the version first. SeaCode itself runs Next.js 16.

    Spot the bug

    An admin adds a course, but the courses page only shows it an hour later. What's wrong?

    Interview question

    What is ISR? How does time-based revalidation differ from on-demand?

    Quick question

    A student renames themself in settings and must see the new name immediately after saving. What do you use in the server action?

    Learn more from

    • Next.js docs: Cachingnextjs.org
    • Next.js docs: Revalidatingnextjs.org
    • web.dev: Keeping things fresh with stale-while-revalidateweb.dev
    • Next.js docs: use cachenextjs.org
    • Next.js docs: updateTagnextjs.org
    • Next.js blog: Next.js 16nextjs.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    Previousloading, error and not-foundNextServer Actions
    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.