Split the home page so the tea of the week is prerendered and only its stock is rendered for each request
Leaf and Kettle's home page, app/page.tsx, shows the tea of the week and how many tins of it are left. The tea comes from the shop's CMS through fetchTeaOfTheWeek() in @/lib/cms: it changes once a week and it's the same for everyone. The stock comes from getStock(slug) in @/lib/warehouse, and it changes with every order. Cache Components is on. The page awaits both at the top, and neither can finish while next build prerenders, so the build stops. Fix it so the static shell holds everything but the stock.
Export getTeaOfTheWeek(), a cached function: it starts with "use cache", calls cacheLife("days"), and returns what fetchTeaOfTheWeek() returns. The page awaits it and shows <h1>Leaf and Kettle</h1>, <h2>Tea of the week: Oolong tea</h2>, and the tea's note in a paragraph.
Below them, show the stock from a component of its own inside <Suspense>, with <p>Checking the stock…</p> as its fallback: Only 4 tins left., Only 1 tin left., or Sold out this week. when there are none. The stock is read for every request: don't cache it, or the page.
app/page.tsximport { Suspense } from "react";
import { cacheLife } from "next/cache";
import { fetchTeaOfTheWeek } from "@/lib/cms";
import { getStock } from "@/lib/warehouse";
export default async function Home() {
const tea = await fetchTeaOfTheWeek();
const tins = await getStock(tea.slug);
return (
<main>
<h1>Leaf and Kettle</h1>
<h2>Tea of the week: {tea.name}</h2>
<p>{tea.note}</p>
<p>Only {tins} tins left.</p>
</main>
);
}
lib/cms.ts (read-only)export type Feature = { slug: string; name: string; note: string };
/** This week's tea, from the shop's CMS: a network request each time it's called. */
export async function fetchTeaOfTheWeek(): Promise<Feature> {
await new Promise((resolve) => setTimeout(resolve, 20));
return { slug: "oolong", name: "Oolong tea", note: "Rolled by hand and roasted twice." };
}
lib/warehouse.ts (read-only)/** Tins of each tea on the shelf right now: it changes with every order. */
export const shelf: Record<string, number> = { green: 12, mint: 0, oolong: 4 };
/**
* While `warehouse.paused` is true, getStock never answers, the way an uncached read never
* finishes while next build prerenders. The tests use it to build the static shell.
*/
export const warehouse = { paused: false };
/** The tins of a tea on the shelf. */
export async function getStock(slug: string): Promise<number> {
if (warehouse.paused) return new Promise<number>(() => {});
await new Promise((resolve) => setTimeout(resolve, 10));
return shelf[slug] ?? 0;
}
- shows the tea of the week and how many tins are left
- says when one tin is left, or the tea is sold out
- prerenders everything but the stock into the static shell
- caches the tea of the week for days
- reads the stock again for every request, and doesn't cache the page
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…