Server-side state passing lets your Next.js app send data from the server to the page before it shows up. This helps show fresh info right away without waiting for the browser to ask for it.
Server-side state passing in NextJS
export async function getServerSideProps(context) { // fetch or prepare data here const data = 'some data'; return { props: { data }, // pass data as props to the page }; } export default function Page({ data }) { return <div>{data}</div>; }
getServerSideProps runs on the server for every request.
The returned props object is sent to the page component as props.
export async function getServerSideProps() { return { props: { message: 'Hello from server!' } }; } export default function Page({ message }) { return <h1>{message}</h1>; }
export async function getServerSideProps() { const res = await fetch('https://api.example.com/data'); const data = await res.json(); return { props: { data } }; } export default function Page({ data }) { return <pre>{JSON.stringify(data, null, 2)}</pre>; }
This page shows the current server time when the page loads. The time is fetched on the server and passed as a prop.
export async function getServerSideProps() { const currentTime = new Date().toLocaleTimeString(); return { props: { currentTime } }; } export default function TimePage({ currentTime }) { return ( <main> <h1>Current Server Time</h1> <p>The time when this page was loaded is: {currentTime}</p> </main> ); }
getServerSideProps runs on every request, so it can slow down your page if it does heavy work.
Use server-side state passing when you need fresh data on each page load.
Remember this only works in page components, not in regular React components.
Server-side state passing sends data from the server to the page before it shows.
Use getServerSideProps to fetch or prepare data on the server.
This helps show fresh data immediately and improves SEO.
Practice
getServerSideProps in Next.js?Solution
Step 1: Understand
This function runs on the server before the page loads to fetch or prepare data.getServerSidePropsroleStep 2: Identify its main use
It ensures the page has fresh data on first load and improves SEO by server-side rendering.Final Answer:
To fetch data on the server before rendering the page -> Option DQuick Check:
Server-side data fetching = A [OK]
- Confusing client-side routing with server-side data fetching
- Thinking it styles components
- Assuming it manages cookies directly
getServerSideProps in a Next.js page?Solution
Step 1: Recall correct export syntax
getServerSidePropsmust be an async function exported with the exact name.Step 2: Check return structure
It returns an object with apropskey containing data for the page.Final Answer:
export async function getServerSideProps(context) { return { props: {} } } -> Option BQuick Check:
Correct async export with props = D [OK]
- Using arrow function without export keyword
- Returning object without wrapping props
- Using default export incorrectly
export async function getServerSideProps() {
return { props: { message: 'Hello from server' } };
}
export default function Page({ message }) {
return <h1>{message}</h1>;
}Solution
Step 1: Understand data flow
getServerSidePropspassesmessageas a prop to the page component.Step 2: Render output
The component receivesmessageand renders it inside an <h1> tag.Final Answer:
<h1>Hello from server</h1> -> Option AQuick Check:
Server prop passed and rendered = A [OK]
- Thinking props are undefined without client fetch
- Rendering literal {message} instead of value
- Confusing server message with client message
getServerSideProps function?export async function getServerSideProps() {
const data = await fetch('https://api.example.com/data');
return data;
}Solution
Step 1: Check fetch usage
The fetch is awaited correctly, so no issue there.Step 2: Check return structure
The function must return an object with apropskey containing an object. Here,datais returned directly, which is incorrect.Final Answer:
The returned props must be an object with a props key wrapping data -> Option AQuick Check:
Return { props: { ... } } not just data [OK]
- Returning data directly instead of inside props
- Forgetting to await fetch
- Thinking async is not allowed
getServerSideProps. Which approach correctly handles the asynchronous fetch and passes the data safely to the page component?Solution
Step 1: Handle async data fetching
The function must be async and await the database call to get user data.Step 2: Return data wrapped in props object
The returned object must have apropskey containing an object with user data.Final Answer:
export async function getServerSideProps() { const user = await getUserFromDB(); return { props: { user } }; } -> Option CQuick Check:
Async await + return props object = C [OK]
- Not awaiting async database call
- Returning data directly without props wrapper
- Using non-async function for async fetch
Ready to prove it?
Pass the test and this pattern moves unknown
