Skip to content

Instantly share code, notes, and snippets.

@sparrow
Created September 18, 2020 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sparrow/417fbaaa580e1d923f47392c2dd2afae to your computer and use it in GitHub Desktop.
Save sparrow/417fbaaa580e1d923f47392c2dd2afae to your computer and use it in GitHub Desktop.
This code snippet is an example of using getInitialProps to enable server-side rendering for a React application with Next.js. This code snippet is from the article originally published on RubyGarage’s blog: https://rubygarage.org/blog/how-to-integrate-ssr-for-react-app
function Page({ stars }) {
return <div>Next stars: {stars}</div>
}
Page.getInitialProps = async (ctx) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const json = await res.json()
return { stars: json.stargazers_count }
}
export default Page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment