Next.js Curriculum
HardAdvanced Routing100% Free30 min read

Network Routing: Edge Middleware to Next.js 16 proxy.ts

The 30-Second Interview Answer

Historically, Next.js utilized middleware.ts running exclusively in the Edge Runtime. In Next.js 16, the network interception model is modernized with proxy.ts. Running in the standard Node.js runtime, proxy.ts removes Edge Runtime restrictions, allowing developers to use native Node.js libraries, full database drivers, and standard networking APIs while handling request rewriting, redirects, and header mutations.

Learning Objectives

Understand the architectural progression from middleware.ts to Next.js 16 proxy.ts.

Explain the difference between Edge Runtime sandbox constraints and Node.js runtime capabilities.

Implement URL rewrites, subpath routing, and header enrichment.

Avoid performance degradation when performing network routing at the edge or origin.

Prerequisites & Background Knowledge
HTTP reverse proxiesEdge runtime sandboxingURL rewriting vs redirection

Why Interviewers Care

A cutting-edge architectural interview question verifying if the candidate understands Next.js 16’s transition away from Edge-only network middleware.

The Challenge

Trace the evolution of network routing from Edge middleware.ts to Next.js 16 proxy.ts. Why was running network logic strictly in the Edge runtime problematic, and what capabilities does the Node.js runtime restore?

Code Architecture & Implementation

1// proxy.ts (Next.js 16 convention)
2import { NextRequest, NextResponse } from 'next/server';
3// In Next.js 16 proxy.ts, full Node.js APIs and npm packages are supported!
4import { verifySessionWithFullDb } from '@/lib/node-auth';
5
6export async function proxy(request: NextRequest) {
7 const { pathname } = request.nextUrl;
8
9 // 1. Multi-Tenant Subdomain Routing via Rewrites
10 const hostname = request.headers.get('host') ?? '';
11 const subdomain = hostname.split('.')[0];
12
13 if (subdomain && subdomain !== 'www' && subdomain !== 'localhost') {
14 // Rewrite internal URL to tenant folder without changing browser URL
15 return NextResponse.rewrite(
16 new URL(`/tenants/${subdomain}${pathname}`, request.url)
17 );
18 }
19
20 // 2. Node.js native authentication check (no Edge sandbox limits!)
21 const sessionToken = request.cookies.get('auth_token')?.value;
22 if (pathname.startsWith('/admin') && sessionToken) {
23 const isValid = await verifySessionWithFullDb(sessionToken);
24 if (!isValid) {
25 return NextResponse.redirect(new URL('/login', request.url));
26 }
27 }
28
29 return NextResponse.next();
30}

Architectural Explanation

While the Edge Runtime promised low latency, developers faced major challenges: popular packages failed to compile due to missing Node.js APIs, database drivers could not establish TCP connections without HTTP proxies, and bundle size was capped (typically 1MB). In Next.js 16, proxy.ts runs on the Node.js runtime. This enables developers to run standard Node.js packages, direct ORM queries, and microservice authentications right in the network routing phase while keeping serverless latency low.

Common Mistakes & Anti-Patterns

Confusing `NextResponse.rewrite()` (internal server routing) with `NextResponse.redirect()` (browser URL change).

Running heavy synchronous computations in network proxies that delay every HTTP request across the entire application.

Believing proxy.ts replaces the need for data authorization in Server Components.

Interview Follow-Up Traps

The interviewer asks: "Is middleware.ts completely gone in Next.js 16?" Next.js maintains backward compatibility for existing middleware.ts files, but proxy.ts is the forward-looking standard that eliminates Edge runtime limitations.

Frequently Asked Questions

Does proxy.ts run before or after route handlers and pages?

It runs before all Route Handlers, pages, and static files, intercepting the request at the network entry point.

No. You can mutate request and response headers, cookies, and status codes, but not the streaming body.

Export a `config = { matcher: [...] }` object specifying glob patterns.