Q3: What is getServerSideProps and how does it differ from getStaticProps?
getServerSideProps executes on every incoming request on the server. Unlike getStaticProps (which builds once), it fetches fresh data per request before rendering HTML. Use it when data must be computed at request time (e.g., user-specific dashboards or dynamic search queries).
SSR Data Fetching with getServerSideProps
// pages/profile.js
export async function getServerSideProps(context) {
const { req, res } = context;
const user = await fetchUserFromSession(req);
return {
props: { user },
};
}💡 getServerSideProps receives request context (req, res, query) and computes fresh props per request.
Key Takeaways
- Runs on every HTTP request on the server.
- Slower initial TTFB compared to getStaticProps because HTML is built per request.
Knowledge Check
Which parameter object does getServerSideProps receive to access request headers and cookies?