Best practices for data fetching in a Next.js application with Server-Side Rendering (SSR)?
asked 6 months ago
0 Answers
2 Views
I'm working on a Next.js project and want to implement Server-Side Rendering (SSR) for efficient data fetching. What are the best practices for data fetching in a Next.js application with SSR? How can I ensure that my data is pre-fetched on the server and passed to the client for improved performance and SEO?
// pages/index.js
import React from 'react';
function HomePage({ data }) {
return (
<div>
{/* Render data here */}
</div>
);
}
export async function getServerSideProps() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return {
props: {
data,
},
};
}
export default HomePage;
This is a test question.