0
0
NextJSframework~10 mins

Nested routes with folders in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested routes with folders
Start: App Router folder
Create folder for route segment
Add page.tsx inside folder
Next.js reads folder as nested route
Render nested page when URL matches
Repeat for deeper nesting
End: Nested routes available
Next.js reads folders inside the app directory as nested routes. Each folder with a page.tsx file becomes a route segment, creating nested URLs.
Execution Sample
NextJS
app/
  dashboard/
    page.tsx
    settings/
      page.tsx
This folder structure creates nested routes: /dashboard and /dashboard/settings.
Execution Table
StepActionFolder/File ProcessedRoute Segment CreatedURL PathRendered Component
1Read app folderapp/root/Root layout or page if exists
2Read folder inside appapp/dashboard/dashboard/dashboarddashboard/page.tsx component
3Read nested folderapp/dashboard/settings/settings/dashboard/settingsdashboard/settings/page.tsx component
4Match URL /dashboardapp/dashboard/page.tsxdashboard/dashboardRender dashboard/page.tsx
5Match URL /dashboard/settingsapp/dashboard/settings/page.tsxsettings/dashboard/settingsRender dashboard/settings/page.tsx
6No more folders---Stop reading routes
💡 All folders with page.tsx files processed, nested routes created accordingly.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
currentRoute//dashboard/dashboard/settings/dashboard/settings
renderedComponentroot layout/pagedashboard/page.tsxdashboard/settings/page.tsxdashboard/settings/page.tsx
Key Moments - 3 Insights
Why does creating a folder inside app with a page.tsx file create a nested route?
Because Next.js treats each folder with a page.tsx as a route segment, building the URL path from folder names as shown in execution_table rows 2 and 3.
What happens if a folder does not have a page.tsx file?
Next.js will not create a route for that folder segment, so no URL path or component is rendered for it, stopping route nesting at that level.
How does Next.js know which component to render for a nested URL?
It matches the URL path segments to folder names and renders the page.tsx inside the matching folder, as shown in execution_table rows 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what component is rendered at step 5 for URL /dashboard/settings?
Aapp/page.tsx
Bdashboard/page.tsx
Cdashboard/settings/page.tsx
Dsettings/page.tsx
💡 Hint
Check the Rendered Component column at step 5 in execution_table.
At which step does Next.js create the route segment for /dashboard?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Route Segment Created column in execution_table.
If the folder app/dashboard/settings/ did not have a page.tsx file, what would happen to the nested route?
AThe /dashboard/settings route would not exist
BNext.js would render dashboard/page.tsx for /dashboard/settings
CNext.js would throw an error
DThe /dashboard/settings route would render an empty page
💡 Hint
Refer to key_moments about folders without page.tsx files.
Concept Snapshot
Next.js nested routes use folders inside the app directory.
Each folder with a page.tsx file creates a route segment.
URL path matches folder names in order.
Nested folders create nested routes.
No page.tsx means no route segment.
This builds clean, nested URLs automatically.
Full Transcript
In Next.js, nested routes are created by organizing folders inside the app directory. Each folder that contains a page.tsx file becomes a segment of the URL path. For example, a folder named dashboard with a page.tsx inside creates the /dashboard route. If inside dashboard there is another folder settings with its own page.tsx, it creates the nested route /dashboard/settings. Next.js reads these folders step-by-step, matching URL paths to folder names and rendering the corresponding page.tsx components. If a folder does not have a page.tsx file, Next.js does not create a route for that segment. This folder-based routing system helps build nested URLs easily and clearly.