Proxy file and matcher
What you need to know
Proxy is the Next.js request interception convention. It can select, redirect or rewrite a request before the destination route renders. A matcher limits which requests invoke it.
By the end, you can: choose request interception scope; predict matched paths.
Intercept a narrow route family in proxy.ts
import { NextResponse, type NextRequest } from 'next/server';export function proxy(request: NextRequest) { const response = NextResponse.next(); response.headers.set('X-Catalog-Route', 'yes'); return response;}export const config = { matcher: ['/catalog/:path*'] };Expected behavior: The catalog response should include the diagnostic header. Unrelated paths should not invoke this rule. This is staged framework code.
How it works
Place proxy.ts beside app or pages, or beside them inside src when that structure is used. Export one function named proxy or a default function; the optional config export contains statically analyzable matcher patterns. A narrow matcher is preferable when only a few routes need handling. The example uses /catalog/:path* to match the catalog and its descendants. It does not cover /catalogue or unrelated assets. A NextResponse.next() continues processing, whereas a redirect asks the browser to navigate elsewhere and a rewrite selects another internal destination. A response header is sent to the visitor; a request header override is forwarded to the destination and should not be confused with a trusted identity. For request overrides, copy only required fields and never forward arbitrary sensitive headers to another origin. In the documented ordering, config headers and redirects precede Proxy, followed by beforeFiles rewrites and subsequent route matching. Actual requests can have normalization, base-path or data-route behavior beyond this simple example.
Predict the ordinary path matches
/catalog -> match/catalog/fern-atlas -> match/catalogue -> no match/_next/static/chunk.js -> no matchExpected behavior: The prefix must end at a path segment boundary; a broad startsWith test would incorrectly match catalogue.
Common mistake
Do not rely on a global variable shared by Proxy and a route, or use a matcher as the authorization policy. Any data access or mutation must independently check the caller and resource.
Practice and verification
Predict matches for /catalog, /catalogue, /catalog/ and /api/catalog. Run the independent path model, then stage a real matcher test for direct visits, client navigation and assets in the pinned app.
Scope: Proposed Next.js 16.x, React 19, Node runtime. Root-mounted Pages and mixed-router fixtures with Cache Components disabled; the basePath example is separate. Framework examples are staged, not executed. Exact patched dependencies and platform contracts pending.