proxy.ts redirects /shop, rewrites /menu, and answers /api/stock itself. What does each request get?
Leaf and Kettle's proxy.ts handles three addresses: /shop moved to /teas, /menu is another name for the tea list, and the stock API is closed while the stock is counted. The app has only two routes, app/teas/page.tsx and app/api/stock/route.ts. What does each request get, and what's in Chrome's address bar after each visit?
proxy.tsimport { NextResponse, type NextRequest } from "next/server";
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
if (pathname === "/shop") {
return NextResponse.redirect(new URL("/teas", request.url));
}
if (pathname === "/menu") {
return NextResponse.rewrite(new URL("/teas", request.url));
}
if (pathname === "/api/stock") {
return Response.json({ error: "We're counting the stock. Back in ten minutes." }, { status: 503 });
}
return NextResponse.next();
}
export const config = {
matcher: ["/shop", "/menu", "/api/stock"],
};
app/layout.tsxexport const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
app/teas/page.tsxexport default function TeasPage() {
return (
<main>
<h1>Our teas</h1>
<ul>
<li>Green tea</li>
<li>Mint tea</li>
<li>Oolong tea</li>
</ul>
</main>
);
}
app/api/stock/route.tsexport function GET() {
return Response.json({ green: 12, mint: 0, oolong: 4 });
}
It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build and run with next start. What happens:
- You run
next buildandnext start. - A program asks for
/shop?ref=mail,/menu, and/api/stock, without following redirects. - You open
/shop?ref=mailin Chrome. - Then you open
/menu.
The answer
Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.
Checking your sign-in…