Explanation about using Pages Router in Next.js and applications architecture

nextjs pages router building application rendering
10 November 2024

Next.js is one of the most popular web development frameworks developed by Vercel. This framework is particularly ideal for creating modern applications due to its strong capabilities and responsiveness. One of the key features of this framework is the ability to use Pages Router, which helps developers manage different web pages easily. The continuation will provide explanations on how to use this feature and how to implement it in Next.js.

When talking about web development, managing various aspects of a site is one of the key components. In Next.js, this is simply done by creating JavaScript files in the pages folder. Each file in this folder is automatically considered as a new route. This feature helps to clearly and organizedly structure the website.

In Next.js, using the Pages Router allows you to easily create static or dynamic pages. Static pages are created using simple routes without parameters, while dynamic pages can take parameters from the URL and pass them to the application. This feature enables pages to have content based on parameters.

Another key capability of Next.js is SSR or Server-Side Rendering, which helps applications to receive their content before sending to the client. This feature improves site speed and functionality and enhances user experience.

Using the Pages Router in Next.js not only simplifies the creation of websites but also assists in maintaining and upgrading applications. With capabilities that this framework offers, you can create pages with appropriate layouts that provide users with an entirely satisfactory experience.

The next code section will practically showcase how to implement Pages Router.


// Example code for creating a simple route in Next.js
// This file is located in the pages folder

// pages/index.js
export default function Home() {
return <div>Home</div>;
}

// Dynamic page using the created route
// pages/blog/[id].js
import { useRouter } from 'next/router';

export default function BlogPost() {
const router = useRouter();
const { id } = router.query;

return <div>Blog post number {id}</div>;
}

Line by Line Explanation

// Example code for creating a simple route in Next.js
This section shows how you can easily create a route.
// This file is located in the pages folder
This note indicates that the file should be placed in the pages folder to be identified as a route.
export default function Home() {
A function named Home is defined which serves as the root route.
return <div>Home</div>;
This code returns an HTML page with the text "Home" displayed.
// Dynamic page using the created route
In this section, the route creation examples will be provided using named files with the created route.
// pages/blog/[id].js
This file creates a dynamic route that utilizes the parameter id.
import { useRouter } from 'next/router';
These instances are imported from the next/router module to access route parameters.
const { id } = router.query;
This line extracts the parameter id from the router object and utilizes it.
return <div>Blog post number {id}</div>;
In this code, a page is displayed with the parameter id retrieved from the sent URL.

FAQ

?

How can I create a new page in Next.js?

?

How do I create a dynamic route?

?

What benefits does Server-Side Rendering provide in Next.js?

?

How can I access URL parameters in Next.js?