Skip to the code

Stream the slow reviews

Each tea's page at Leaf and Kettle, app/teas/[slug]/page.tsx, shows the tea and its reviews. The tea takes 100 ms to load, but the reviews come from a slow service that takes 800 ms, and right now the page awaits both, one after the other, so customers look at nothing for 900 ms. Stream the reviews instead: the tea shows as soon as it's loaded, with Loading reviews… where the reviews will go, and the reviews replace it when they arrive, in the same response.

Load the tea with getTea(slug) and the reviews with getReviews(slug), both from @/lib/teas. params is a promise of { slug }. When there's no such tea, call notFound() from next/navigation.

Show the tea's name in an <h1>, its about text, and a Reviews heading (<h2>). Below the heading, render the reviews in an async Server Component of their own, wrapped in <Suspense> with <p>Loading reviews…</p> as its fallback. Show each review as a list item, Kenji: Fresh, like spring., or No reviews yet. when there are none.

Start the reviews query at the same time as the tea's, not after it: the reviews should be on the page 800 ms after the request, not 900.

/teas/sencha
  0 ms   both queries start
100 ms   Sencha, Grassy, bright, and fresh., Reviews, Loading reviews…
800 ms   Kenji: Fresh, like spring.  Ana: My morning cup.

The app's other files

lib/teas.ts, read-only. Import it as @/lib/teas.

export type Tea = { slug: string; name: string; about: string };
export type Review = { author: string; text: string };
 
/** The queries that have started, in order: what the database would see. */
export const started: string[] = [];
 
/** How long each query takes to answer. */
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
 
const teas: Tea[] = [
  { slug: "sencha", name: "Sencha", about: "Grassy, bright, and fresh." },
  { slug: "assam", name: "Assam", about: "Malty and strong." },
];
 
const reviews: Record<string, Review[]> = {
  sencha: [
    { author: "Kenji", text: "Fresh, like spring." },
    { author: "Ana", text: "My morning cup." },
  ],
};
 
/** The tea with this slug, or undefined when the shop doesn't sell it. Takes 100 ms. */
export async function getTea(slug: string): Promise<Tea | undefined> {
  started.push(`tea:${slug}`);
  await wait(100);
  return teas.find((tea) => tea.slug === slug);
}
 
/** The tea's reviews, newest first, or an empty list. The reviews service is slow: 800 ms. */
export async function getReviews(slug: string): Promise<Review[]> {
  started.push(`reviews:${slug}`);
  await wait(800);
  return reviews[slug] ?? [];
}

What the tests check

  1. shows the tea once it's loaded, with Loading reviews… in place of the reviews
    // Renders a page the way Next.js streams it: each Server Component shows once its data has
    // arrived, and a <Suspense> shows its fallback until everything inside it has.
    const Await = ({ promise }) => React.use(promise);
    const streamed = (node) => {
      if (Array.isArray(node)) return node.map(streamed);
      if (!React.isValidElement(node)) return node;
      if (typeof node.type === "function") return <Await key={node.key} promise={Promise.resolve(node.type(node.props)).then(streamed)} />;
      const { children, fallback } = node.props;
      const props = fallback === undefined ? {} : { fallback: streamed(fallback) };
      return children === undefined ? React.cloneElement(node, props) : React.cloneElement(node, props, ...[].concat(streamed(children)));
    };
    const clock = useFakeTimers();
    const open = async (slug) => {
      await settle(async () => {
        render(streamed(<Page params={params({ slug })} />));
      });
      await flush();
    };
    await open("sencha");
    await clock.advance(100);
    expect(screen.getByRole("heading", { level: 1, name: "Sencha" })).toBeInTheDocument();
    expect(screen.getByText("Grassy, bright, and fresh.")).toBeInTheDocument();
    expect(screen.getByRole("heading", { level: 2, name: "Reviews" })).toBeInTheDocument();
    expect(screen.getByText("Loading reviews…")).toBeInTheDocument();
    expect(screen.queryByRole("listitem")).not.toBeInTheDocument();
    
  2. streams the reviews in when they arrive
    // Renders a page the way Next.js streams it: each Server Component shows once its data has
    // arrived, and a <Suspense> shows its fallback until everything inside it has.
    const Await = ({ promise }) => React.use(promise);
    const streamed = (node) => {
      if (Array.isArray(node)) return node.map(streamed);
      if (!React.isValidElement(node)) return node;
      if (typeof node.type === "function") return <Await key={node.key} promise={Promise.resolve(node.type(node.props)).then(streamed)} />;
      const { children, fallback } = node.props;
      const props = fallback === undefined ? {} : { fallback: streamed(fallback) };
      return children === undefined ? React.cloneElement(node, props) : React.cloneElement(node, props, ...[].concat(streamed(children)));
    };
    const clock = useFakeTimers();
    const open = async (slug) => {
      await settle(async () => {
        render(streamed(<Page params={params({ slug })} />));
      });
      await flush();
    };
    await open("sencha");
    await clock.advance(1000);
    expect(screen.queryByText("Loading reviews…")).not.toBeInTheDocument();
    expect(screen.getAllByRole("listitem").map((item) => item.textContent)).toEqual(["Kenji: Fresh, like spring.", "Ana: My morning cup."]);
    expect(screen.getByRole("heading", { level: 1, name: "Sencha" })).toBeInTheDocument();
    
  3. starts the reviews query with the tea's, so they arrive at 800 ms
    // Renders a page the way Next.js streams it: each Server Component shows once its data has
    // arrived, and a <Suspense> shows its fallback until everything inside it has.
    const Await = ({ promise }) => React.use(promise);
    const streamed = (node) => {
      if (Array.isArray(node)) return node.map(streamed);
      if (!React.isValidElement(node)) return node;
      if (typeof node.type === "function") return <Await key={node.key} promise={Promise.resolve(node.type(node.props)).then(streamed)} />;
      const { children, fallback } = node.props;
      const props = fallback === undefined ? {} : { fallback: streamed(fallback) };
      return children === undefined ? React.cloneElement(node, props) : React.cloneElement(node, props, ...[].concat(streamed(children)));
    };
    const clock = useFakeTimers();
    const open = async (slug) => {
      await settle(async () => {
        render(streamed(<Page params={params({ slug })} />));
      });
      await flush();
    };
    await open("sencha");
    expect([...started].sort()).toEqual(["reviews:sencha", "tea:sencha"]);
    await clock.advance(800);
    expect(screen.getAllByRole("listitem").length).toBe(2);
    
  4. says No reviews yet. when a tea has none
    // Renders a page the way Next.js streams it: each Server Component shows once its data has
    // arrived, and a <Suspense> shows its fallback until everything inside it has.
    const Await = ({ promise }) => React.use(promise);
    const streamed = (node) => {
      if (Array.isArray(node)) return node.map(streamed);
      if (!React.isValidElement(node)) return node;
      if (typeof node.type === "function") return <Await key={node.key} promise={Promise.resolve(node.type(node.props)).then(streamed)} />;
      const { children, fallback } = node.props;
      const props = fallback === undefined ? {} : { fallback: streamed(fallback) };
      return children === undefined ? React.cloneElement(node, props) : React.cloneElement(node, props, ...[].concat(streamed(children)));
    };
    const clock = useFakeTimers();
    const open = async (slug) => {
      await settle(async () => {
        render(streamed(<Page params={params({ slug })} />));
      });
      await flush();
    };
    await open("assam");
    await clock.advance(100);
    expect(screen.getByText("Loading reviews…")).toBeInTheDocument();
    await clock.advance(900);
    expect(screen.getByText("No reviews yet.")).toBeInTheDocument();
    expect(screen.queryByText("Loading reviews…")).not.toBeInTheDocument();
    
  5. calls notFound() for a tea the shop doesn't sell
    const clock = useFakeTimers();
    const status = accessErrorOf(() => Page({ params: params({ slug: "coffee" }) }));
    await flush();
    await clock.advance(100);
    expect(await status).toBe(404);
    

Learn more

app/teas/[slug]/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).