Part 05 / Topic 2 of 6
async Server Components: await your data directly
Because a Server Component only ever runs on the server, it can do something a client-side React component never could: be an async function, and simply await its own data before returning markup. Apna Bazaar's owner wants an internal "wholesale desk" page — the cost price and margin behind every product, numbers no customer should ever see — and it is built exactly this way, with a small data-layer module standing in for a real database:
// harness/app/store/lib/wholesale.ts
import "server-only";
import { products } from "./products";
export type WholesaleLine = {
id: string;
name: string;
retailPrice: number;
costPrice: number;
};
const costRatioById: Record<string, number> = { p1: 0.55, p2: 0.62, p3: 0.58 };
export const SUPPLIER_HELPLINE = "+91-98xxxxxx01";
export async function getWholesaleLines(): Promise<WholesaleLine[]> {
await Promise.resolve();
return products.map((product) => ({
id: product.id,
name: product.name,
retailPrice: product.price,
costPrice: Math.round(product.price * costRatioById[product.id]),
}));
}
// harness/app/store/(shop)/wholesale/page.tsx
import Link from "next/link";
import { getWholesaleLines, SUPPLIER_HELPLINE } from "../../lib/wholesale";
export default async function WholesalePage() {
const lines = await getWholesaleLines();
// ...render a table of lines...
}
WholesalePage is declared async, and the component body awaits getWholesaleLines() before it ever returns JSX — no useEffect fetching after the first render, no isLoading state, no data ?? null guard. The component simply waits, the same way an async function anywhere else in Node would wait on a real database driver's query() call. The real response proves the table rendered with the resolved data already in place, every time, because by the time any HTML reaches a browser the await has already finished on the server:
$ curl -s http://localhost:PORT/store/wholesale | grep -o '<main[^>]*>.*</main>'
<main style="font-family:system-ui;padding:2rem;max-width:560px"><h1>Wholesale desk</h1><p>Internal margin view — never shown to a customer browsing Apna Bazaar.</p><table style="width:100%;border-collapse:collapse"><thead><tr><th style="text-align:left">Product</th><th style="text-align:right">Retail</th><th style="text-align:right">Cost</th><th style="text-align:right">Margin</th></tr></thead><tbody><tr><td>Jaipur Blue Pottery Vase</td><td style="text-align:right">₹<!-- -->1,299</td><td style="text-align:right">₹<!-- -->714</td><td style="text-align:right">₹<!-- -->585</td></tr><tr><td>Banarasi Silk Saree</td><td style="text-align:right">₹<!-- -->4,999</td><td style="text-align:right">₹<!-- -->3,099</td><td style="text-align:right">₹<!-- -->1,900</td></tr><tr><td>Chennai Filter Coffee Set</td><td style="text-align:right">₹<!-- -->899</td><td style="text-align:right">₹<!-- -->521</td><td style="text-align:right">₹<!-- -->378</td></tr></tbody></table><p>Supplier helpline (staff only): <!-- -->+91-98xxxxxx01</p><p><a href="/store">Back to Apna Bazaar</a></p></main>
🔍 Going deeper — a genuinely new finding beyond
TOOLING.md'snew Date()check:TOOLING.mdalready found thatcacheComponents: true(on by default in this harness, since Part 1) rejects a Server Component that reads a non-deterministic value likenew Date()during prerendering. This part found something sharper while writinggetWholesaleLines(): the build rejects a bare timer, even one whose resolved value never changes and is never read. Swap the harmlessawait Promise.resolve();above forawait new Promise((resolve) => setTimeout(resolve, 10));— delaying, but still always resolving to the sameundefined— and the real build fails, for real, with exactly the same errorTOOLING.mdrecorded fornew Date():$ npm run build ... Error: Route "/store/wholesale": Next.js encountered uncached or runtime data during prerendering. `fetch(...)`, `cookies()`, `headers()`, `params`, `searchParams`, or `connection()` accessed outside of `<Suspense>` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.So the rule isn't just "could this value change between two prerenders" — it's "could this
awaititself take an unpredictable amount of time," which asetTimeout-backed Promise always can, even resolving to a constant. Reverting toawait Promise.resolve();(or noawaitworth flagging at all) builds clean again.<Suspense>(Part 7) and"use cache"(Stage 3) are the two real fixes for a genuinely slow await; neither is taught yet, so this page's data stays instant on purpose.
Full files: examples/01-wholesale-data-module.ts, examples/02-wholesale-page.tsx.
Done here? The practice set for Part 5 — including the solutions — is on the part page.