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

    Mutations, APIs and security · Lesson 11 of 16

    Proxy (formerly Middleware)

    Code that runs before each request: redirects, route protection and headers.

    10 minutes2 games

    A proxy.ts file at the project root runs before any request reaches a page. It can redirect (not signed in? go to /login), rewrite (serve a different page without changing the URL), or set headers and cookies. In Next.js 16 this file was renamed from middleware.ts to proxy.ts; the old name still works but is deprecated.

    TypeScript
    // proxy.ts (project root)
    import { NextResponse, type NextRequest } from "next/server"
    
    const PROTECTED = ["/dashboard", "/classroom", "/settings"]
    
    export function proxy(request: NextRequest) {
      const { pathname } = request.nextUrl
      const hasSession = request.cookies.has("sb-access-token")
    
      if (PROTECTED.some((p) => pathname === p || pathname.startsWith(p + "/")) && !hasSession) {
        const login = new URL("/login", request.url)
        login.searchParams.set("next", pathname)          // come back here after login
        return NextResponse.redirect(login)
      }
      return NextResponse.next()
    }
    
    // Only run on these paths (skip static files and images)
    export const config = {
      matcher: ["/((?!_next/static|_next/image|favicon.ico|.*\\.png$).*)"],
    }

    Request: GET /dashboard

    proxy.ts: is there a session?

    no → redirect to /login?next=/dashboard

    yes → the page (which must check again)

    Important

    Proxy is a useful first layer for quick redirects, but it must not be the only protection: a matcher may miss a route, and in 2025 a vulnerability (CVE-2025-29927) allowed skipping middleware entirely. Check the user and their permissions again inside the page, server action or route handler that touches the data.

    Complete the code

    Complete it so anyone opening /old-courses is sent to /courses.

    export function proxy(request: NextRequest) {
      if (request.nextUrl.pathname === "/old-courses") {
        return NextResponse.(new URL("/courses", request.url))
      }
      return NextResponse.()
    }

    Interview question

    What's the difference between a redirect and a rewrite?

    Exercise

    Write decide(path, role) like a proxy: if the path starts with /admin and the role isn't admin, return redirect:/dashboard. If it starts with /dashboard and there's no role (null), return redirect:/login. Otherwise return next.

    Expected output:
    redirect:/dashboard
    next
    redirect:/login
    next
    JavaScript
    function decide(path, role) {
      return "next"
    }
    
    console.log(decide("/admin/users", "student"))
    console.log(decide("/admin/users", "admin"))
    console.log(decide("/dashboard", null))
    console.log(decide("/courses", null))

    Learn more from

    • Next.js docs: proxy.jsnextjs.org
    • Next.js blog: Next.js 16 (middleware renamed to proxy)nextjs.org
    • Vercel: CVE-2025-29927 postmortemvercel.com
    • MDN: Redirections in HTTPdeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousRoute Handlers: building an APINextAuthentication and protecting pages
    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.