Currently, I'm working with Next.js and have set up a custom server using Express. One of my pages needs to retrieve data from the database.
When getInitialProps()
is executed on the server side, it easily retrieves the necessary data from the database without any complications. However, when getInitialProps()
runs on the client side (like when a user navigates directly to the page), fetching data from the database isn't an option since we are on the client side. In this scenario, AJAX must be used to communicate with the server and ask it to provide the required information.
Setting up a new Express route on the server to handle this request becomes necessary, but duplicating the server-side logic of getInitialProps()
in this route is far from ideal.
How can I best address this issue?