Next.js Discord

Discord Forum

fetch not working in production

Unanswered
Beveren posted this in #help-forum
Open in Discord
BeverenOP
I got a dynamic page [id] and trying to fetch data there. It works in localhost but after building in vercel its not doing anything. This is the code:

const getItems = async (id: string) => {
  try {
    const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/operator/score/items/questionnaire/${id}`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Basic ${btoa(`${process.env.NEXT_PUBLIC_API_USERNAME}:${process.env.NEXT_PUBLIC_API_PASSWORD}`)}`,
      },
      cache: 'no-store',
    });

    const data = await response.json();

    return data;
  } catch (error) {
    console.error('Error:', error);
  }
};

const ItemsPage = async ({ params }: { params: { id: string } }) => {
  const data = await getItems(params.id);

  return (
    <Page title="Schalen">
      <VerticalMargin size="small">
        <PageHeader title={params.id} />
      </VerticalMargin>
      <MaxWidth>
        <ItemsForm items={data?.items} assesmentId={params.id} />
      </MaxWidth>
    </Page>
  );
};

17 Replies

BeverenOP
@joulev Why is it working on my home page but not on the dynamic page? Im calling a 3rd party API btw
@Beveren <@484037068239142956> Why is it working on my home page but not on the dynamic page? Im calling a 3rd party API btw
so you are fetching not your own api but instead a third party API? the code is the one given above?
BeverenOP
Yes
how does it "not work"? can you describe how it currently does in details?
what happens when you load the page
BeverenOP
So on localhost it just fetches but when I build it on vercel I nothing is happening. I see no fetch in the network tab either
the network tab doesnt have the fetch because the fetch runs on the server not on the browser
BeverenOP
oh yea that makes sense 😅
yeah so the more important thing is, how does it not work? error? or just a blank page? or what happened?
BeverenOP
No error, not a blank page but the items that suppose to show arent showing. It is showing in dev
can add console.log(data) and head to vercel log to see what's the value of data
BeverenOP
where in vercel can I see that log?
there is a Logs tab over here
BeverenOP
Ok I see the error now
thank you!
what was the error?