Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

useSearchParams() should be wrapped in a suspense boundary: how to fix it in Next.js

next build prerenders the page, and the query string does not exist at build time, so a Client Component that calls useSearchParams() needs a <Suspense> boundary above it. Wrap the smallest component that reads the search params in <Suspense>, read searchParams in the server page instead, or make the route dynamic with connection(). next dev renders on demand, which is why it only fails in the build.

By Livops team, the people who build and run LivopsUpdated

The end of the next build log:

 ⨯ useSearchParams() should be wrapped in a suspense boundary at page "/search". Read more: https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout
    at ...
Error occurred prerendering page "/search". Read more: https://nextjs.org/docs/messages/prerender-error

Both lines come from Next.js itself: the first is written when a page bails out to client-side rendering while it is being prerendered[4], the second by the build step that prerenders pages[5]. The Next.js docs call this error “Missing Suspense boundary with useSearchParams”[1]. This page explains why it happens only in the build, gives the three fixes with code, and shows how to find the component when the message names a page you did not expect.

What does the error mean?

useSearchParams is a Client Component hook that reads the current URL’s query string[2]. next build prerenders every static route to HTML ahead of time, and at that point there is no request and no query string.

Next.js handles this with Suspense. If a route is prerendered, calling useSearchParams makes the Client Component tree up to the closest Suspense boundary render on the client, so the rest of the route can still be prerendered[2]. With no boundary at all, the whole page would be rendered on the client and could be blank until its JavaScript loads[1]. Rather than ship that, the production build fails with this error[2].

Why next dev does not show it

In development, routes are rendered on demand, so useSearchParams does not suspend and the page may appear to work without Suspense[2]. The first time the page is prerendered is in next build, which is why a project can work on your machine all week and fail on its first deploy. Run next build locally before you push.

Finding the component

The message names the page, not the component. The component can sit anywhere the page renders it: a search box in a shared header, a filter bar, a layout used by many pages. For unminified stack traces with source maps that point at the exact component and route, run[1]:

npx next build --debug-prerender

Fixes, most common first

1. Wrap the component that reads the URL in Suspense

To keep the route static, wrap the smallest subtree that calls useSearchParams() in <Suspense>[1]. Everything outside the boundary is prerendered and sent in the initial HTML; the fallback stands in for the component until it renders in the browser[2].

// app/search/search-bar.tsx
'use client'
import { useSearchParams } from 'next/navigation'

export default function SearchBar() {
  const searchParams = useSearchParams()
  return <input defaultValue={searchParams.get('q') ?? ''} />
}
// app/search/page.tsx (a Server Component)
import { Suspense } from 'react'
import SearchBar from './search-bar'

export default function Page() {
  return (
    <>
      <Suspense fallback={<input disabled placeholder="Search" />}>
        <SearchBar />
      </Suspense>
      <h1>Search</h1>
    </>
  )
}

If the whole page.tsx is a 'use client' component that calls useSearchParams at the top, move that code into a child Client Component and render it inside <Suspense> from the page[1]; the docs suggest making the page a Server Component again and isolating the client-only code[1]. A fallback the same size as the real component avoids a layout jump when it swaps in.

2. Read searchParams in the server page

A page receives the query string as its searchParams prop[3]. Since Next.js 15 it is a promise, so you await it, or unwrap it with React’s use in a Client Component[3]. Using it opts the page into dynamic rendering at request time[3], so nothing needs to suspend at build time:

// app/search/page.tsx
export default async function Page({
  searchParams,
}: {
  searchParams: Promise<{ q?: string }>
}) {
  const { q } = await searchParams
  return <Results query={q ?? ''} />
}

You can then pass the values down as props to any component, Server or Client[2]. If you pass the promise itself to a Client Component and unwrap it with use(), that component suspends, so it still needs a Suspense boundary around it[1]. Layouts do not receive searchParams[2]: in a layout, use fix 1.

3. Make the route dynamic

If the page is meant to be rendered per request anyway, say so. Call connection() in a Server Component, the page or a wrapping layout: it waits for an incoming request and excludes everything below from prerendering[1].

// app/search/page.tsx
import { connection } from 'next/server'
import SearchBar from './search-bar'

export default async function Page() {
  await connection()
  return <SearchBar />
}

A dynamically rendered route has useSearchParams available on the server during the initial render[2]. Before connection(), the same was done with export const dynamic = 'force-dynamic' in a Server Component page.tsx or layout.tsx; setting dynamic in a 'use client' page has no effect[1]. The cost is that the page is rendered on every request instead of once at build time, which needs a running Next.js server rather than static files.

Not a fix: turning the check off

In Next.js 14.x, experimental.missingSuspenseWithCSRBailout: false disabled the error. The docs do not recommend it, say the option is only available in 14.x, and tell later versions to use the fixes above[1].

With Cache Components: a different message

With Cache Components enabled, the same mistake reads Next.js encountered URL data in a Client Component outside of Suspense. It covers all the navigation hooks, usePathname, useParams, useSearchParams, useSelectedLayoutSegment and useSelectedLayoutSegments, called outside a <Suspense> boundary during prerendering[6]. The fix is the same: a boundary with a fallback that can go into the static shell[6].

How to confirm the fix locally

Build and start the production server exactly as a host does, then open the page with a query string:

npx next build
npx next start
# open http://localhost:3000/search?q=test

The build must finish without the error, and the page must show the value from the URL once loaded. If another page fails next, it is usually the same component used elsewhere: the boundary belongs as close to that component as possible, so every page that uses it is covered.

On Livops

  • The build fails, the site does not. next build runs in the build step, so this error ends the deploy as build_failed. The previous version keeps running, and the live address keeps serving it while you fix the page.
  • The same error as on your machine. The cause of a failed build is nearly always in the code: run npm run build locally and it stops in the same place. The build log shows the page path from the message.
  • The assistant explains. Next to a failed build there is a single button: it reads the log and tells you what happened and how to fix it. It does not change your code.
  • Dynamic routes work. A Next.js app that is not a static export runs on Livops as a Node server, started with next start or, when the build writes Next’s standalone output, with its server.js. So fixes 2 and 3, which render the page per request, work as they do locally.

More in the docs: Build failed.

Questions

Why does it work in next dev but fail in next build?

In development every route is rendered on demand, with the real URL, so useSearchParams does not suspend and the page appears to work without Suspense. next build prerenders static pages ahead of time, when there is no query string, and there the missing boundary fails the build.

Where exactly do I put the Suspense boundary?

Around the smallest component that calls useSearchParams, not around the whole page. Everything outside the boundary is still prerendered into the HTML, and the fallback is shown in place of that component until the browser renders it with the real query string.

Does force-dynamic fix it?

Set in a Server Component page.tsx or layout.tsx, it opts the route into rendering per request, which is how the docs described this fix before the connection API existed. They now prefer calling await connection() in a Server Component. Setting dynamic in a 'use client' page has no effect.

Can I turn the error off?

Only in Next.js 14, with experimental.missingSuspenseWithCSRBailout set to false, and the docs do not recommend it. For later versions the docs say to fix it with a Suspense boundary, the searchParams prop or dynamic rendering.

The error names a page that does not call useSearchParams. Why?

The component that calls it is rendered by that page somewhere below it, for example a search box in a shared header or a layout. Run next build --debug-prerender for unminified stack traces with source maps that point at the component.

Is this the same as Next.js encountered URL data in a Client Component outside of Suspense?

It is the same problem with Cache Components turned on. There the message covers all navigation hooks, such as usePathname and useParams too, and the fix is again a Suspense boundary with a fallback.

Sources

  1. Next.js docs — Missing Suspense boundary with useSearchParams (nextjs.org). Checked .
  2. Next.js docs — useSearchParams (nextjs.org). Checked .
  3. Next.js docs — page.js, searchParams (nextjs.org). Checked .
  4. Next.js source — packages/next/src/server/app-render/app-render.tsx (raw.githubusercontent.com). Checked .
  5. Next.js source — packages/next/src/export/worker.ts (raw.githubusercontent.com). Checked .
  6. Next.js docs — Next.js encountered URL data in a Client Component outside of Suspense (nextjs.org). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Next.js app

Got a question? — @livops_uz