Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.JS: How to make ALL requests server-side

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 to send all requests from the server-side, not the user's client.

I have been working with and grokking getInitialProps, but I am not confident that it is the full solution that I need because of this line in the README:

For the initial page load, getInitialProps will execute on the server only. getInitialProps will only be executed on the client when navigating to a different route via the Link component or using the routing APIs.

It seems that getInitialProps is designed for the initial page load, and not for subsequent server-side data fetching.

How can I design my Next.JS app in such a way that all requests come from the server-side?

Notes:

  • It is OK that every request essentially results in an initial page load.
  • It is OK for the user Client to talk back to the Node (Next.JS) server since it's publicly-exposed. I am currently experimenting with wrapping Next.JS in an express server.

Ty in advance for any help

like image 255
AuthorOfTheSurf Avatar asked Apr 20 '18 22:04

AuthorOfTheSurf


People also ask

Is Next.js server-side rendering by default?

By default, Next.js pre-renders every page. This means that Next.js generates HTML for each page in advance, instead of having it all done by client-side JavaScript. Pre-rendering can result in better performance and SEO. Each generated HTML is associated with minimal JavaScript code necessary for that page.

Is Next.js server-side or client-side?

Next. js is used for server side rendering of react application . React along with other framework like angular and vue. js are traditional client side framework ,they run in browser but there are technology to run this framework on server side, and next.

What is difference between getInitialProps and getServerSideProps?

Is there a difference between getInitialProps and getServerSideProps ? The main difference between the legacy getInitialProps and the newer getServerSideProps is how the function is used during transitions, when users click on a Link to visit different parts of your site.

Can I use Next.js without server-side rendering?

Next. js claims to support ES2020 dynamic import() for JavaScript. It allows you to import JavaScript modules dynamically and works collaboratively with SSR as well. You can use the ssr: false object to disable server-side rendering of your dynamically imported component, as shown below.


1 Answers

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:

  • Wrap Next.JS in an express server. Explicitly render pages by calling nextApp.render(...) which happens implicitly in standard Next.JS apps. See server.js
  • Use express routing. Make server-side requests prior to rendering the pages with nextApp.render(...). See server.js.
  • Use standard anchor tags to ensure that pages requests hit the express server. See index.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.js

Suggestions and improvements welcome!

like image 123
AuthorOfTheSurf Avatar answered Oct 21 '22 21:10

AuthorOfTheSurf