Home
NextJS
NextJS - Proxy
October 10, 2023
1 min

Proxy allows you to run code before a request is completed, enabling you to modify how the request or response is handled.

Common Use Cases

Proxy is useful for tasks such as:

  • Modifying headers for all pages or a subset of pages
  • Rewriting to different pages based on A/B tests or experiments
  • Programmatic redirects based on incoming request properties

For simple redirects, it’s recommended to use redirects in next.config.ts. Use Proxy only when request-based logic is required.

File Convention

Create a file named proxy.ts (or proxy.js) in the project root.

Example structure:

proxy.ts
app/
pages/

Rules:

  • Only one proxy.ts file per project
  • Can import logic from other files to keep it modular

Basic Example

import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
export function proxy(request: NextRequest) {
return NextResponse.redirect(new URL("/home", request.url))
}

This example redirects incoming requests to /home.


Tags

#NextJS

Share

Related Posts

NextJS
NextJS - Route Handlers
October 08, 2023
1 min
© 2026, All Rights Reserved.
Powered By

Social Media

githublinkedinyoutube