Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

getServerSideProps never runs, or its data never reaches the component

ErrorgetServerSideProps is defined, but the component never receives its data

Short answer

Next.js calls getServerSideProps only when it is exported from a page file in pages/. Exported from a component, a layout or a file in app/, it is ignored. Move it to the page and pass the data down as props, and read its logs in the terminal, not in the browser console.

  • Applies to Next.js 15, Next.js 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every file that exports getServerSideProps. Anything outside pages/ is ignored, so a hit in components/ or app/ already explains the symptom.

    grep -rln "getServerSideProps" . --include="*.js" --include="*.jsx" --include="*.ts" --include="*.tsx" --exclude-dir=node_modules --exclude-dir=.next
  2. 2Match what you see, then jump to its fix

    1. 1grep finds getServerSideProps in components/, lib/ or a hook file, and pages/ has no such exportThe function is exported from a component, not from a pageFix
    2. 2The page shows correct data, but nothing from getServerSideProps appears in DevToolsThe log is being looked for in the browserFix
    3. 3The route is app/.../page.js and an exported getServerSideProps is never calledThe file lives in app/, where the function was replacedFix

What does the error look like?

  • getServerSideProps is defined, but the component never receives its data
  • console.log inside getServerSideProps prints nothing in the browser console
  • A getServerSideProps export in a file under app/ has no effect

Cause 1 of 3

The function is exported from a component, not from a page

1Check

The docs state that getServerSideProps can only be exported from a page. A page is a file in pages/ whose default export is the page component.

grep -rn "export .*getServerSideProps" components pages

2Fix

Move the function into the page file that renders the component, and hand the result to the component as props.

// pages/dashboard.js
import Stats from '../components/Stats'

export async function getServerSideProps() {
  const res = await fetch('https://api.example.com/stats')
  return { props: { stats: await res.json() } }
}

export default function Dashboard({ stats }) {
  return <Stats stats={stats} />
}

3Verify

Request the page and look at the returned HTML. The props passed to the page are part of the initial HTML.

curl -s http://localhost:3000/dashboard

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

The log is being looked for in the browser

1Check

getServerSideProps runs on the server. On a full load that is the Node.js process behind next dev or next start. On a client-side move through next/link or next/router, the browser asks the server to run it. Either way the output goes to the server's terminal.

export async function getServerSideProps(context) {
  console.log('getServerSideProps ran for', context.resolvedUrl)
  return { props: {} }
}

2Fix

Read the terminal where the server runs. To see the data in the browser, log it inside the page component, since props reach the client as part of the initial HTML.

export default function Dashboard({ stats }) {
  console.log('props in the browser', stats)
  return null
}

3Verify

Reload the page and see the first line in the terminal and the second in the browser console.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

The file lives in app/, where the function was replaced

1Check

Inside the app directory, getServerSideProps and getStaticProps are replaced by data fetching in async Server Components.

ls app && grep -rn "getServerSideProps" app

2Fix

Delete the export and fetch inside the page component. Setting cache to no-store makes each request fetch fresh data, which is the closest match to the old behaviour.

// app/dashboard/page.js
export default async function Dashboard() {
  const res = await fetch('https://api.example.com/stats', { cache: 'no-store' })
  const stats = await res.json()
  return <pre>{JSON.stringify(stats)}</pre>
}

3Verify

Open the route and confirm the data renders without any props argument.

npm run dev

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Keep one rule in the team: data functions live in page files, and components only receive props.
  • Look for server-side logs in the terminal first when a function is meant to run on the server.
  • Type the function with satisfies GetServerSideProps, as the Next.js TypeScript example does, so the return shape is checked.
  • Pick one router per route. Use pages/ with getServerSideProps, or app/ with async Server Components, and do not mix their data APIs in one file.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.