Advanced
Advanced

Proxy Request Boundaries

Implement lightweight request interception with Next.js Proxy for redirects, headers, and coarse route gates.

25 min
2 sections
proxy
redirects
auth
1
2

01. Creating Proxy

Section 1 of 2

Proxy runs before a matched request completes. Use it for lightweight routing decisions, redirects, rewrites, and headers. Keep authorization and business rules in server-side data access and mutations.

typescript
// proxy.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export function proxy(request: NextRequest) {
  // Add custom header
  const response = NextResponse.next();
  response.headers.set('x-custom-header', 'value');
  return response;
}

export const config = {
  matcher: '/about/:path*',
};

Exercise

Create a Locale Proxy

Practice

Create a Proxy that checks for a locale cookie and redirects to the appropriate localized route.

Back to Course