Skip to the code

Short links that redirect

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

The app's other files

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;
}

What the tests check

  1. a permanent link answers 308 with the full URL in Location
    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 });
    
  2. a temporary link answers 307, and an outside link keeps its own address
    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");
    
  3. matches a code whatever its case, and counts it in lowercase
    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 });
    
  4. adds the short link's search params to the target's
    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");
    
  5. answers an empty 404 for a code that doesn't exist, and counts nothing
    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({});
    

Learn more

app/go/[code]/route.tsSaved in this browser

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).