Question presented to candidate: "What is middleware in Next.js, what is it good for and what should it not do? And what changed with Next.js 16?"
What a strong answer should cover:
- Middleware runs code before a request reaches a route, for paths chosen with a
matcher. - It can redirect, rewrite to another path, set request and response headers or cookies, or return a response directly.
- Typical uses: auth redirects based on a cookie, locale or A/B routing, bot filtering, adding security headers.
- It should stay fast and light: no heavy data fetching or full authorization logic; verify permissions again close to the data.
- In Next.js 16 the file is
proxy.tswith aproxyfunction, running on Node.js by default;middleware.tsis deprecated and still runs on the Edge runtime.
Clarifying questions expected:
- "Is this for every request or only some paths?" β decides the matcher.
- "Which Next.js version?" β decides the file name and the runtime.
Code / implementation expected: Yes β a proxy.ts with a matcher, a redirect, a rewrite and a header.