Next.js

Proxy, Matchers & Edge Routing

28 min Lesson 48 of 80

Proxy, Matchers & Edge Routing

This lesson expands the Next.js path with an advanced topic from the official Next.js documentation. The goal is not only to memorize an option or file name, but to understand its impact on rendering, caching, security, and deployment.

After this lesson you should be able to apply the topic in a real project, choose the right boundary for it, and explain it as a reviewable engineering decision.

Core Concepts

  • proxy.ts convention
  • matcher constants
  • negative matching
  • NextRequest
  • NextResponse

Practical Example

// proxy.ts import { NextResponse, type NextRequest } from 'next/server' export function proxy(request: NextRequest) { const session = request.cookies.get('session')?.value if (!session && request.nextUrl.pathname.startsWith('/dashboard')) { return NextResponse.redirect(new URL('/login', request.url)) } return NextResponse.next() } export const config = { matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'], }
This lesson is aligned with these official Next.js documentation areas: Proxy file convention and matcher docs.

Why It Matters

In production applications, this topic affects page speed, data freshness, authorization clarity, and operational reliability after deployment.

Implementation Workflow

  • Decide whether the data is public or user-specific.
  • Choose the smallest part of the tree that needs this behavior.
  • Connect the example to a real route and add a small verification check.
  • Document the effect on caching and deployment.

Hands-on Practice

Migrate an auth guard from middleware.ts to proxy.ts and exclude static assets from the matcher.

Without a matcher, Proxy may run for images, scripts, and static files unnecessarily.

Summary

Judge the implementation by how clear the decision is, whether the behavior is correct after build, and how easily it can be traced in production.