Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

Next.js proxy.ts Explained: A Practical Cheat Sheet

A practical Next.js 16 guide to proxy.ts: placement, exports, matchers, responses, runtime limits, security boundaries, and migration from middleware.ts.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

proxy.ts is Next.js’s project-level hook for handling a request before routing finishes: it can redirect or rewrite a request, change headers, or return a response. In Next.js 16, the older Middleware convention was renamed and deprecated in favor of Proxy; the core functionality remains the same. Use it for request-dependent routing decisions, not as your application’s sole authorization or session-management layer.

What is proxy.ts in Next.js?

Proxy runs code before a request is completed. It can inspect a request and decide how Next.js should handle it—for example, by redirecting to another URL, rewriting the destination, changing request or response headers, or returning a response directly. The official Next.js Proxy overview describes request-dependent redirects, experiment rewrites, and header changes as suitable uses.

In Next.js 16, Proxy is the renamed and deprecated-replacement convention for Middleware. The Proxy API reference records the change in v16.0.0; the version 16 upgrade guide explains the runtime change to account for as well.

Where does proxy.ts go?

Put one Proxy file at the project root, or inside src at the same level as app or pages. A project supports only one Proxy file. If your project customizes pageExtensions, use the corresponding naming convention, such as proxy.page.ts. These conventions are documented in the API reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I use proxy.ts?

Export one function, either as a named proxy export or as the default export. The optional config object can define where Proxy runs. This example redirects matching requests to /home:

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function proxy(request: NextRequest) {
  return NextResponse.redirect(new URL('/home', request.url))
}

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

NextResponse can redirect or rewrite, set request or response headers, set cookies, or allow the request to continue. A Proxy function may also return a standard Response. See the API reference for the complete API.

How do matchers control where Proxy runs?

Proxy is invoked for project routes, so use a matcher to scope the paths and conditions where your logic applies. Matcher values must be statically analyzable constants; dynamically computed values are ignored at build time.

  • A matcher can be a string, an array of strings, or an object with a source and optional locale, has, or missing conditions.
  • Conditions can test request headers, query parameters, or cookies.
  • Patterns begin with /. Named path parameters support *, ?, and + modifiers, and regular expressions are also supported.

For example, '/about/:path*' matches /about and paths nested beneath it. Choose the matcher deliberately: excluding a path can also prevent Proxy from running for Server Function calls made on that path. Matcher syntax and execution behavior are described in the API reference.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When should I use Proxy instead of next.config redirects?

Choose based on whether the decision depends on the incoming request or needs logic beyond a static mapping. Next.js processes headers and redirects from next.config.js before Proxy; Proxy then runs before beforeFiles rewrites and filesystem routes.

Approach Use it when Trade-off
redirects in next.config A straightforward redirect can be declared statically. Does not provide Proxy’s request-dependent logic.
proxy.ts The redirect or rewrite depends on request data, or you need logic such as an experiment decision or header change. Adds a request-time hook; keep its work lightweight rather than using it for slow data fetching.

Next.js says fetch options such as cache, next.revalidate, and next.tags have no effect in Proxy. For use cases and limitations, see the getting-started guide.

What runtime does Proxy use?

Proxy uses the Node.js runtime by default. You cannot configure a runtime option in the Proxy file, and the Next.js 16 upgrade guide says Edge is not supported for Proxy. When upgrading, check that the project’s deployment and any libraries used by the hook fit the runtime available in the Next.js version you run. The API reference and version 16 guide document these constraints.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Is Proxy enough to authorize a request?

No. Proxy can make an optimistic routing decision, such as redirecting a request that appears unauthenticated, but it is not a full authorization or session-management solution. Enforce access control in the relevant Server Function, route handler, or other application code that performs the protected operation. This matters especially when a matcher skips a path: a Server Function called on that path may not pass through Proxy. The API reference explicitly advises verifying authorization inside each Server Function.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I migrate middleware.ts to proxy.ts?

The rename applies to the Next.js 16 convention. The official migration guidance provides the codemod below as a starting point:

npx @next/codemod@canary middleware-to-proxy .
  1. Rename middleware.ts or middleware.js to proxy.ts or proxy.js.
  2. Rename a named middleware export to proxy. A default export remains an available function-export form.
  3. Rename Middleware-specific configuration flags too; for example, skipMiddlewareUrlNormalize becomes skipProxyUrlNormalize.
  4. Review the matcher, runtime assumptions, and authorization checks in application code after the codemod. Confirm that deployment settings and libraries are compatible with Proxy’s Node.js runtime.

If you are not upgrading to Next.js 16, check the documentation for the version your project actually runs before changing conventions; the rename and runtime guidance here are specific to the documented Next.js 16 transition.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.