Files
codegraph/__tests__
Colby MchenryandClaude Opus 5.5 446d08c272 feat(nextjs): a route links to the proxy that runs before it (#3119)
Next wires proxy.ts (middleware.ts before Next 16) by file name and
config.matcher, never by an import, so to the graph it was a function
nobody calls. `codegraph impact middleware` came back with just the
function, for the one file that gates every authenticated route.

A new synthesis pass links each route the matcher accepts to what runs
in the proxy: a `calls` edge, synthesizedBy next-proxy, carrying the
matcher entry and where it is written. It looks for the proxy only where
next build loads it, beside pages or app, app by app, and takes a lone
proxy.ts only when the app doesn't pin Next below 16.

Three decisions differ from PR #2265, each from a real-repo result:
- The edge is `calls`, not `decorates`. `callers` and explore's Flow ride
  calls, so with decorates the proxy has no callers and every flow stops
  at the route. routeRoots and the Screens walk leave the edge out, as
  they do for other edges that don't say where a route's code starts.
- Regular-expression matchers are read. In a sample of 157 proxies from
  GitHub code search, about two thirds use a negative lookahead such as
  /((?!api|_next/static).*), which a reader of literal paths links
  nothing from. compileMatcher builds its expression from the parts it
  recognizes and never from the entry's text, since new RegExp() on a
  pattern in an indexed repository could hang the index.
- A wrapped default export links to the file. About a third of that
  sample, which two search queries looked for, is `export default
  clerkMiddleware(...)` or similar, which has
  no node, and the file node is where the wrapper's calls are recorded.

Validation:
- nextjs-proxy.test.ts, 56 tests; 8 end-to-end ones fail without the
  pass, and the Screens one fails without the walk change.
- Against path-to-regexp 6.3.0, Next's own matcher library: 344 entries
  from the 157 proxies, 339 read, all agreeing on 27 addresses; 5 not
  read, which link nothing. Route by route on the four apps below, 387
  of 387 agree.
- A/B on main cd04b21a: only `+calls next-proxy`. taxonomy 298a8857 +6 of
  23 routes, nextjs/saas-starter 6e33e58b +8 of 12, vercel/ai-chatbot
  c2f8235e +19 of 19, dub 6b463b4b's apps/web +80 of 333. No node, ref or
  other edge changes. Controls flask 49943b5a and gin 0f09c3a9 are
  byte-identical. Screens is byte-identical on all four and route roots
  are unchanged.
- Flow connects: saas-starter's Header -> [next link] / -> [Next proxy]
  middleware -> verifyToken.
- Index CPU, median of 3 interleaved, with a suite running: ai-chatbot
  -2.9%, taxonomy +3.1%, excalidraw +2.8%, all within noise.

Left out: `has` and `missing` on a matcher entry, which only narrow which
requests run the proxy; a matcher Next can't read statically, which links
nothing; dev server and basePath rewriting of addresses.

Reimplements PR #2265 by @RIGOandre.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 18:55:24 -05:00
..