Skip to the code

A home page without a waterfall

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

The app's other files

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." },
  ];
}

What the tests check

  1. shows the three sections in order
    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"]);
    
  2. shows each section's own data
    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)']);
    
  3. starts all three queries before any of them finishes
    const clock = useFakeTimers();
    const rendering = renderServer(<Page />);
    await flush();
    expect([...started].sort()).toEqual(["featured", "new", "reviews"]);
    await clock.advance(1000);
    await rendering;
    
  4. is ready in 400 ms, when the slowest query is
    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);
    
  5. runs each query once
    const clock = useFakeTimers();
    const rendering = renderServer(<Page />);
    await flush();
    await clock.advance(1000);
    await rendering;
    expect([...started].sort()).toEqual(["featured", "new", "reviews"]);
    

Learn more

app/page.tsxSaved 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).