Part 12 / Topic 7 of 7
useOptimistic + useTransition — making latency disappear from the user's perspective
getCachedMenu (Part 10/11's own function) has a genuine 500ms simulated price-join delay. Toggling an item's stock status re-runs that same cached function via updateTag, section 6's mechanism — which means, with no further work, every click on a stock toggle would sit doing nothing for over 500ms before the button visibly changed. AvailabilityToggle.tsx fixes exactly this, using the installed interactive-apps.md guide's own pattern:
// harness/app/swiggy-menu/_components/AvailabilityToggle.tsx
const [optimisticAvailable, setOptimisticAvailable] = useOptimistic(available);
const [isPending, startTransition] = useTransition();
<button
onClick={() => {
const next = !optimisticAvailable;
startTransition(async () => {
setOptimisticAvailable(next); // applies on THIS frame
await toggleAvailability(id, next); // the real, slow Server Action
});
}}
>
{optimisticAvailable ? "In stock" : "Sold out"}
</button>
// harness/app/swiggy-menu/toggle-actions.ts
export async function toggleAvailability(id: string, available: boolean) {
setAvailability(id, available);
updateTag("swiggy-menu"); // section 6's mechanism, reused
}
setOptimisticAvailable updates what the button shows immediately, on the same frame as the click — before the Server Action has even been dispatched, let alone finished. Measuring the real round trip this is hiding, by calling the exact same action directly (the browser's own fetch-based dispatch, reproduced with curl by reading the action id out of the compiled build's own server-reference manifest, .next/server/app/swiggy-menu/page/server-reference-manifest.json):
$ time curl -s -o /dev/null -X POST "http://localhost:PORT/swiggy-menu" \
-H "Next-Action: 602e8053cda5ef5b751880f7295ac1525f8acc0018" \
-H "Content-Type: text/plain;charset=UTF-8" \
--data-raw '["m2",false]'
real 0m0.527s
527ms, matching getCachedMenu's own 500ms delay plus overhead — exactly what a user would otherwise sit through, in silence, waiting for the button to respond. useOptimistic doesn't make that 527ms go away; it changes what's visible during it, which is the entire value of this pattern. When the transition finishes, the real server-confirmed value (from the next render, carried in the same response via updateTag, section 6) replaces the optimistic one — if the two ever disagreed (a second admin changed the same item first, say), the server's answer would win, not the optimistic guess.
🔍 Going deeper — reading the real action id out of a production build: the
Next-Actionheader above (602e8053...) isn't guessed — it's read directly from.next/server/app/swiggy-menu/page/server-reference-manifest.json, generated bynext build, under the key matching"exportedName": "toggleAvailability". This is the same encrypted-action-id mechanism section 5's security note names; it rotates on every build (confirmed by theserver-actions.mdguide: "Next.js rotates them at most every 14 days, even when the source is unchanged"), so this exact id string is a point-in-time fact about one specific build, not a stable API.
Done here? The practice set for Part 12 — including the solutions — is on the part page.