Leaf and Kettle prints short links on its tins and flyers, like leafandkettle.example/go/mint. Write the GET handler in app/go/[code]/route.ts, which sends each visitor where the link points.
Find the link with findLink(code) from @/lib/links. Codes are saved in lowercase, but people type them from print, so MINT and Mint are the same code as mint.
Answer with NextResponse.redirect(url, status): 308 for a link marked permanent, 307 for the others. Its Location must be a full URL. A target that starts with / is a page of this site, so resolve it against the request's URL (http://localhost:3000/teas/mint in the tests); a target that's already a full URL, like https://brewing.example/guides/oolong, stays as it is.
Search params on the short link are added to the target's own, so a flyer's /go/spring?ref=flyer lands on /sale?season=spring&ref=flyer.
Count each visit to a real link with countClick(code), using the lowercase code. For a code that doesn't exist, call notFound() from next/navigation: Next.js answers 404 with an empty body, and nothing is counted.
GET /go/MINT → 308 Location: http://localhost:3000/teas/mint
GET /go/spring?ref=flyer → 307 Location: http://localhost:3000/sale?season=spring&ref=flyer
GET /go/coffee → 404
lib/links.ts, read-only. Import it as @/lib/links.
export type ShortLink = { code: string; to: string; permanent: boolean };
/** Every short link, by its lowercase code. A target that starts with / is a page of this site. */
export const links: ShortLink[] = [
{ code: "mint", to: "/teas/mint", permanent: true },
{ code: "spring", to: "/sale?season=spring", permanent: false },
{ code: "guide", to: "https://brewing.example/guides/oolong", permanent: true },
];
/** How many times each code has been followed. */
export const clicks: Record<string, number> = {};
/** The link with this code (lowercase), or undefined when there's none. */
export async function findLink(code: string): Promise<ShortLink | undefined> {
return links.find((link) => link.code === code);
}
/** Counts one visit to the link with this code. */
export async function countClick(code: string) {
clicks[code] = (clicks[code] ?? 0) + 1;
}
const response = await GET(request("/go/mint"), { params: params({ code: "mint" }) });
expect(response.status).toBe(308);
expect(response.headers.get("location")).toBe("http://localhost:3000/teas/mint");
expect(clicks).toEqual({ mint: 1 });
const sale = await GET(request("/go/spring"), { params: params({ code: "spring" }) });
expect(sale.status).toBe(307);
expect(sale.headers.get("location")).toBe("http://localhost:3000/sale?season=spring");
const guide = await GET(request("/go/guide"), { params: params({ code: "guide" }) });
expect(guide.status).toBe(308);
expect(guide.headers.get("location")).toBe("https://brewing.example/guides/oolong");
for (const code of ["MINT", "Mint", "mint"]) {
const response = await GET(request("/go/" + code), { params: params({ code }) });
expect(response.headers.get("location")).toBe("http://localhost:3000/teas/mint");
}
expect(clicks).toEqual({ mint: 3 });
const response = await GET(request("/go/spring?ref=flyer&utm_source=tin"), { params: params({ code: "spring" }) });
expect(response.headers.get("location")).toBe("http://localhost:3000/sale?season=spring&ref=flyer&utm_source=tin");
const guide = await GET(request("/go/guide?ref=tin"), { params: params({ code: "guide" }) });
expect(guide.headers.get("location")).toBe("https://brewing.example/guides/oolong?ref=tin");
const response = await GET(request("/go/coffee"), { params: params({ code: "coffee" }) });
expect(response.status).toBe(404);
expect(response.headers.get("location")).toBe(null);
expect(await response.text()).toBe("");
expect(clicks).toEqual({});
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Short links that redirect. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.