Performance: Server-side state passing
This affects the initial page load speed and time to interactive by controlling how much data is sent from server to client and how quickly the page renders.
export async function getServerSideProps() { const data = await fetch('https://api.example.com/summary').then(res => res.json()); return { props: { summary: data.summary } }; } export default function Page({ summary }) { return <div>{summary}</div>; }
export async function getServerSideProps() { const data = await fetch('https://api.example.com/large-data').then(res => res.json()); return { props: { data } }; } export default function Page({ data }) { return <pre>{JSON.stringify(data, null, 2)}</pre>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing large server-side state | No extra DOM nodes | 1 reflow after hydration | High paint cost due to delayed hydration | [X] Bad |
| Passing minimal server-side state | No extra DOM nodes | 1 reflow after hydration | Low paint cost with fast hydration | [OK] Good |
getServerSideProps in Next.js?getServerSideProps rolegetServerSideProps in a Next.js page?getServerSideProps must be an async function exported with the exact name.props key containing data for the page.export async function getServerSideProps() {
return { props: { message: 'Hello from server' } };
}
export default function Page({ message }) {
return <h1>{message}</h1>;
}getServerSideProps passes message as a prop to the page component.message and renders it inside an <h1> tag.getServerSideProps function?export async function getServerSideProps() {
const data = await fetch('https://api.example.com/data');
return data;
}props key containing an object. Here, data is returned directly, which is incorrect.getServerSideProps. Which approach correctly handles the asynchronous fetch and passes the data safely to the page component?props key containing an object with user data.Pass the test and this pattern moves unknown