Next.js: Dynamic server usage —Route couldn't be rendered statically [Fixed 2026]
Also covers: cookies() · headers() · searchParams · force-dynamic · Suspense · PPR
⚡ The Error
Error: Dynamic server usage:
Route /dashboard couldn't be rendered statically because
it used `cookies`. See more info here:
https://nextjs.org/docs/messages/dynamic-server-error✅ Fastest Fix
// Add this line at the top of the file
export const dynamic = "force-dynamic"
import { cookies } from "next/headers"
export default async function DashboardPage() {
const cookieStore = await cookies()
const session = cookieStore.get("session")
// ...
}What Causes the Dynamic Server Usage Error?
Next.js App Router statically renders routes at build time by default. Static rendering is faster and cheaper — pages are built once and served from a CDN. But some APIs are inherently per-request: cookies(), headers(), and searchParams all return data that is only known at request time.
When Next.js detects these APIs in a statically-expected route, it throws the dynamic server usage error. You have two choices: accept the dynamic rendering with force-dynamic, or isolate the dynamic parts into a Suspense boundary for partial prerendering.
Add export const dynamic = 'force-dynamic'
Fastest fix — entire route renders per-requestexport const dynamic = "force-dynamic" // ← add this
import { cookies, headers } from "next/headers"
export default async function DashboardPage() {
const cookieStore = await cookies()
const headersList = await headers()
const session = cookieStore.get("session")?.value
const userAgent = headersList.get("user-agent")
return <Dashboard session={session} userAgent={userAgent} />
}force-dynamic is appropriate for authenticated pages, dashboards, and any route where the content is user-specific. It is not appropriate for public marketing pages — those should stay static.
Wrap Dynamic Parts in a Suspense Boundary
Best for performance — static shell + dynamic contentIf only part of the page is dynamic, wrap just that part in <Suspense> and move the dynamic API calls into a child Server Component. The static shell renders at build time; the dynamic part streams in per request.
import { Suspense } from "react"
import { ProductList } from "@/components/product-list"
import { CartCount } from "@/components/cart-count"
// Page stays STATIC — no force-dynamic needed
export default function ShopPage() {
return (
<div>
<h1>Our Products</h1>
{/* Static — rendered at build time */}
<ProductList />
{/* Dynamic — streams per request, Suspense fallback shown until ready */}
<Suspense fallback={<span>Loading cart...</span>}>
<CartCount /> {/* reads cookies() internally */}
</Suspense>
</div>
)
}import { cookies } from "next/headers"
export async function CartCount() {
const cookieStore = await cookies()
const cartId = cookieStore.get("cart_id")?.value
const count = cartId ? await getCartItemCount(cartId) : 0
return <span>Cart ({count})</span>
}Fix searchParams Making a Route Dynamic
Search pages, filtered listings, paginated routesReading searchParams directly in a page component makes the entire route dynamic. Move the searchParams consumer into a child component inside a Suspense boundary instead.
// ❌ searchParams access at page level = force-dynamic on entire route
export default async function SearchPage({
searchParams,
}: {
searchParams: { q?: string; page?: string }
}) {
const query = searchParams.q ?? "" // makes entire page dynamic
const results = await searchProducts(query)
return <ResultsList results={results} />
}// app/search/page.tsx — stays static
import { Suspense } from "react"
import { SearchResults } from "@/components/search-results"
export default function SearchPage() {
return (
<div>
<h1>Search</h1>
<Suspense fallback={<div>Searching...</div>}>
<SearchResults /> {/* reads searchParams inside */}
</Suspense>
</div>
)
}
// components/search-results.tsx
import { type UnsafeUnwrappedSearchParams } from "next/dist/server/app-render/app-render"
export async function SearchResults() {
// searchParams accessed here in a Suspense-wrapped child
const { searchParams } = await import("next/headers") // example approach
// or receive as prop from parent via async data
}Fix cookies() and headers() in Static Routes
Middleware is often the better place for cookie logicIf you only need to read a cookie to redirect or check auth, consider doing it in middleware.ts instead. Middleware runs before rendering and does not affect the static/dynamic status of the page.
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
export function middleware(request: NextRequest) {
const session = request.cookies.get("session")?.value
// Redirect unauthenticated users without making the page dynamic
if (!session && request.nextUrl.pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/login", request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ["/dashboard/:path*"],
}Use generateStaticParams for Dynamic Routes
[slug] pages getting dynamic server usage errorFor dynamic route segments like [id] or [slug], export generateStaticParams to pre-render known paths at build time and avoid the dynamic server usage error for those paths.
// Generate static pages for all known blog posts at build time
export async function generateStaticParams() {
const posts = await db.post.findMany({ select: { slug: true } })
return posts.map((post) => ({ slug: post.slug }))
}
// This page is now statically rendered for known slugs
export default async function BlogPostPage({ params }: { params: { slug: string } }) {
const post = await db.post.findUnique({ where: { slug: params.slug } })
if (!post) notFound()
return <Article post={post} />
}Prevention
- Never read cookies(), headers(), or searchParams at the top level of a page you want to be static
- Use middleware.ts for auth checks and redirects based on cookies — keeps pages static
- Wrap dynamic sections in Suspense boundaries to preserve static rendering for the rest of the page
- Add export const dynamic = 'force-dynamic' intentionally on user-specific routes like dashboards
- Use generateStaticParams for dynamic route segments with known values (blog posts, product pages)
- Run next build locally and check the route type output (● static, λ dynamic) before deploying
Frequently Asked Questions
What does 'Route couldn't be rendered statically' mean in Next.js?+−
How do I fix the dynamic server usage error?+−
When should I use force-dynamic vs Suspense?+−
Why does reading searchParams make a route dynamic?+−
Can I use cookies() in a statically rendered page?+−
What is Partial Prerendering (PPR) in Next.js?+−
Need Expert Help?
We Architect High-Performance Next.js Applications
Softplix engineers design Next.js applications with optimal static/dynamic rendering strategies. Need help maximising your App Router performance? Let us help.
Talk to an Engineer