Skip to content

Instantly share code, notes, and snippets.

@sparrow
Created September 18, 2020 09:35
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/e3637955e31f0d580a3d3b936e5b4b3f to your computer and use it in GitHub Desktop.
Save sparrow/e3637955e31f0d580a3d3b936e5b4b3f to your computer and use it in GitHub Desktop.
This code snippet is an example of using getServerSideProps to enable server-side rendering for a React app 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({ data }) {
// Render data...
}
// This gets called on every request
export async function getServerSideProps() {
// Fetch data from external API
const res = await fetch(`https://.../data`)
const data = await res.json()
// Pass data to the page via props
return { props: { data } }
}
export default Page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment