How the NextJS app router works

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

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/09/2024 at 10:30
Last Updated on 11/13/2024 at 11:33
<p>A flower that represents NextJS with the text "App Router" beneath it.</p>

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

SubscribeDownload Open Source Project

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

NextJS: PagesNextJS: Layouts and Templates

Structure

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

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

<p>A screenshot of the Root Layout of the student archive.</p>

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

<p>A screenshot of VSCode showing the home page for the student archive.</p>

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

<p>NextJS's Route Segments example.</p>

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

<p>A screenshot of the NextJS documentation showing how nested layouts work.</p>

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

<p>An example of a nested page with a static path within the student archive project.</p>

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

<p>An example of a nested page with a static path within the student archive project.</p>

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

<p>A screenshot of VSCode showing a dynamic route within the student archive project.</p>

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.

How to add static or dynamic metadata to a NextJS pageHow to add a sitemap to a NextJS websiteHow to create a 404 page in a NextJS projectBuilding Static Sites with Next.js: A Hands-On Tutorial

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

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

Search our Blog

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.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe