πŸ“Œ
My Docs
React
React
  • General Notes
    • index
    • Review-Of-Previous-Concepts
    • Reiew
    • Spread and Rest
    • Understanding By Example
    • React-Resources
    • Using Web Components in React
    • Thinking in React
    • Hello World
    • REACT ENVIORMENT
    • Components And Props
    • Composition vs Inheritance
    • JSX
    • Advanced
    • Project Examples
    • Node.js versus Next.js - A React Approach
    • Composition vs Inheritance
    • React Components
    • Docs
    • Prerequisites
      • Callbacks
      • Scope
      • Mutability
      • Array-CB-Methods
      • Objects
      • Glossary
    • index
    • Question Set #1:
    • Website
    • Editor Setup
    • Quick Start
    • JavaScript in JSX with Curly Braces
    • Your First Component
    • Reducer
    • Passing Data Deeply with Context
    • Manipulating the DOM with Refs
    • Rendering Lists
    • Choosing the State Structure
    • Tips
  • Udemy React & Redux
    • JSX
    • Modern+React+With+Redux
    • Examples
  • Articles
    • Introduction to React for Complete Beginners
    • A Comprehensive Deep Dive into React
    • Super Simple Intro To React
    • Basic React Tutorial
  • REACT DOCS
    • Shallow Compare
    • Performance Tools
    • Keyed Fragments
    • Test Utilities
    • Code-Splitting
    • Introducing Concurrent Mode (Experimental)
    • Components and Props
    • Concurrent Mode API Reference (Experimental)
    • Conditional Rendering
    • Suspense for Data Fetching (Experimental)
    • Cross-origin Errors
    • Error Decoder
    • Error Boundaries
    • New React App
    • Passing Functions to Components
    • recommended way to structure React projects?
    • Forms
    • Fragments
    • Getting Started
    • Versioning Policy
    • Add-Ons
    • Rules of Hooks
    • Using the State Hook
    • How to Contribute
    • Introducing JSX
    • JSX In Depth
    • Event Pooling
    • Portals
    • Optimizing Performance
    • React Without ES6
    • SyntheticEvent
    • PureRenderMixin
    • ReactDOMServer
    • Profiler API
    • Test Renderer
    • Refs and the DOM
    • Static Type Checking
    • State and Lifecycle
    • Uncontrolled Components
    • Web Components
    • PureRenderMixin
Powered by GitBook
On this page

Was this helpful?

  1. General Notes

Node.js versus Next.js - A React Approach

PreviousProject ExamplesNextComposition vs Inheritance

Last updated 3 years ago

Was this helpful?

Back in the early days when websites were becoming interactive, JavaScript was introduced. The JS interpreter was present in the browser and did an amazing job of making websites interactive. All the computations were done on the local machine using in the in-built JS Engine. The makers of JS took JS which is confined to the browser and made it available to the local machine. With this change, a lot of new features were unlocked.

  • Access Local files

  • Monitor Networks

  • Listen to HTTP requests

  • Access Database directly

There are two types of applications built with Node.js. The first one are utility applications which are used to enhance the performance of the main application. For example, hot-reload functionality is a utility function. While coding, the changes are made as we code along. The second type of applications built with Node.js are web servers that handle requests from other sites to provide software services.

Next.js

The official website talks about why Next.js was introduced. Let us discuss the reasons in detail to get a perspective about Next.js.

  • In his , Guillermo Rauch talks about the seven principles for creating rich web applications. In short, they are

  1. Pre-rendering pages for optimal performance

  2. User-Input must be dealt with immediately: For example, when we type into the Google search box, results are displayed instantly without any reloading. AJAX, which stands Asynchronous JavaScript And XML, is an example of this.

  3. Let the user know about changes in data on the server without asking. This ensures minimal reloading and maximum data throughout the screen.

  4. Ensuring proper data exchange with the server: Libraries like Axios, Node-fetch, etc. have enhanced the level of control we have over network calls. Therefore, taking full advantage of the network calls to maximize user experience is necessary. Error handling, data synchronization, and offline caches are essential for optimal levels of data exchange with the server irrespective of network conditions.

  5. Consider the problem of infinite pagination. Since web browsers don’t store history, the problem of navigation is a significant one. User’s don’t expect changes when visiting the previous pages. Therefore maintaining custom local caches is essential for fast feedback.

  6. Push code updates: Data handling and API calls must be handled carefully. Changes in data should correspond to changes in code as well. This is predominantly done to avoid errors during data fetch.

  7. Predict user’s behavior: Understand patterns and have mechanisms for predicting the final user input.

  • Building a PHP like system with the convenience and vast resources of Node.js.

  • Enabling easy-to-setup server-side rendering for React. Node.js does offer support for server-side rendering, but the setup is tedious and involves a steep learning curve.

Server Side Rendering vs. Client-Side Rendering

The two diagrams mentioned below detail the processes involved in server-side rendering and client-side rendering. We observe that server-side rendered applications are one step ahead of client-side rendered applications. They display the HTML and then load the JS files, therefore enhancing the user experience in terms of performance and loading time. It also falls in line with the 7 principles listed above.

Node.js provides a very vast framework and includes many libraries to support every need a developer might come across. Next.js enhances the user experience by introducing server-side rendering with a lower threshold for developers. Using the existing React knowledge, developers can build SSR applications.

Node.js vs. Next.js

  1. We use create-react-app for building React applications with Node.js. On the contrary, create-next-app(CNA) is used for building applications with Next.js.

  2. Discussing the differences between the frameworks should begin with the directory structures. create-react-app(CRA) generates the following directory structure.

nodesampleproject
β”œβ”€β”€ README.md
β”œβ”€β”€ node_modules
β”œβ”€β”€ package.json
β”œβ”€β”€ .gitignore
β”œβ”€β”€ public
β”‚   β”œβ”€β”€ favicon.ico
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ logo192.png
β”‚   β”œβ”€β”€ logo512.png
β”‚   β”œβ”€β”€ manifest.json
β”‚   └── robots.txt
└── src
    β”œβ”€β”€ App.css
    β”œβ”€β”€ App.js
    β”œβ”€β”€ App.test.js
    β”œβ”€β”€ index.css
    β”œβ”€β”€ index.js
    β”œβ”€β”€ logo.svg
    └── serviceWorker.js

whereas, create-next-app generates the directory structure below:

nextsampleproject
β”œβ”€β”€ README.md
β”œβ”€β”€ .next
β”œβ”€β”€ node_modules
β”œβ”€β”€ package.json
β”œβ”€β”€ .gitignore
β”œβ”€β”€ styles
β”‚   β”œβ”€β”€ globals.css
β”‚   β”œβ”€β”€ Home.module.css
β”œβ”€β”€ public
β”‚   β”œβ”€β”€ favicon.ico
β”‚   β”œβ”€β”€ vercel.svg
└── pages
    β”œβ”€β”€ api
        β”œβ”€β”€ hello.js
    β”œβ”€β”€ _app.js_
    β”œβ”€β”€ index.js
  1. The App component in CRA is the main file called Index.js. App.js is the parent component that calls all the child components. On the contrary, in Next.js, the _app.js file enables loading initial props using getInitialProps function. This function offers server-side rendering by preloading the webpage with the initial props.

  2. In create-react-app, we need to run npm run build to make the code production-ready. Next.js offers many functionalities including webpack compiling and code splitting. We can specify our webpack and the babel configs in package.json as well.

  3. Next.js offers react-fast-refresh, which is an enhanced version of React Hot Reloading provided by CRA. It makes the react app reloads very smooth and fast with changes in code. A react-native concept has been included in the Next.js framework.

  4. Next.js offers static file serving. This way all resources like images, CSS files, robots.txt, and global js files can be placed in the public folder, and such resources can be easily called using relative paths. create-react-app does allow static file serving. When it comes to JS files they need to be structured as an external library. Furthermore, they are stored in a global variable referenced within the code.

  5. HTML files received when loading an application built using Next.js are shown in the figure below. The website you are viewing this blog on, is client-side rendered. Let’s do something fun. Press Cltrl+Shift+I to access the developer tools and click on the elements option. You will find a short piece of HTML calling JS files as and when user inputs are received. Review the differences between the HTML file that you observe in the console and the image shown below.

  1. SSR enables enhanced Search-Engine-Optimization. SEO is easily performed using Next because search engines have more data to parse through. This helps websites stand out within search engines.

Conclusion

We have gone through a huge list of differences between Next.js and create-react-app. Although Next.js beats create-react-app applications in most of the cases, create-react-app is the most suitable in cases of CSR Applications. CSR applications might be a necessity when SSR is not an option, for example, when videos are played. Companies like Netflix take advantages of both worlds and designed superior systems. We hope you keep in mind the differences and points mentioned above while designing your applications. Be legendary.

Walmart Labs has written a well detailed on the subject of performance enhancement using server-side rendering. Make sure to check that out. It details the performance enhancement that Next.js enables concerning the create-react-app package.

We have looked at building React applications using create-react-app in a previous two article series. You can review them and .

We will be using create-next-app instead of create-react-app for building next.js applications. Next.js provides a detailed step-by-step tutorial on how to implement a Next.js project. We will focus on the major differences between the two and understand the features in depth.

Next.js is ready for production from the start of the project. Using create-react-app, the deployment process is lengthy. Depending on the type of website, the deployment techniques differ. These can be found in the for create-react-app

Screenshot of the HTML rendered on Server-Side.

blog
blog
Image Source
Image Source
here
here
documentation
documentation