#Do you use `Suspense fallback` on `Pages router` for a loading page?

24 messages · Page 1 of 1 (latest)

vital venture
#

I'm trying to have a fallback page that is displayed only when some data is being fetched. In NextJS's app router, they use a loading.tsx file, but I can't use that for the pages router it seems. ReactJS has Suspense fallback and there's something in NextJS called Lazy loading which uses next/dynamic, should I use this then? Would it be something like

export default function Home<HomeProps>() {

  const DynamicTest = dynamic(async () => {
    await Wait(2000)
    return import("@/pages/Home")
  }, {
    loading: () => <Loading></Loading>
  })

  return (
    <>

      {<DynamicTest />}

    </>
  )
}

The function Wait has a setTimeout asynchronous function that halts for 2000 milliseconds to emulate fetching data. Would this be a good practice?

gentle copperBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

fallow pine
#

to use Suspense with data fetching, you need to use Suspense-compatible data fetching APIs/libraries which are rare

#

server components are one of such APIs, but it doesn't work in the pages router

#

in the pages router you cannot have loading state for getServerSideProps; if you do client-side fetching with e.g. tanstack query you can use the isLoading prop to control loading state manually

vital venture
vital venture
#

const [isLoading, setIsLoading] = useState(true)

return <> 
{isLoading ? <Loading/> : <Home/>}
</>

vital venture
fallow pine
vital venture
fallow pine
vital venture
fallow pine
vital venture
fallow pine
#

Depending on how you fetch data and what router you use the approach is different

vital venture
fallow pine
#

Conditional rendering and SSR/SSG are two completely different groups of concepts

vital venture
fallow pine