Menu

Static vs Dynamic Rendering

Lesson 1: Static vs Dynamic Rendering

What decides how a route renders?

Next.js tries to prerender every route at build time. A route becomes dynamic (rendered per request) when it reads information that only exists at request time.

API Makes the route dynamic? Why
cookies() Yes Different for each user
headers() Yes Different for each request
searchParams prop Yes Different for each URL query
connection() from next/server Yes Explicitly waits for a real request
params for paths not in generateStaticParams Rendered on first request Unknown at build time
fetch with no request-time data No (by default the result is baked into the prerender) Same for everyone

Example

// app/about/page.tsx – static: nothing request-specific
export default function About() {
  return <h1>About LearnHub</h1>;
}

// app/search/page.tsx – dynamic: depends on the query
export default async function Search({ searchParams }: PageProps<'/search'>) {
  const { q } = await searchParams;
  const results = await searchCourses(String(q ?? ''));
  return <Results items={results} />;
}

The build output shows the result for each route:

Route (app)
┌ ○ /about
├ ƒ /search
└ ● /courses/[slug]

○  (Static)   prerendered as static content
●  (SSG)      prerendered as static HTML (uses generateStaticParams)
ƒ  (Dynamic)  server-rendered on demand

How it works

  • Static routes are generated once and served from a CDN or disk, so they are fast and cheap.
  • Dynamic routes run on every request, so they are always fresh but cost server time.
  • One dynamic API anywhere in the route (including a shared layout) can make the whole route dynamic, unless you isolate it inside a Suspense boundary with Cache Components (Lesson 4).

Route segment config (without Cache Components): older projects control rendering with exports such as export const dynamic = 'force-dynamic' or export const revalidate = 3600. These options are not used when Cache Components is enabled; 'use cache' and cacheLife replace them.

Common mistakes

Mistake Effect Fix
Reading cookies in the root layout Every page becomes dynamic Read cookies in the small component that needs them
Expecting Date.now() on a static page to update Shows the build time forever Make that part dynamic or render the time on the client
Never checking the build output Surprise slow pages in production Review the ○ / ƒ symbols after next build

Key takeaways

  • Request-time APIs make routes dynamic; everything else can be prerendered.
  • Check the build output to confirm how each route renders.

Try it: Run npm run build on LearnHub and list which routes are static and which are dynamic. Change one dynamic route to static by removing an unnecessary request-time API.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details