getStaticProps
getStaticProps runs at build time on the server. Use it when the data needed to render a page is available ahead of a user’s request.
pages/index.tsx
When getStaticProps runs
- Always during
next build. - In the background when
revalidateis set (ISR). - On every request during
next dev.
Return values
getStaticProps must return an object with one of these shapes:
| Return | Description |
|---|---|
{ props } | Pass data to the page component. |
{ props, revalidate } | Enable ISR. revalidate is a number of seconds. |
{ notFound: true } | Render the 404 page. |
{ redirect: { destination } } | Redirect to another page. |
Server-only code
getStaticProps never runs on the client. You can import server-only modules and query databases directly:
pages/blog.tsx
Restrictions
- Only export
getStaticPropsfrom a page file. It cannot be used in_app,_document, or non-page files. - It does not have access to the incoming request (no query params, cookies, or headers).
In the App Router, you fetch data directly in async Server Components using
fetch or an ORM. There is no equivalent function to getStaticProps.getStaticPaths
When a page has dynamic routes and uses getStaticProps, you must also export getStaticPaths. It tells Next.js which paths to pre-generate at build time.
pages/posts/[id].tsx
Fallback modes
Thefallback field controls what happens when a visitor requests a path that was not pre-generated.
fallback: false
fallback: false
Any path not returned by
getStaticPaths returns a 404 page. Use this when you have a small, known set of paths.fallback: true
fallback: true
Paths not returned at build time are not 404. Next.js serves a fallback version of the page (you must handle the loading state), then generates and caches the full page in the background.
pages/posts/[id].tsx
fallback: 'blocking'
fallback: 'blocking'
Paths not returned at build time cause Next.js to server-render the page on first request (like SSR), cache it, and serve the cached version for subsequent requests. The user waits but never sees a loading state.
Skipping build-time generation
Return an emptypaths array to generate all pages on-demand. This speeds up builds for large sites:
pages/posts/[id].ts
getServerSideProps
getServerSideProps runs on the server on every request. Use it when the page content depends on request-time data.
pages/profile.tsx
Accessing the request
Thecontext object contains the request and response:
Caching SSR responses
UseCache-Control headers to cache SSR responses at the CDN level:
Error handling
IfgetServerSideProps throws an error, Next.js shows pages/500.js. In development, the error overlay is shown instead.
In the App Router, Server Components fetch data directly using
async/await. The getServerSideProps pattern has no direct equivalent; instead, each component can be async and fetch its own data.Client-side fetching with SWR
For data that does not need to be server-rendered, fetch it on the client with SWR.components/Profile.tsx
SWR with an initial value from getStaticProps
You can combine SSG and client-side fetching. Pass pre-fetched data as the fallbackData option so the page renders immediately, then revalidates in the background:
pages/profile.tsx
