Middleware
Put request middleware in middleware.ts at the project root. Scope it with a config.matcher export and it runs on the Edge runtime.
The established pattern is a middleware.ts file at the project or src root that exports a middleware function. You limit which paths it runs on with an exported config.matcher. It runs on the Edge runtime by default. Next.js 16 deprecated this in favor of proxy.ts on the Node.js runtime.