Leaf and Kettle's collections put their filters in the path, where search engines and caches treat each list as a page of its own: /collections is every tea, /collections/green the green teas, /collections/green/under-5/in-stock the green teas under $5 that are in stock. Write app/collections/[[...filters]]/page.tsx. The double brackets make it an optional catch-all: it also matches the bare /collections, where params is a promise of { filters: undefined }; everywhere else filters is the array of segments.
There are three kinds of filter, and a URL may use each kind once at most. A type from TYPES (green, black, oolong, herbal) keeps the teas of that type. A price from PRICE_LIMITS (under-5, under-10) keeps the teas that cost less than its limit. in-stock keeps the teas whose stock is more than 0. Both lists and getTeas() come from @/lib/teas.
A segment that isn't a filter, or a second filter of a kind the URL already has (/collections/green/black), makes a URL the shop doesn't have: call notFound().
The same filters in another order would put one list at several URLs. The one right order is type, then price, then in-stock. When the filters are all fine but out of that order, like /collections/in-stock/green, call permanentRedirect with the URL in the right order, /collections/green/in-stock.
Show an <h1>: All teas without a type, or the type with a capital and teas, like Green teas. For each filter in use, add a link (with next/link) that removes it: Remove green, to the same collection without it, in the right order; removing the last one leads to /collections. Then list the matching teas' names, each an <li> in a <ul> (the page's only list), or show No teas match these filters. when none do.
/collections/green/under-5 → Green teas: Genmaicha, Jasmine
Remove green → /collections/under-5
/collections/under-5/green → 308 to /collections/green/under-5
/collections/green/black → 404
lib/teas.ts, read-only. Import it as @/lib/teas.
export type Tea = { slug: string; name: string; type: string; price: number; stock: number };
/** The tea types, as they appear in a collection's URL. */
export const TYPES = ["green", "black", "oolong", "herbal"];
/** The price filters, as they appear in a URL, and the price a tea must be under to match. */
export const PRICE_LIMITS: Record<string, number> = { "under-5": 5, "under-10": 10 };
const teas: Tea[] = [
{ slug: "sencha", name: "Sencha", type: "green", price: 6, stock: 12 },
{ slug: "genmaicha", name: "Genmaicha", type: "green", price: 4, stock: 0 },
{ slug: "jasmine", name: "Jasmine", type: "green", price: 4.5, stock: 7 },
{ slug: "assam", name: "Assam", type: "black", price: 4, stock: 20 },
{ slug: "earl-grey", name: "Earl Grey", type: "black", price: 5.5, stock: 0 },
{ slug: "tie-guan-yin", name: "Tie Guan Yin", type: "oolong", price: 12, stock: 3 },
{ slug: "mint", name: "Mint", type: "herbal", price: 3, stock: 30 },
{ slug: "chamomile", name: "Chamomile", type: "herbal", price: 3.5, stock: 0 },
];
/** Every tea, in the shop's order. */
export async function getTeas(): Promise<Tea[]> {
return teas;
}
const listed = () => screen.queryAllByRole("listitem").map((item) => item.textContent);
await renderServer(<Page params={params({ filters: undefined })} />);
expect(screen.getByRole("heading", { level: 1, name: "All teas" })).toBeInTheDocument();
expect(listed()).toEqual(["Sencha", "Genmaicha", "Jasmine", "Assam", "Earl Grey", "Tie Guan Yin", "Mint", "Chamomile"]);
expect(screen.queryByRole("link", { name: /^Remove/ })).not.toBeInTheDocument();
const listed = () => screen.queryAllByRole("listitem").map((item) => item.textContent);
await renderServer(<Page params={params({ filters: ["green"] })} />);
expect(screen.getByRole("heading", { level: 1, name: "Green teas" })).toBeInTheDocument();
expect(listed()).toEqual(["Sencha", "Genmaicha", "Jasmine"]);
cleanup();
await renderServer(<Page params={params({ filters: ["green", "under-5", "in-stock"] })} />);
expect(listed()).toEqual(["Jasmine"]);
cleanup();
await renderServer(<Page params={params({ filters: ["under-10", "in-stock"] })} />);
expect(screen.getByRole("heading", { level: 1, name: "All teas" })).toBeInTheDocument();
expect(listed()).toEqual(["Sencha", "Jasmine", "Assam", "Mint"]);
const listed = () => screen.queryAllByRole("listitem").map((item) => item.textContent);
await renderServer(<Page params={params({ filters: ["oolong", "under-5"] })} />);
expect(screen.getByRole("heading", { level: 1, name: "Oolong teas" })).toBeInTheDocument();
expect(screen.getByText("No teas match these filters.")).toBeInTheDocument();
expect(listed()).toEqual([]);
expect(await redirectOf(() => renderServer(<Page params={params({ filters: ["in-stock", "green"] })} />))).toEqual({ url: "/collections/green/in-stock", status: 308, type: "replace" });
expect((await redirectOf(() => renderServer(<Page params={params({ filters: ["in-stock", "under-5", "black"] })} />))).url).toBe("/collections/black/under-5/in-stock");
expect((await redirectOf(() => renderServer(<Page params={params({ filters: ["under-10", "herbal"] })} />))).url).toBe("/collections/herbal/under-10");
const bad = [["coffee"], ["green", "teapots"], ["Green"], ["green", "black"], ["under-5", "under-10"], ["in-stock", "in-stock"], ["in-stock", "green", "black"]];
for (const filters of bad) {
expect(await accessErrorOf(() => renderServer(<Page params={params({ filters })} />))).toBe(404);
}
await renderServer(<Page params={params({ filters: ["green", "under-5", "in-stock"] })} />);
expect(screen.getByRole("link", { name: "Remove green" })).toHaveAttribute("href", "/collections/under-5/in-stock");
expect(screen.getByRole("link", { name: "Remove under-5" })).toHaveAttribute("href", "/collections/green/in-stock");
expect(screen.getByRole("link", { name: "Remove in-stock" })).toHaveAttribute("href", "/collections/green/under-5");
cleanup();
await renderServer(<Page params={params({ filters: ["herbal"] })} />);
expect(screen.getByRole("link", { name: "Remove herbal" })).toHaveAttribute("href", "/collections");
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 Filters in the path, one URL for each list. 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.