skip to Main Content

I am creating a Single Page Application on Nextjs 13, using the pages directory. I have exported a getServerSideProps() in one of the pages which passes some fetched data to the props. I found that the data fetching takes 2-3 seconds during which the page loads. I want to provide some loading state UI until this page is getting rendered server side. How can I do that efficiently?

I tried to use router events like some other solutions but those do not get triggered if the route is directly opened (or in case of home route being server rendered).

Here is a simplified code for the server side props fetching:

export async function getServerSideProps() {
  try {
    //fetching data 
    return {
      props: { data: //fetched data },
    };
  } catch (e) {
    console.error(e);
  }
} 

2

Answers


  1. If I understood your question, you are trying to display some friendly page / component till final page is being rendered.

    Since you are using Nextjs 13 – I will look into creating a loading state by adding a loading.js file. here the link to detailed explanations / examples from Nextjs.org
    https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming

    Good luck

    Login or Signup to reply.
  2. You cannot use FallBack like in get static props.
    We can solve the slow load time by using apollo client.
    https://github.com/vercel/next.js/tree/canary/examples/with-apollo
    Sure there are extra work
    but it loads instantaneously.
    you will also get the loading state
    or a simpler solution will be to use getStaticProps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search