nextjs-16-proxy
Original:🇺🇸 English
Translated
Next.js 16 proxy convention (replacing middleware). Use when creating middleware, handling URL rewrites, or when the user mentions proxy.ts, middleware.ts, or route interception in Next.js 16+.
7installs
Sourceblink-new/claude
Added on
NPX Install
npx skill4agent add blink-new/claude nextjs-16-proxyTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Next.js 16 Proxy Convention
Next.js 16 renamed to to better reflect its network boundary purpose.
middlewareproxyKey Changes from Middleware
| Old (Next.js 15) | New (Next.js 16+) |
|---|---|
| |
| |
| Same location | Same location |
File Location
Place at the same level as or :
proxy.tsapppagessrc/
├── proxy.ts ← Correct location
├── app/
│ └── ...NOT inside :
app/src/
├── app/
│ ├── proxy.ts ← Wrong location
│ └── ...Basic Template
typescript
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// Example: rewrite /uploads/* to /api/uploads/*
if (pathname.startsWith("/uploads/")) {
const newUrl = request.nextUrl.clone();
newUrl.pathname = pathname.replace("/uploads/", "/api/uploads/");
return NextResponse.rewrite(newUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ["/uploads/:path*"],
};Common Issue: Dynamic Routes Catching Paths
When you have a dynamic route like at the root, it can catch paths before rewrites in are applied.
[teamSlug]next.config.tsProblem:
- Request:
/uploads/image.png - Dynamic route catches it as
[teamSlug]teamSlug = "uploads" - Results in 404
Solution:
Use instead of rewrites. Proxy runs before routing.
proxy.tsnext.config.tsMigration Command
bash
npx @next/codemod@canary middleware-to-proxy .This automatically:
- Renames →
middleware.tsproxy.ts - Updates function name →
middlewareproxy