Capítulo 377 de 456
Exporting getServerSideProps from a page makes Next.js prerender it on every request using the returned data, ideal for frequently-changing content.
params, req, res, query, preview/previewData (deprecated for draftMode), draftMode, resolvedUrl, locale, locales, defaultLocale.props: serializable key-value object passed to the page component.notFound: true returns a 404, even overriding a previously successful build.redirect: { destination, permanent } (or statusCode instead of permanent, not both).import type { InferGetServerSidePropsType, GetServerSideProps } from 'next'
type Repo = { name: string; stargazers_count: number }
export const getServerSideProps = (async () => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo: Repo = await res.json()
return { props: { repo } }
}) satisfies GetServerSideProps<{ repo: Repo }>
export default function Page({
repo,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
return <main><p>{repo.stargazers_count}</p></main>
}
getServerSideProps with satisfies and InferGetServerSidePropsType.| Context key | Description |
|---|---|
params | dynamic route params |
req / res | HTTP request/response objects |
query | full query string object |
resolvedUrl | normalized URL, strips _next/data prefix |
locale / locales / defaultLocale | i18n info |
statusCode and permanent on redirect: not supported, choose one.getStaticProps.preview/previewData are deprecated in favor of draftMode.revalidate for ISR.