nextjs-16-proxy
Next.js 16 Proxy Convention
Next.js 16 renamed middleware to proxy to better reflect its network boundary purpose.
Key Changes from Middleware
| Old (Next.js 15) | New (Next.js 16+) |
|---|---|
middleware.ts |
proxy.ts |
export function middleware() |
export function proxy() |
| Same location | Same location |
File Location
Place proxy.ts at the same level as app or pages:
src/
├── proxy.ts ← Correct location
├── app/
│ └── ...
NOT inside app/:
src/
├── app/
│ ├── proxy.ts ← Wrong location
│ └── ...
Basic Template
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 [teamSlug] at the root, it can catch paths before rewrites in next.config.ts are applied.
Problem:
- Request:
/uploads/image.png - Dynamic route
[teamSlug]catches it asteamSlug = "uploads" - Results in 404
Solution:
Use proxy.ts instead of next.config.ts rewrites. Proxy runs before routing.
Migration Command
npx @next/codemod@canary middleware-to-proxy .
This automatically:
- Renames
middleware.ts→proxy.ts - Updates function name
middleware→proxy
More from blink-new/claude
saas-sidebar
Build a modern, collapsible sidebar for SaaS dashboards following the ChatGPT/Notion design pattern
75seo-article-writing
A comprehensive workflow for creating high-ranking SEO blog articles with keyword research, competitive analysis, AI-generated unique images, and optimized content structure
69pg-boss
Implement reliable PostgreSQL-based job queues with PG Boss. Use when implementing background jobs, scheduled tasks, cron-like functionality, task rollover, or email notifications in Node.js/TypeScript projects.
57kanban-dnd
Build world-class kanban board drag-and-drop with @dnd-kit. Linear-quality UX with proper collision detection, smooth animations, and visual feedback
57datafast
Accelerate adoption of DataFast analytics across any stack by codifying the installation, attribution, event, proxy, and API patterns that drive reliable conversion intelligence
54wysiwyg-editor
Build production-grade WYSIWYG editors using Tiptap v3 with proper markdown-style formatting, instant rendering, and bullet/numbered list support
51