How can I implement a suspense using NextJS 14

73 Views Asked by At

I want a suspense (initial loading of the page while page's assets are being fetched)
My assets are the images implemented in the home screen or children of RootLayout
How can I make a initial Loading to the children?

export default function RootLayout() {
  return (
    <html translate="no">
      <head></head>
      <body>
        <Suspense fallback={<div>loading...</div>}> //suspense here 
          <Header />
          {children}
          <Footer />
        </Suspense>
      </body>
    </html>
  );
}
1

There are 1 best solutions below

0
G Sriram On

You can create a loading.jsx or loading.tsx under your page folder to create a loading till all your assets are loaded.

export default function Loading() {
  // You can add any UI inside Loading, including a Skeleton.
  return <LoadingSkeleton />
}

Reference: https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming