Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Next.js 14.1 prerendering Error

I recently upgraded my Next.js 14.01 to 14.1 and I got this error while doing the build in my app how can I fix this error?

it says :

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

 ✓ Generating static pages (21/21) 
> Export encountered errors on following paths:
    /_not-found
    /admin/add-products/page: /admin/add-products
    /admin/manage-orders/page: /admin/manage-orders
    /admin/manage-products/page: /admin/manage-products
    /admin/page: /admin
    /cart/page: /cart
    /checkout/page: /checkout
    /collections/page: /collections
    /contactus/page: /contactus
    /login/page: /login
    /orders/page: /orders
    /register/page: /register
Error: Command "npm run build" exited with 1

enter image description here

Please help me to fix this error.

like image 535
Nuwan Chamikara Avatar asked Jan 24 '26 06:01

Nuwan Chamikara


2 Answers

The follwing pages renders a Client Component that uses usePathnames.

enter image description here

Because the error message lists all your pages, the component must be rendered in app/layout.tsx.

This hook gets the searchparams from the Request which is not available at the building time. Using this hook will disable Static PreRendering for the entire route. The error suggest you to benefit from Streaming and partially pre-render the parts of the route that doesn't depend on request time values.

To fix:

  1. Find the component that uses useSearchParams (You can use the IDE search)
  2. wrap it in a <Suspense> boundary:
import {useSearchParams} from "next/navigation";

function MyComponent() {
  const searchParams = useSearchParams()
  // ...
}
// app/page.tsx
import {Suspense} from "react";

export default function Page() {
  return <div>
    <h1>...</h1>
    <Suspense fallback={<>Loading...</>}>
      <MyComponent />
    </Suspense>
  </div>
}
like image 166
Ahmed Abdelbaset Avatar answered Jan 25 '26 20:01

Ahmed Abdelbaset


Just add loading.tsx to the root of app. and if you made any group routing then just add loading.tsx there too. that's worked for me

like image 24
Freelancer BJR Avatar answered Jan 25 '26 19:01

Freelancer BJR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!