Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Next.js build logs 'Dynamic server usage' for a route handler that reads searchParams

ErrorDynamic server usage: Page couldn't be rendered statically because it used `nextUrl.searchParams`

Short answer

Next.js tries to build a route handler as static HTML or data. When the handler reads the incoming request, such as nextUrl.searchParams, it cannot be static. Declare the route dynamic with export const dynamic = 'force-dynamic', or upgrade, since GET handlers stopped being cached by default in Next.js 15.

  • Applies to Next.js 14, Next.js 15, Next.js 16, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run a production build and note which route the message names. The route table at the end shows whether it is marked static or dynamic.

    npx next build
  2. 2Match what you see, then jump to its fix

    1. 1The route is app/**/route.ts, exports GET, and calls request.nextUrl.searchParams or new URL(request.url)A GET route handler on Next.js 14 reads the requestFix
    2. 2DynamicServerError thrown from code that uses cookies() or headers() inside setTimeout, setInterval or an unawaited promisecookies() or headers() is called outside the request's call stackFix
    3. 3The file exports dynamic = 'error' or dynamic = 'force-static'The route is forced to be static or to fail on request APIsFix

What does the error look like?

  • Dynamic server usage: Page couldn't be rendered statically because it used `nextUrl.searchParams`
  • DynamicServerError

Cause 1 of 3

A GET route handler on Next.js 14 reads the request

1Check

In Next.js 14, GET handlers that return a Response were cached by default, and using the Request object is one of the things that opts out. The build tries static first, sees the request being read, and reports it. Check the Next.js version and the handler.

npx next --version
grep -rn "nextUrl.searchParams" app --include=route.ts --include=route.js

2Fix

Say so explicitly: export dynamic = 'force-dynamic' from the route file. On Next.js 15 and later GET handlers are dynamic by default, so upgrading removes the need.

// app/api/search/route.ts
import { type NextRequest } from 'next/server'

export const dynamic = 'force-dynamic'

export function GET(request: NextRequest) {
  const query = request.nextUrl.searchParams.get('query')
  return Response.json({ query })
}

3Verify

Rebuild and check that the route is listed as dynamic, then call it with a query string.

npx next build
npx next start &
curl "http://localhost:3000/api/search?query=hello"

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

cookies() or headers() is called outside the request's call stack

1Check

These functions only work while the request that started the render is still on the call stack. Find any call that sits inside a timer callback or a promise you did not await.

grep -rnE "setTimeout|setInterval" app lib | head -20

2Fix

Call cookies() or headers() first, in the main async function, and pass the value into the timer or promise. Await every promise that reads them.

import { cookies } from 'next/headers'

async function getCookieData() {
  const cookieStore = await cookies()
  const cookieData = cookieStore.getAll()
  return new Promise((resolve) =>
    setTimeout(() => resolve(cookieData), 1000)
  )
}

3Verify

Run the build again; the error should not appear.

npx next build

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

The route is forced to be static or to fail on request APIs

1Check

'error' makes the build fail if the route uses request-time APIs or uncached data. 'force-static' prerenders the route and makes cookies and headers return empty values, so a handler that depends on them gets nothing.

grep -rnE "export const dynamic" app

2Fix

If the route must read the request, remove those exports or set dynamic = 'force-dynamic'. If it must stay static, stop reading request data in it and move that logic to a dynamic route.

// before
export const dynamic = 'force-static'
// after
export const dynamic = 'force-dynamic'

3Verify

Build again and confirm the route shows as dynamic.

npx next build

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Declare dynamic = 'force-dynamic' on every handler that depends on the request, so the intent is written in the file.
  • Read the build's route table after upgrades; the static and dynamic markers tell you how each route is served.
  • Call cookies() and headers() at the top of the function, before any timer or promise.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.