Created
September 18, 2020 09:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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