Navigating Your Way: Next.js Routing Cheat sheet (App-Dir)
Routing is a crucial aspect of web development, determining how users navigate through your web application. In this guide, we'll explore various routing scenarios using a sample URL structure based on a Next.js application hosted at HTTP://localhost:3000
. We'll discuss different routing scenarios and their significance within the context of Next.js.
Table of Contents
-
Root Route
- Structure:
/app/page.tsx
- URL:
/
- Structure:
-
API Route
- Structure:
/app/api/route.tsx
- URL:
/api
- Structure:
-
Normal Route
- Structure:
/app/about/page.tsx
- URL:
/about
- Structure:
-
Nested API Route
- Structure:
/app/about/route.tsx
- URL:
/api/about
- Structure:
-
Nested Route
- Structure:
/app/user/about.tsx
- URL:
/user/about
- Structure:
-
Dynamic Route
- Structure:
/app/[item]/page.tsx
- URL:
/slug
- Structure:
-
Nested Dynamic Route
- Structure:
/app/item/[item]/page.tsx
- URL:
/item/slug
- Structure:
-
Nested Dynamic Route (Multiple Parameters)
- Structure:
/app/[item]/[id]/page.tsx
- URL:
/slug/123
- Structure:
-
Nested Dynamic Route (Complex)
- Structure:
/app/item/[item]/id/[id]/page.tsx
- URL:
/item/slug/id/123
- Structure:
-
Catch-All Route
- Structure:
/app/about/[…join]/page.tsx
- URLs:
/about/hello/join/with/us
/about/hello/join
/about
(Not Matched)
- Structure:
-
Catch-All Route (Optional)
- Structure:
/app/about/[[…join]]/page.tsx
- URLs:
/about/hello/join/with/us
/about
(Matched)
- Structure:
-
Private Route
- Structure:
/app/_loging/page.tsx
- URL: No URL (Not accessible directly)
- Structure:
-
Group Route (No Effect)
- Structure:
/app/(profile)/(user)/sohanemon/page.tsx
- URL:
/sohanemon
(No specific effect)
- Structure:
-
Parallel Routes
- Structure:
/app/@user/page.tsx
,/app/@footer/page.tsx
- URL:
/
(Both components parallel)
- Structure:
-
Intercept Route
- Structure:
/app/about/(..)details/page.tsx
- URL:
/about
- Structure:
-
Intercept Route (Multiple Intercepts)
- Structure:
/app/about/(..)(..)details/page.tsx
- URL:
/
- Structure:
-
Opengraph Images
- Structure:
/app/about/opengraph-image.png
,/app/about/opengraph-image.ts
,/app/about/twitter-image.png
,/app/about/twitter-image.ts
- URL: OG image for
/about
- Structure:
-
SEO Routes
- Structure:
/app/about/sitemap.xml
,/app/about/sitemap.ts
,/app/about/robots.txt
,/app/about/robots.ts
- URL: Sitemap and robots text for
/about
- Structure:
-
Rerendered Template
- Structure:
/app/template.tsx
- URL:
/
- Structure:
-
Error Routes
- Structure:
/app/error.tsx
- URLs:
/
,/any
,/any/thing
- Structure:
-
Fallback Page
- Structure:
/app/default.tsx
- URL:
/
- Structure:
-
Layout Page
- Structure:
/app/any/layout.tsx
- URLs:
/any
,/any/thing
- Structure:
-
Loading UI
- Structure:
/app/loading.tsx
- URL:
/
- Structure:
-
Not Found UI
- Structure:
/app/not-found.tsx
- URL:
/URL/with/no/route
- Structure:
This guide specifically addresses routing within the context of a Next.js application, which offers dynamic routing capabilities and server-side rendering. Understanding how to configure and utilize Next.js routing effectively is essential for building dynamic and user-friendly web experiences. Whether you're working on a personal project or a complex web application, Next.js routing is a powerful tool to ensure smooth navigation for your users.
Sohan R. Emon
Developer, Learner, Tech Enthusiast