# Dynamic Rendering (Opting Out)

> How request-specific functions force routes out of the static cache.

Subject: Next.js | Lesson 29 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/dynamic-rendering
Previous: [On-Demand Revalidation (revalidateTag)](https://codewithanimation.in/nextjs/revalidate-tag.md) | Next: [React cache()](https://codewithanimation.in/nextjs/react-cache.md)

## The Poison Pill

Dynamic rendering means a page is generated on the server at the exact moment a user requests it, rather than being pre-built.

In Next.js, this happens automatically when you use a Dynamic Function like `cookies()`, `headers()`, or `searchParams`. These functions act as a 'poison pill' — the moment Next.js detects them during the build, it aborts static generation for that route.

## Unknowable Data

Next.js aggressively tries to build everything statically for maximum performance. However, some data is impossible to know ahead of time.

You cannot know what a user's session cookie is, or what they will type into a search bar (`?query=test`), until they actually make the HTTP request.

## Automatic Detection

You do not need to manually configure `{ cache: 'no-store' }` to make a route dynamic.

Simply importing and calling `import { cookies } from 'next/headers'; cookies().get('session');` in a Server Component is enough to transition the entire route from Static to Dynamic.

## Common mistake: Accidental De-optimization

A common mistake for beginners is adding `headers()` to a layout or page to check a minor detail, like the user's browser type.

Because they called a dynamic function, they accidentally de-optimized the entire page. It will no longer be served instantly from the CDN; the server will have to re-render it from scratch on every single visit.

## Tip: Push It Down

To protect your static pages, push dynamic behavior as far down the component tree as possible, or handle it on the client side.

Alternatively, keep an eye on Partial Prerendering (PPR), an experimental Next.js feature that allows you to wrap dynamic components in a `<Suspense>` boundary while keeping the rest of the page static.

## Try it: The Poison Pill

Hover over the code snippets. Notice how standard Server Components result in a Static Route (Full Route Cache), but the moment you introduce `cookies()`, Next.js detects the 'poison pill' and forces the route out of the cache.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/dynamic-rendering

## Check yourself

### Question 1

Which of the following will automatically force a route to become dynamically rendered?

- A. Using the <Image /> component
- B. Calling the cookies() function
- C. Importing a Client Component
- D. Using a CSS Module

Correct answer: B

Functions like `cookies()` or `headers()` rely on the incoming HTTP request. Next.js detects them and automatically switches the route to Dynamic Rendering.

- Why not A: The Next.js Image component handles image optimization, but does not opt you out of static caching.
- Why not C: Client Components are still pre-rendered to HTML during the static build step.
- Why not D: CSS has no impact on server rendering strategies.

### Question 2

You have a fast, static blog. You add `headers()` to the root `layout.tsx` to read the user-agent. What happens?

- A. Only the layout becomes dynamic; the blog posts remain static
- B. The blog throws a build error
- C. The entire blog becomes dynamically rendered on every request
- D. Next.js caches the headers() output at build time

Correct answer: C

This is the classic 'accidental de-optimization'. Because the root layout requires request-time data, every single page that shares that layout must now be rendered on demand.

- Why not A: Without Partial Prerendering, a dynamic function in a layout poisons the entire route tree beneath it.
- Why not B: Next.js gracefully transitions it to dynamic rendering without error.
- Why not D: headers() cannot be cached at build time because they change per-request.

## Remember this

- Dynamic rendering occurs when a page is built at request-time.
- `cookies()`, `headers()`, and `searchParams` act as 'poison pills'.
- Calling a dynamic function automatically opts the route out of the static cache.
- Be careful: adding a dynamic function to a layout can accidentally de-optimize your whole app.
- Next.js 15's Partial Prerendering (PPR) aims to solve this by mixing static shells with dynamic holes.
