liveBook · Manning

Route segment

As explained in Build AI-Enhanced Web Apps

A portion of an application's route hierarchy associated with a folder and its rendered content.

A route segment is a folder within Next.js's app directory that represents one portion of the URL hierarchy. Adding page.js to that folder makes the route publicly accessible, so folders and URLs mirror each other: app/blog/page.js serves /blog. section 2.8.1

Route segments can also organize rendered content. A layout.js file inside a segment defines UI shared by that segment and its descendants, while nested layouts inherit from the root layout and can add section-specific elements. section 2.8.5

Use route segments to structure pages and shared application chrome:

  • Create app/page.js for the index page at /. section 2.8.1
  • Create app/blog/page.js for /blog. section 2.8.1
  • Create app/blog/[post]/page.js for dynamic post URLs; Next.js exposes the captured value through the params prop. section 2.8.1
  • Place layout.js inside a segment to add shared elements such as blog sidebars or featured content. section 2.8.5
Figure 2.9 File-based routing in Next.js. The root page.js in the app corresponds to the index page (/). The blog page.js in the blog folder corresponds to the blog path (/blog). Finally, the route segment that corresponds to individual articles (wrapped in brackets [ ]) corresponds to the blog post path (/blog/hello).

Compare folders, page.js files, static paths, and bracketed dynamic segments in a route tree. section 2.8.1

Figure 2.10 This diagram shows nested layouts in Next.js. The root layout wraps all content, while the blog layout adds elements specific to the blog section. Individual blog posts are inherited from both.

Inspect how a root layout wraps a section layout and individual pages. section 2.8.5

Chat and blog structure

An app directory can contain a chat route group and a blog segment. The blog page serves /blog, while app/blog/[post]/page.js can render posts such as /blog/hello using the captured post value. A blog layout can add section-specific UI beneath the global root layout. section 2.8.1 section 2.8.5

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains folder conventions, dynamic params, and nested layouts, but stops short of routing internals or advanced route behavior.
Examples
Uses app/blog/page.js, app/blog/[post]/page.js, chat routes, blog sidebars, and root-layout nesting.
What it assumes
Assumes a Next.js app-directory project while teaching folder-based routing and page and layout conventions directly.
What you can do after
Apply it via Next.js conventions to build static pages, dynamic URLs, and nested shared layouts.
How it connects
Connects route segments with file-based routing, dynamic routes, layouts, route groups, and React components.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.