Keep English pages at /teas and French ones at /fr/teas, with every page under app/[lang]
Leaf and Kettle's pages all live under app/[lang], so /en/teas and /fr/teas are the tea list in English and in French. Most customers read English, and the shop wants its English pages back at short addresses without a language, like /teas and /teas/mint; French pages keep /fr in front. @/lib/i18n has the locales, ["en", "fr"], and the defaultLocale, "en". The current proxy.ts redirects every address without a language to /en. Change it.
A path without a language shows the English page at the same address: rewrite /teas/mint?ref=mail to /en/teas/mint?ref=mail, and / to /en. A path has a language only when its whole first segment is one, so /fresh-picks is an English page.
A path that starts with /fr goes on to its page. One that starts with /en would show the same page as the address without it, so it gets a permanent 308 redirect there, keeping the rest of the path and the query: /en/teas?ref=mail goes to /teas?ref=mail, and /en to /.
Keep the matcher, which skips the API, Next.js's files, and paths with a dot. The tests read a rewrite's target from the response's x-middleware-rewrite header, where NextResponse.rewrite puts it.
proxy.tsimport { NextResponse, type NextRequest } from "next/server";
import { locales } from "@/lib/i18n";
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const hasLocale = locales.some((locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`));
if (hasLocale) return;
// Every English page lives at /en/..., so send the visitor there.
request.nextUrl.pathname = `/en${pathname}`;
return NextResponse.redirect(request.nextUrl);
}
export const config = {
matcher: ["/((?!api(?:/|$)|_next/|.*\\..*).*)"],
};
lib/i18n.ts (read-only)/** The languages the shop is written in. Every page lives under app/[lang], like app/[lang]/teas/page.tsx. */
export const locales = ["en", "fr"];
/** The language of the addresses without one. */
export const defaultLocale = "en";
- shows the English page at an address without a language
- French addresses go on to their pages
- an /en address moves for good to the same page without it
- a path that only starts with a language's letters is an English page
- still runs for pages only
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…