Collections at Leaf and Kettle, like Japanese greens, are lists of teas staff put together, and getCollection(slug) in lib/collections.ts is the cached read behind each collection page. Staff tools revalidate tea:<slug> when they save a tea, retiring it or bringing it back included, and collection:<slug> when they save a collection. Write getCollection so that both refresh the page.
Start its body with "use cache". Read the collection with db.collection(slug) and its teas with db.teas(slugs) from @/lib/db. Return { slug, title, teas }, where teas is { slug, name } for each tea in the collection's order, each once (staff sometimes add a tea twice), leaving out retired teas. Cache it with cacheLife("max").
Tag it collection:<slug>, and tea:<slug> for every tea in the collection, each once. Tag the retired teas too: they aren't shown, but bringing one back revalidates its tag, and the collection must then show it again.
Mind Next.js's limit: one cacheTag call keeps at most 128 tags and drops the rest with only a warning, and Every blend has 240 teas. Pass no more than 128 tags to any single call. (Each tag here is far below the other limit, 256 characters.)
A collection that doesn't exist returns null, tagged collection:<slug> so creating it refreshes the page, and cached for minutes.
getCollection("japanese-greens") // sencha, gyokuro, genmaicha (retired), sencha, hojicha
→ { slug: "japanese-greens", title: "Japanese greens", teas: [Sencha, Gyokuro, Hojicha] }
tags: collection:japanese-greens, tea:sencha, tea:gyokuro, tea:genmaicha, tea:hojicha
cacheLife("max")
getCollection("every-blend") // 241 tags → cacheTag(…128 tags…), cacheTag(…113 tags…)
lib/db.ts, read-only. Import it as @/lib/db.
export type Tea = { slug: string; name: string; retired: boolean };
export type CollectionRow = { slug: string; title: string; teas: string[] };
const blends: Tea[] = Array.from({ length: 240 }, (_, index) => ({ slug: `blend-${index + 1}`, name: `Blend no. ${index + 1}`, retired: false }));
/** Every tea: a stand-in for the shop's database. Retired teas are kept, but not sold. */
export const teaRows: Tea[] = [
{ slug: "sencha", name: "Sencha", retired: false },
{ slug: "gyokuro", name: "Gyokuro", retired: false },
{ slug: "genmaicha", name: "Genmaicha", retired: true },
{ slug: "hojicha", name: "Hojicha", retired: false },
...blends,
];
/** Collections, each with its teas in the order staff chose. Staff sometimes add a tea twice. */
export const collectionRows: CollectionRow[] = [
{ slug: "japanese-greens", title: "Japanese greens", teas: ["sencha", "gyokuro", "genmaicha", "sencha", "hojicha"] },
{ slug: "every-blend", title: "Every blend", teas: blends.map((tea) => tea.slug) },
];
export const db = {
/** The collection with this slug, or null. */
async collection(slug: string): Promise<CollectionRow | null> {
const row = collectionRows.find((entry) => entry.slug === slug);
return row ? { ...row, teas: [...row.teas] } : null;
},
/** The teas with these slugs, in the same order, skipping slugs that aren't teas. */
async teas(slugs: string[]): Promise<Tea[]> {
return slugs.flatMap((slug) => {
const tea = teaRows.find((row) => row.slug === slug);
return tea ? [{ ...tea }] : [];
});
},
};
expect(await getCollection("japanese-greens")).toEqual({
slug: "japanese-greens",
title: "Japanese greens",
teas: [
{ slug: "sencha", name: "Sencha" },
{ slug: "gyokuro", name: "Gyokuro" },
{ slug: "hojicha", name: "Hojicha" },
],
});
expect(cache.lives).toEqual(["max"]);
await getCollection("japanese-greens");
expect([...cache.tags].sort()).toEqual(["collection:japanese-greens", "tea:genmaicha", "tea:gyokuro", "tea:hojicha", "tea:sencha"]);
const tagCalls = () => cache.calls.filter((call) => call.name === "cacheTag").map((call) => call.args);
const collection = await getCollection("every-blend");
expect(collection.teas).toHaveLength(240);
for (const tags of tagCalls()) {
if (tags.length > 128) throw new Error("One cacheTag call got " + tags.length + " tags: Next.js keeps the first 128 and drops the rest");
}
const all = tagCalls().flat();
expect(new Set(all).size).toBe(241);
expect(all).toHaveLength(241);
expect(all).toContain("collection:every-blend");
expect(all).toContain("tea:blend-240");
collectionRows[0].teas.push("blend-7");
const collection = await getCollection("japanese-greens");
expect(collection.teas.map((tea) => tea.slug)).toEqual(["sencha", "gyokuro", "hojicha", "blend-7"]);
expect(cache.tags).toContain("tea:blend-7");
expect(await getCollection("autumn-reds")).toBe(null);
expect(cache.tags).toEqual(["collection:autumn-reds"]);
expect(cache.lives).toEqual(["minutes"]);
const expectUseCache = (fn, name) => {
if (!/\)\s*(=>\s*)?\{\s*(["'])use cache\2/.test(String(fn))) throw new Error(name + ' should start its body with "use cache", so Next.js caches what it returns');
};
expectUseCache(getCollection, "getCollection");
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 Tag a big collection without losing tags. 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.