Skip to main content
The Pages Router provides three server-side data fetching functions and supports client-side fetching via SWR or other libraries.

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 revalidate is set (ISR).
  • On every request during next dev.

Return values

getStaticProps must return an object with one of these shapes:

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 getStaticProps from 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

The fallback field controls what happens when a visitor requests a path that was not pre-generated.
Any path not returned by getStaticPaths returns a 404 page. Use this when you have a small, known set of paths.
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
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 empty paths 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

The context object contains the request and response:

Caching SSR responses

Use Cache-Control headers to cache SSR responses at the CDN level:

Error handling

If getServerSideProps 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 handles caching, revalidation on focus, polling, and error retries. Install it with:

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