11

When I'm using getServerSideProps with next js and it almost takes 5 or 6 seconds for the page to load . I tried using only one url to fetch and not many but again it takes alot for the page to load .

export const getServerSideProps = async (ctx) => {
   let data ;
   try {
      data = axios.get("url")
   } catch(e){
      data = "error"
   }
   return {
     data: data,
   };
 };

I was wondering is there any trick to speed up getServerSideProps data fetching so I could have faster initial page load ?

  • Have you tried testing the API call in isolation/independently of your frontend and seeing what response time is like? Are you confident it's not the API specifically that is slow – adrian Dec 09 '22 at 06:50

1 Answers1

1

got this solution while searching for this bug

const isServerReq = req => !req.url.startsWith('/_next');

function Post({ post }) {
  // Render post...
}

export async function getServerSideProps({ req }) {
  const initData = isServerReq(req) ? await fetch(...) : null;

  return {
    props: {
      initData,
    },
  }
}

issue: https://github.com/vercel/next.js/issues/13910