I am building a Next.JS app that will be getting data from a Python API and an Postgres Database.
Normally this would be simple, except requirements are such that I need
I found a solution by wrapping Next.JS in Express!
I have pushed a simple example project to GitHub here
The repo has a nice README as well as comments in the code that detail what's going on.
Quick rundown:
nextApp.render(...)
which happens implicitly in standard Next.JS apps. See server.jsnextApp.render(...)
. See server.js.nextApp.render
provides passed values to the page in the context (ctx
) parameter of getInitialProps
. You can make these values available in the pages this.props
by returning them in getInitialProps
. See stars.jsSuggestions and improvements welcome!
I thinks this is done simply with getServerSideProps()
Official Docs. It's as simple as it says really: "If you export an async function called getServerSideProps from a page, Next.js will pre-render this page on each request using the data returned by getServerSideProps"
This is generally a bad idea.
The entire point of an SPA is to prevent full page loads.
You can use getInitialProps
to fetch stuff while on the initial page request, and on subsequent navigations, as it is called for every page.
Next is encouraging you to use an API to talk to the server