NextJs GetServerSideProps after update how can i call?

Viewed 1669

Below I am pulling all the data from the database. But let's say I deleted one piece of data. How can I retrieve the renewed data? How can I run it again?

export async function getServerSideProps() {
  const res = await fetch(`http://localhost:8000/api/getAllShipmentTypes`);
  const shipmentTypes = await res.json();

  return {
    props: { shipmentTypes } // will be passed to the page component as props
  };
}

1 Answers

But let's say I deleted one piece of data. How can I retrieve the renewed data?

I think you will need to define what is the trigger for the deletion, I can think of these two.

  1. Another action user performs on a page.
  2. Some other system modifying the database that this client application shows.

For #1, To the action, say a button click you can use a router object to set the same route again which will run getServerSideProps again

When you request this page on client-side page transitions through next/link or next/router, Next.js sends an API request to the server, which runs getServerSideProps

For #2 - this would be handled by giving the user an option to refetch the data from the server again using a link or router component

Related