/ 3 min read
Moving routing out of the proxy: a function call on every page view
My portfolio served cached pages, yet a first visit could wait more than three seconds for the first byte. The cause was a small proxy file that ran a server function before every page. How I moved its work into routing rules.
In this article
The home page of this site shows a small readout of its own speed, measured in the visitor's browser. One day it showed me a time to first byte of more than three seconds. That is strange for a site whose pages are prerendered and served from a CDN: the HTML already exists, so the first byte should arrive in well under a second.
What the proxy was doing
The site is English only and lives at the root (/, /work/api-platform), while the route tree sits under /en. A small proxy.ts (Next.js 16 renamed middleware to proxy) did four jobs: rewrite / and every other path to its /en twin, redirect /en/... back to the clean address, redirect a retired /fa section, and send unknown project or service slugs to a path no route matches, so they answer a real 404.
None of that needs a server. But in Next.js 16 the proxy runs on the Node.js runtime by default, and the runtime option is not allowed in the proxy file. On Vercel that means a function invocation in front of every page view. A cached page still had to wait for that function, and on a cold start, from far away, the wait is long.
Rules instead of code
Rewrites and redirects in next.config.ts are applied by the platform's routing layer, without starting a function. So the locale handling moved there, and the proxy kept only the one job that needs a request: the optimistic session check for the admin area.
async redirects() {
return [
{ source: '/en', destination: '/', permanent: true },
// Link preview images keep their internal /en paths.
{ source: '/en/:path((?!.*(?:opengraph|twitter)-image).*)', destination: '/:path', permanent: true },
]
},
async rewrites() {
return {
afterFiles: [
...unknownSlugs,
{ source: '/', destination: '/en' },
{ source: '/:path((?!en(?:/|$)|admin(?:/|$)|api(?:/|$)|_next(?:/|$)).*)', destination: '/en/:path' },
],
}
},export const config = { matcher: ['/admin', '/admin/:path*'] }The 404 that turned into a 200
The first version of the change broke one thing quietly: an unknown address such as /work/does-not-exist started answering 200. The page itself calls notFound(), but with partial prerendering the static shell is already on its way, with status 200, before the page code runs. The proxy used to prevent this by rewriting unknown slugs to a missing path.
The usual fix, dynamicParams = false, is not available: with Cache Components enabled that export fails the build. Instead, next.config.ts builds one rewrite per collection from the same content files the pages read, with a negative lookahead for the known slugs:
const unknownSlugs = Object.entries(knownSlugs).map(([collection, slugs]) => ({
// A dot is a flight file (.rsc, .segments), not a page slug.
source: `/${collection}/:slug((?!(?:${slugs.map(escapeRegex).join('|')})(?=$|[/?#]))[^/.]+)`,
destination: '/en/_missing',
}))How I checked it
- A table of curl requests against a production build: real pages answer 200, unknown slugs 404, /en/... redirects with 308, files and preview images are served directly, /admin redirects to the login.
- The end to end tests, including one that every page in the sitemap loads without errors and one that unknown addresses answer 404.
- The real number comes from real visitors: the readout on the home page and field data from Speed Insights, compared before and after the deploy.
What I took from it
A proxy is code on the request path, and code on the request path has a cost even when it only rewrites a URL. Before putting logic there, I now ask whether a rule can say the same thing. Most redirects and rewrites can, and then the cached page is truly the first thing a visitor waits for.
Next article ↗
Fast tables, honest numbers: keeping data heavy React screens responsive