# Static vs Dynamic Handlers

> Understanding when Route Handlers are cached.

Subject: Next.js | Lesson 45 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/static-vs-dynamic-handlers
Previous: [Route Handlers (APIs)](https://codewithanimation.in/nextjs/route-handlers.md) | Next: [NextRequest & NextResponse](https://codewithanimation.in/nextjs/web-request-response.md)

## Aggressive Caching

By default, Next.js will statically evaluate `GET` Route Handlers during the build process.

If you fetch data from a database inside a standard `GET` handler and return it, that data will be frozen at build time. It will not update when users request the endpoint.

## Performance vs Freshness

Static handlers are incredibly fast because they just return a pre-generated JSON string from the CDN.

However, APIs usually need to respond with fresh data (e.g., the latest user profile). In these cases, you must opt out of caching to make the handler Dynamic.

## Opting into Dynamic Rendering

`POST`, `PUT`, `PATCH`, and `DELETE` handlers are *always* dynamic.

To make a `GET` handler dynamic, you must do one of three things:

1. Use a dynamic function (like `cookies()` or `headers()`).

2. Access the URL's query string (`request.nextUrl.searchParams`).

3. Explicitly export `const dynamic = 'force-dynamic'` from the file.

## Common mistake: The Stale API Trap

The most common bug with Route Handlers is developers building a `GET` endpoint that returns a list of items from a database, deploying it, and wondering why new items never show up.

Because they didn't read the `Request` object or explicitly force dynamic rendering, Next.js assumed the data was static.

## Try it: The Cache Race

Fire requests at both the Static GET endpoint and the Dynamic GET endpoint. Watch how the Static endpoint always returns the exact same build timestamp, while the Dynamic one updates in real-time.

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

## Check yourself

### Question 1

Which of the following will NOT force a `GET` Route Handler to become dynamic?

- A. Calling `cookies()` inside the handler.
- B. Adding a `console.log()` inside the handler.
- C. Reading `request.nextUrl.searchParams`.
- D. Exporting `const dynamic = 'force-dynamic'`.

Correct answer: B

A `console.log` has no effect on Next.js's static analysis. Unless you use a dynamic API or config, the GET handler will remain statically cached.

- Why not A: Cookies are dynamic, so this forces dynamic rendering.
- Why not C: Search params are only known at request time.
- Why not D: This explicitly forces dynamic rendering.

## Remember this

- `GET` Route Handlers are statically cached by default.
- `POST` and `DELETE` handlers are always dynamic.
- Reading `cookies()`, `headers()`, or `searchParams` opts a GET handler into dynamic rendering.
- You can explicitly opt out of caching with `export const dynamic = 'force-dynamic'`.
