I finished writing my frontend code in Next.js and wanted to build, only to keep getting confronted with this error:
⨯ useSearchParams() should be wrapped in a suspense boundary at page "/404". Read more: https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout
at a (C:\Users\USER\Desktop\dock-fe\.next\server\chunks\203.js:3:13936)
at d (C:\Users\USER\Desktop\dock-fe\.next\server\chunks\203.js:5:30727)
at p (C:\Users\USER\Desktop\dock-fe\.next\server\chunks\403.js:1:10402)
at nF (C:\Users\USER\Desktop\dock-fe\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:76:46843)
...
at nX (C:\Users\USER\Desktop\dock-fe\node_modules\next\dist\compiled\next-server\app-page.runtime.prod.js:76:69876)
Error occurred prerendering page "/_not-found". Read more: https://nextjs.org/docs/messages/prerender-error
Export encountered an error on /_not-found/page: /_not-found, exiting the build.
⨯ Next.js build worker exited with code: 1 and signal: null
.next
directory to rebuild from scratch - still nothing happened.useSearchParams()
function and wrapped them in a suspense boundary, even had to create new components for it - still the same error!Q: How to fix "useSearchParams() should be wrapped in a suspense boundary at page '/404'" in Next.js?
A: After 3 days of debugging, here’s the solution that worked when nothing else did:
1️⃣ Wrap your provider in Suspense (mine used useSearchParams()
in AuthProvider
):
// components/auth-wrapper.tsx
"use client";
import { Suspense } from "react";
import { AuthProvider } from "./auth-context";
export function AuthWrapper({ children }) {
return (
<Suspense fallback="Loading...">
<AuthProvider>{children}</AuthProvider>
</Suspense>
);
}
2️⃣ Update your root layout:
// app/layout.tsx
export default function RootLayout({ children }) {
return (
<html>
<body>
<AuthWrapper> {/* Replaces AuthProvider */}
{children}
</AuthWrapper>
</body>
</html>
);
}
/404
useSearchParams()
requires Suspense for SSR/static generation# Check what's actually using useSearchParams
grep -r "useSearchParams" src/
ctrl+shift+F
not-found.tsx
To Next.js Devs: Please improve the error messages! A simple pointer to the problematic component would save hours of pain. 😅
This approach eliminated the error instantly after days of failed attempts.