#get serverside props
1 messages · Page 1 of 1 (latest)
Hi @surreal meteor. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Next.js, you can use the getServerSideProps function to fetch data on each request and pass it to your page component as props. Here's a basic example:
export default function Page({ data }) {
// Render data...
}
// This gets called on every request
export async function getServerSideProps() {
// Fetch data from external API
const res = await fetch(`<https://.../data`>)
const data = await res.json()
// Pass data to the page via props
return { props: { data } }
}
In this example, getServerSideProps fetches data from an external API and passes it to the Page component as a prop named data. Inside the Page component, you can access this data with props.data or { data } if you're using destructuring.
Remember, getServerSideProps runs on the server on every request, so it's ideal for fetching data that changes often. Also, it can only be exported from a page, and it returns JSON.