getServerSideProps vs getStaticProps in Next.js

IntermediateNext.js interview questionBy , updated 17 November 2026

Short answer

Both are Pages Router functions you export from a page file to give it props on the server. getStaticProps runs at build time, so the page is prerendered to HTML once and served from a CDN; adding revalidate regenerates it in the background (ISR). getServerSideProps runs on every request, so it can read cookies, headers and the query string, at the cost of a server render each time.

When each one runs

getStaticProps getServerSideProps
Runs During next build, then in the background when revalidate or on-demand revalidation triggers On every request, including client-side navigations
Output HTML plus a JSON file of props, cacheable on a CDN HTML rendered for that request
Has the request (req, res, cookies) No Yes
Has params Yes, with getStaticPaths for dynamic routes Yes, plus query
Personalised content No, every visitor gets the same page Yes
Typical pages Blog posts, docs, marketing, product pages with ISR Account pages, search results, anything per user
In next dev Runs on every request Runs on every request

Both run only on the server. Their code and imports are removed from the client bundle, so you can query a database directly. On a client-side navigation with next/link, Next.js does not rerun getStaticProps; it fetches the prebuilt JSON. For getServerSideProps it sends a request to the server, which runs the function and returns JSON for the new page.

The code

// pages/posts/[slug].js: static, regenerated at most once a minute
export async function getStaticPaths() {
  const posts = await getPopularPosts();
  return {
    paths: posts.map((p) => ({ params: { slug: p.slug } })),
    fallback: "blocking",
  };
}

export async function getStaticProps({ params }) {
  const post = await getPost(params.slug);
  if (!post) return { notFound: true };
  return { props: { post }, revalidate: 60 };
}

export default function Post({ post }) {
  return <article>{post.title}</article>;
}
// pages/account.js: rendered on every request
export async function getServerSideProps({ req }) {
  const user = await getUserFromSession(req.cookies.session);
  if (!user) {
    return { redirect: { destination: "/login", permanent: false } };
  }
  return { props: { user } };
}

export default function Account({ user }) {
  return <h1>Hello, {user.name}</h1>;
}

Both functions return one of props, redirect or notFound. Only getStaticProps accepts revalidate. notFound: true gives a 404 even if a page for that path was generated earlier, which handles deleted content.

revalidate and ISR

revalidate: 60 means a page can be regenerated at most once every 60 seconds. After that window, the next visitor still gets the cached page, and Next.js runs getStaticProps in the background. When it succeeds, later visitors see the new version; when it fails, the old page keeps being served. For changes you know about, such as a CMS webhook, call res.revalidate('/posts/my-slug') from an API route to regenerate one path on demand. The wider choice between static, regenerated and per-request pages is in SSR vs SSG vs ISR.

getStaticPaths and fallback

A dynamic route like pages/posts/[slug].js needs getStaticPaths to say which paths to build. Its fallback value decides what happens for every other path:

  • false: unknown paths return 404. Good for a small, fixed set of pages; adding one means rebuilding.
  • true: unknown paths get a fallback version of the page at once (empty props, router.isFallback is true), then the real page swaps in when getStaticProps finishes. Crawlers are served as if it were 'blocking'.
  • 'blocking': the first request waits while the page renders on the server, like SSR, then the result is cached for everyone after.

With true or 'blocking' you can build only your most popular pages and let the long tail render on first request, which keeps builds short for large catalogues.

How to choose

Start from getStaticProps. Serving a file from a CDN is faster and cheaper than rendering, and ISR covers most "changes sometimes" content. Move to getServerSideProps only when the page depends on the request: the logged-in user, cookies, headers, or a query string with too many combinations to prebuild. If you only need short-lived caching for an SSR page, you can set a Cache-Control header with s-maxage and stale-while-revalidate on res inside getServerSideProps, though the Next.js docs suggest checking whether ISR fits first.

A page can use only one of them. Exporting both from the same file is a build error.

What replaces them in the App Router

The App Router has no page-level data functions. Server Components are async and fetch their own data, and Next.js decides per route, or per component, what can be prerendered. App Router vs Pages Router covers the wider move.

Pages Router App Router
getStaticProps An async Server Component with no request-time APIs, prerendered at build
getStaticProps with revalidate export const revalidate = 60 or fetch(url, { next: { revalidate: 60 } }); with Cache Components, 'use cache' and cacheLife
getServerSideProps Reading cookies(), headers() or searchParams, or an uncached fetch
getStaticPaths generateStaticParams
fallback: false / true export const dynamicParams = false / true (the default)
res.revalidate(path) revalidatePath, revalidateTag or updateTag

Practise it

Choose a rendering strategy per route for a marketplace is this decision at system design scale, and Design book search end to end pairs a server-rendered results page with CDN caching. Fix a Next.js page that shows stale data and Design a news article page like the New York Times test revalidation and freshness, and Implement SSR hydration mismatch detection covers what happens after the HTML arrives.

Common follow-up questions

Can I use getStaticProps in a component that is not a page?

No. Both functions work only when exported from a page file, not from components, _app, _document or _error. Fetch in the page and pass data down as props.

Why do I get "cannot be serialized as JSON" errors?

Props are sent to the browser as JSON for hydration. A Date, a Map, a class instance or an undefined value cannot be serialised. Convert dates to ISO strings and use null instead of undefined.

Are the props secret?

No. Props are embedded in the HTML so the page can hydrate, and anyone can read them in the page source. Return only what the page needs, never tokens or full database rows.

What about getInitialProps?

It is a legacy API that runs on the server for the first load and in the browser on client navigations. The docs recommend getStaticProps or getServerSideProps instead.

Mistakes to avoid

  • Using getServerSideProps for content that is the same for every visitor, paying for a render on every request

  • Expecting getStaticProps to see cookies or the logged-in user

  • Building every product page at build time with fallback: false, then waiting an hour for each deploy

  • Calling your own API route from getServerSideProps

    It already runs on the server, so call the data source directly.

  • Assuming revalidate: 60 means the page refreshes every minute

    It regenerates only when a request arrives after the window.

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Choose a rendering strategy per route for a marketplaceHard50 min
  2. Design book search end to end: instant suggestions, a server-rendered results page, CDN caching and the search backendHard55 min
  3. Fix a Next.js page that shows stale data, layer by layer through its cachesHard45 min
  4. Design a news article page like the New York Times: ads without layout shift, a metered paywall, speed, SEO and sharingHard55 min
  5. Implement SSR hydration mismatch detection and a reproduction/fix exerciseHard60 min