Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
React Application Architecture for Production

You're reading from  React Application Architecture for Production

Product type Book
Published in Jan 2023
Publisher Packt
ISBN-13 9781801070539
Pages 230 pages
Edition 1st Edition
Languages
Author (1):
Alan Alickovic Alan Alickovic
Profile icon Alan Alickovic

Table of Contents (13) Chapters

Preface 1. Chapter 1: Understanding the Architecture of React Applications 2. Chapter 2: Setup and Project Structure Overview 3. Chapter 3: Building and Documenting Components 4. Chapter 4: Building and Configuring Pages 5. Chapter 5: Mocking the API 6. Chapter 6: Integrating the API into the Application 7. Chapter 7: Implementing User Authentication and Global Notifications 8. Chapter 8: Testing 9. Chapter 9: Configuring CI/CD for Testing and Deployment 10. Chapter 10: Going Beyond 11. Index 12. Other Books You May Enjoy

Next.js application overview

Next.js is a web framework built on top of React and Node.js, allowing us to build web applications. Because it can run on the server, it can be used as a full-stack framework.

Why Next.js?

Using Next.js has multiple benefits. We want to use it because of several reasons:

  • Very easy to get started with: In the early days of React, it was very challenging to start a project. To get a simple page on the screen, we had to deal with many tools such as Webpack, Babel, and others. We still use those tools today, but fortunately, most tooling configuration is hidden from us with an interface to extend the configuration if required.

Besides the challenges of setting up the project, it is very challenging to maintain all those dependencies over time. Next.js hides all those complexities away from developers and allows them to get started quickly with a new project.

  • Allows multiple rendering strategies: Being able to use multiple rendering strategies is probably the main reason why we want to use Next.js, although it comes with other great benefits. First, it allows us to define the behavior of page rendering at the page level, meaning we can define how we want to render each page individually. It also supports multiple rendering strategies, such as the following:
    • Client-side rendering
    • Server-side rendering
    • Static site generation
    • Incremental static regeneration

We will be using different strategies based on the application’s needs.

  • Performance optimizations: Next.js is built with web performance in mind. It implements performance optimization techniques such as the following:
    • Code splitting
    • Lazy loading
    • Prefetching
    • Image optimization

That sums up why we want to use Next.js for our application. Now, let’s see what the Next.js application structure looks like.

Next.js application structure

The easiest way to get started with Next.js is to use the create-next-app CLI to generate a new application.

Since we have already generated the application as part of the code samples, we do not need to use the CLI, but if we were generating the application from scratch, we would execute the following command:

npx create-next-app@latest jobs-app --typescript

By executing this command, we would generate a new Next.js application with TypeScript configured out of the box.

There are a couple of things that are specific to Next.js. Let’s look at the following file and folder structure of a simple Next.js application:

- .next
- public
- src
  - pages
    - _app.tsx
    - index.tsx
- next.config.js
- package.json

Let’s analyze each file and folder one by one:

  • .next: Contains production-ready files generated by running the build command of Next.js.
  • public: Contains all static assets of the application.
  • src/pages: This is a special folder in Next.js where all pages defined here become available at corresponding routes. This is possible thanks to the filesystem-based routing system. The pages folder can also live in the root of the project, but it is nice to keep everything in the src folder.
  • src/pages/_app.tsx: The _app.tsx file is a special file that exports a React component that wraps every page when rendered. By wrapping pages with this special component, we can add custom behavior for our application, such as adding any global configurations, providers, styles, layouts, and more to all the pages.
  • src/pages/index.tsx: This is how we declare pages of the application. This shows how the root page is defined. We will dive into Next.js-specific routing in the upcoming chapters.
  • next.config.js: This is where we can extend the default functionalities such as Webpack configuration and other things in a simple way.
  • package.json: Every Next.js application includes the following npm scripts:
    • dev: Starts a development server on localhost:3000
    • build: Builds the application for production
    • start: Starts the production build on localhost:3000

We will cover more on these topics in the following chapters, but for now, this should give us enough information to get started with Next.js.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}