Functions

Pages getServerSideProps

Request-time server data function. Returned props are exposed to the client. Source: https://nextjs.org/docs/pages/api-reference/functions/get-server-side-props

Formal Syntax & Structure

nextjs syntaxnextjs
export const getServerSideProps: GetServerSideProps = async (context) => ...

Working Example

Example Snippetnextjs
// pages/view.tsximport type { GetServerSideProps, InferGetServerSidePropsType } from 'next';export const getServerSideProps = (async ({req,res}) => { res.setHeader('Cache-Control','private, no-store'); return {props:{view:req.cookies.view==='compact'?'compact':'comfortable'}};}) satisfies GetServerSideProps<{view:string}>;export default function View({view}:InferGetServerSidePropsType<typeof getServerSideProps>) {return <main><h1>View</h1><p>{view}</p></main>;}
Next.js Reference