- Next.js
- Advanced Routing & Interception
- Network Routing: Edge Middleware to Next.js 16 proxy.ts
Network Routing: Edge Middleware to Next.js 16 proxy.ts
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
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';56export async function proxy(request: NextRequest) {7 const { pathname } = request.nextUrl;89 // 1. Multi-Tenant Subdomain Routing via Rewrites10 const hostname = request.headers.get('host') ?? '';11 const subdomain = hostname.split('.')[0];1213 if (subdomain && subdomain !== 'www' && subdomain !== 'localhost') {14 // Rewrite internal URL to tenant folder without changing browser URL15 return NextResponse.rewrite(16 new URL(`/tenants/${subdomain}${pathname}`, request.url)17 );18 }1920 // 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 }2829 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
Can you modify the response body in proxy.ts?
No. You can mutate request and response headers, cookies, and status codes, but not the streaming body.
How do you filter which routes trigger proxy.ts?
Export a `config = { matcher: [...] }` object specifying glob patterns.
