Next.js Routing
Next.js Routing refers to how navigation between pages is handled in a Next.js application. When a user visits a website, routing determines which page is displayed based on a specific URL.
Next.js handles routing by mapping URLs to files and folders within the pages directory. This means that any file inside the pages directory automatically becomes a route, eliminating the need to use a separate Routing library like React Router.
How does the Next.js File-based Routing System work?
Next.js utilizes a file-based routing system that automatically defines the routes of your application based on your project's folder and file structure. The Pages Router, also known as the Pages directory (or folder), is the traditional routing system in Next.js.
When you add a file inside the pages/ directory, Next.js automatically turns the file into a route.
For example:
In a Next.js project, if a file named index.js is placed inside the pages directory, it is automatically routed to the root of the site.
If you add other folders or create subfolders within the pages directory, they become nested routes.
Example
The second Routing system is called the App Router. App Router was introduced in Next.js version 13. It is the recommended system that utilizes the App directory (/app) to define routes, layout, and UI structures using React server components.
If the App directory, each folder represents a route. Inside each folder, a special file named page.js defines the page that renders that route.
Example:
The App Router also introduces other special files that simplify routing, such as
If you have elements like a footer or a header on each page, layout ensures they are persistent across the different routes.
default.js (or jsx, tsx) - This provides a fallback UI for parallel routes (which allows different parts of a page to render independently) when no specific child route is matched.
global-error.js (or jsx, tsx) - This file defines a global error UI that catches and handles errors across your Next.js application, ensuring users get a clear error message when something in the application goes wrong
error.js (or jsx, tsx) - This file is used to display an error message in the UI if an error occurs.
not-found.js (or jsx, tsx) - This is used to define 404 pages.
loading.js (or jsx, tsx) - This is used to add loading states for pages or routes.
template.js (or jsx, tsx) - This file wraps a layout or a page, and can remount its child components when route parameters change, for example, whenever a user navigates back and forth between routes.
Routing in Next.js
Let’s see how Routing works in Next.js using a few examples.
Prerequisite: To get started, you should have a basic understanding of how to create a Next.js app and set it up in the VSCode environment
Set up the project
Install Next.js by running the following command:
npx create-next-app@latest
During setup, you'll see prompts allowing you to choose your project's requirements, such as whether to use JavaScript or TypeScript, whether to use the App router (which is recommended), and whether to install Tailwind CSS, among other options.
After choosing your preferences, create-next-app will automatically create a new folder with your project name and install all the required dependencies, as shown in the image below.