2

I am serving my next.js on Vercel (I tried Amplify as well). Page works but components that require data from getServerSideProps() in my pages/index.tsx are not required. It seems that function isn't called at all.

Could anyone help me fix this issue?

export default function Home({ cryptosData, tempObject }) {
  return (
    <>
      {tempObject && <Converter tempObject={tempObject} />} 
      {cryptosData && <MainTable data={cryptosData} />}
    </>
  );
}

export const getServerSideProps = async () => {
  const url =
    "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";

  const query = "?limit=80";
  try {
    const res = await fetch(url + query, {
      headers: { "X-CMC_PRO_API_KEY": process.env.API_KEY },
    });
    const json = await res.json();
    const data = json.data;
    const [cryptosData, tempObject] = parseData(data, 40);
    return {
      props: { cryptosData, tempObject },
    };
  } catch (error) {
    console.log(error);
    return {
      props: {},
    };
  }
};
Yeon
  • 23
  • 3
  • That's right, the function isn't actually called in non-page components. `getServerSideProps` only works in page components. Does this answer your question: [NEXTJS: getServerSideProps not working into components](https://stackoverflow.com/questions/64136025/nextjs-getserversideprops-not-working-into-components)? – juliomalves Mar 11 '22 at 22:01
  • If I understand it correctly, my code is located at the main home page at pages/index.tsx. Does this page not support `getServerSideProps` ? It works on DEV so I assumed it was my deployment issue. – Yeon Mar 13 '22 at 18:55
  • I forgot to mention, the code works on local but not after production. – Yeon Mar 13 '22 at 18:57
  • 1
    If that's the component at `pages/index` then `getServerSideProps` should definitely be called. Do you see any errors in Vercel logs for that page? The request could be failing for some reason, is `process.env.API_KEY` properly setup in Vercel? – juliomalves Mar 13 '22 at 18:59
  • I don't believe it's an API set up problem, getServerSideProps is just never called on Vercel. – Yeon Mar 14 '22 at 20:55

1 Answers1

0

I had the same issue. And the reason was in headers I sent with API requests

Vadim Khamzin
  • 13
  • 1
  • 3
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '22 at 02:06