How the NextJS app router works

Oscar de la Hera Gomez
A flower that represents NextJS with the text "App Router" beneath it.

A short guide that explains how the NextJS app router works.

The following post serves as a summary for the documentation found in the links below.

Structure

A screenshot of the student archive project in VSCode. It shows the folder structure within the app folder.

The structure for Oscar de la Hera Gomez's student archive.

The structure of a NextJS app router is based on two critical elements: pages and layouts.

Root Layout

A screenshot of the Root Layout of the student archive.

The layout found at the root of the app folder is known as the root layout.

This layout is required and applies to all routes - meaning, on EVERY page, this layout will get called before the page.

"Children" refers to the content of each page, at each route.

Root Page

A screenshot of VSCode showing the home page for the student archive.

A page, which gets rendered as the children of the root layout, represents the UI that is unique to each route.

The page.tsx found at the root of the app folder represents the home page (i.e. "/").

Folders as Routes

NextJS's Route Segments example.

Folders within the app root folder, or sub folders within folders, represent routes of the web page.

These folders must contain a page.tsx file and optionally, can contain layout.tsx file.

The naming convention for these folders dictate how the routes work.

Layout

A screenshot of the NextJS documentation showing how nested layouts work.

The layout within a sub folder would get called and added to the root layout at the top (i.e. where "Layout UI" is found in the image above).

Page

An example of a nested page with a static path within the student archive project.

The page within a folder represents the unique UI of the route.

For more information on static or dynamic routes, read the next two sections.

Folders as Static Paths

An example of a nested page with a static path within the student archive project.

If you wish to use a static path, name the folder after the route that you wish to use (i.e. "cohorts" for /cohorts).

Please note each folder or subfolder requires a page.tsx file.

Folders as Dynamic Paths

A screenshot of VSCode showing a dynamic route within the student archive project.

If you wish to use dynamic paths, use a naming convention such as classes/[slug] or element-[slug]-[id] which would allow for a routes "/classes/[slug]" or "/element-[slug]-[id]" respectively.

These dynamic parameters (i.e. slug and id) will be available within the page and layout of the folder, enabling you to gather content dynamically.

Please note each folder or subfolder requires a page.tsx file.

Looking to add Metadata, a Sitemap, a 404 or Generate Static Websites (SSG)?

Follow our tutorials below to learn how to add metadata to pages, create a sitemap, a 404 or generate static websites.

Looking to learn more about NextJS, ReactJS or web development?

Search our blog to find educational content on NextJS, ReactJS and web development.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details