Next.js App Router Best Practices

Chapter 2 · Practice 5 of 6

Call connection() before per-request random values and timestamps

From Caching with Cache Components

Why it matters

A prerender runs once, so any random value or timestamp it computes would be frozen for every visitor. Under Cache Components, Math.random(), Date.now() and crypto.randomUUID() therefore need an explicit decision.

You needDo this
A value unique per requestawait connection() from next/server first, and wrap the component in Suspense
One shared value is fineCompute it inside a 'use cache' scope, so everyone sees the same value until it revalidates

The dev overlay flags these calls, so you do not need to remember every case.

Example

TSX
import { connection } from 'next/server';

async function RequestId() {
  await connection();
  return <p>Request: {crypto.randomUUID()}</p>;
}