next.config.ts redirects /shop and rewrites /team to /staff, and proxy.ts guards /shop and /staff. What gets through?
Leaf and Kettle's next.config.ts sends /shop to /teas for good, and shows the staff page at /team too. A teammate's proxy.ts also handles /shop, sending it to the spring sale, and sends anyone without a staff-session cookie from /staff to the sign-in page. It logs every path it runs for. A program without cookies asks for four paths. What does each one get, and what does proxy.ts log?
next.config.tsimport type { NextConfig } from "next";
const nextConfig: NextConfig = {
cacheComponents: true,
partialPrefetching: true,
async redirects() {
return [{ source: "/shop", destination: "/teas", permanent: true }];
},
async rewrites() {
return [{ source: "/team", destination: "/staff" }];
},
};
export default nextConfig;
proxy.tsimport { NextResponse, type NextRequest } from "next/server";
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
console.log(`proxy.ts saw ${pathname}`);
if (pathname === "/shop") {
return NextResponse.redirect(new URL("/sale", request.url));
}
if (pathname.startsWith("/staff") && !request.cookies.has("staff-session")) {
return NextResponse.redirect(new URL("/sign-in", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/shop", "/teas", "/staff/:path*"],
};
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/sale/page.tsxexport default function SalePage() {
return (
<main>
<h1>Spring sale</h1>
<p>Every tea is 20% off.</p>
</main>
);
}
app/staff/page.tsxexport default function StaffPage() {
return (
<main>
<h1>Staff room</h1>
<p>Mint tea: 12 boxes in the back.</p>
</main>
);
}
app/sign-in/page.tsxexport default function SignInPage() {
return (
<main>
<h1>Staff sign-in</h1>
</main>
);
}
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 without cookies asks for
/shop,/team,/staff, and/teas, in that order, without following redirects.
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…