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 thebuild
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. Thepages
folder can also live in the root of the project, but it is nice to keep everything in thesrc
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 onlocalhost:3000
build
: Builds the application for productionstart
: Starts the production build onlocalhost: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.