Leaf and Kettle's home page, app/page.tsx, shows three things from the database: this week's featured teas, the newest teas, and the latest reviews. It works, but it's slow. Each await waits for one query to finish before the next one starts, so the page takes 300 + 200 + 400 = 900 ms: a request waterfall.
The three queries in @/lib/home don't need each other's results. Make the page start all three at once and wait for them together, with Promise.all, so it's ready as soon as the slowest one is: 400 ms. Each query must still run exactly once.
Keep what the page shows as it is: the three <section>s in the same order, each with its <h2> (This week's teas, New in, What customers say) and its list. lib/home.ts keeps started, the queries in the order they began, so the tests can see when each one ran.
now: featured 0 → 300, then new 300 → 500, then reviews 500 → 900 ready at 900 ms
after: featured 0 → 300, new 0 → 200, reviews 0 → 400 ready at 400 ms
lib/home.ts, read-only. Import it as @/lib/home.
export type Tea = { slug: string; name: string };
export type Review = { tea: string; author: string; text: string };
/** The queries that have started, in order: what the database would see. */
export const started: string[] = [];
/** How long the database takes to answer. */
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
/** This week's featured teas. Takes 300 ms. */
export async function getFeaturedTeas(): Promise<Tea[]> {
started.push("featured");
await wait(300);
return [
{ slug: "sencha", name: "Sencha" },
{ slug: "assam", name: "Assam" },
];
}
/** The teas added most recently. Takes 200 ms. */
export async function getNewTeas(): Promise<Tea[]> {
started.push("new");
await wait(200);
return [{ slug: "gyokuro", name: "Gyokuro" }];
}
/** The latest reviews. Takes 400 ms. */
export async function getLatestReviews(): Promise<Review[]> {
started.push("reviews");
await wait(400);
return [
{ tea: "Mint", author: "Ana", text: "Cool and sweet." },
{ tea: "Sencha", author: "Kenji", text: "Fresh, like spring." },
];
}
const clock = useFakeTimers();
const rendering = renderServer(<Page />);
await flush();
await clock.advance(1000);
await rendering;
expect(screen.getAllByRole("heading", { level: 2 }).map((heading) => heading.textContent)).toEqual(["This week's teas", "New in", "What customers say"]);
const clock = useFakeTimers();
const rendering = renderServer(<Page />);
await flush();
await clock.advance(1000);
await rendering;
const items = (name) => within(screen.getByRole("heading", { name }).closest("section")).getAllByRole("listitem").map((item) => item.textContent);
expect(items("This week's teas")).toEqual(["Sencha", "Assam"]);
expect(items("New in")).toEqual(["Gyokuro"]);
expect(items("What customers say")).toEqual(['Mint: "Cool and sweet." (Ana)', 'Sencha: "Fresh, like spring." (Kenji)']);
const clock = useFakeTimers();
const rendering = renderServer(<Page />);
await flush();
expect([...started].sort()).toEqual(["featured", "new", "reviews"]);
await clock.advance(1000);
await rendering;
const clock = useFakeTimers();
const start = Date.now();
let readyAfter = null;
const rendering = renderServer(<Page />).then(() => (readyAfter = Date.now() - start));
await flush();
await clock.advance(1000);
await rendering;
expect(readyAfter).toBe(400);
const clock = useFakeTimers();
const rendering = renderServer(<Page />);
await flush();
await clock.advance(1000);
await rendering;
expect([...started].sort()).toEqual(["featured", "new", "reviews"]);
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 A home page without a waterfall. 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.