-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnextjs-routing-dynamic-static.js
More file actions
117 lines (105 loc) · 2.81 KB
/
nextjs-routing-dynamic-static.js
File metadata and controls
117 lines (105 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* 📘 Topic: Static vs Dynamic Routing in Next.js
*
* Next.js provides a powerful file-based routing system.
* Let's understand both static and dynamic routes, how they're defined, and their behavior.
*
* --------------------------------------------------
* 🔹 Static Routes
* Pages automatically created based on file structure under `/pages`.
*
* Example:
* pages/about.js ➜ Route: /about
* pages/contact.js ➜ Route: /contact
*
* --------------------------------------------------
* 🔹 Dynamic Routes
* Created using file name with brackets [].
*
* Example:
* pages/blog/[slug].js ➜ /blog/my-first-post
*
* 📌 You can even nest dynamic routes.
* pages/product/[category]/[id].js ➜ /product/shoes/123
*/
// 👉 Static Page Example (pages/about.js)
export default function About() {
return (
<div>
<h1>About Us</h1>
<p>This is a statically routed page at /about</p>
</div>
);
}
// 👉 Dynamic Page Example (pages/blog/[slug].js)
import { useRouter } from 'next/router';
export default function BlogPost() {
const router = useRouter();
const { slug } = router.query;
return (
<div>
<h1>Blog Post: {slug}</h1>
<p>This page is dynamically routed based on the URL slug.</p>
</div>
);
}
/**
* --------------------------------------------------
* 🔁 Catch-All Routes
* For deeply nested paths, use three dots (...)
*
* File: pages/docs/[...slug].js
* Matches:
* - /docs
* - /docs/setup
* - /docs/setup/install
*/
export function DocsCatchAll() {
const router = useRouter();
const { slug } = router.query;
return (
<div>
<h2>Docs Path: {slug?.join(' / ')}</h2>
</div>
);
}
/**
* --------------------------------------------------
* ❓Fallback in Dynamic Routes
* When using getStaticPaths, set fallback to:
*
* - false: Only paths returned by getStaticPaths will exist (404 otherwise)
* - true: Other paths will be generated at runtime (and cached)
* - 'blocking': Wait until the new page is built (SSR-like experience)
*/
// Example of dynamic page using getStaticPaths
// pages/blog/[slug].js
export async function getStaticPaths() {
return {
paths: [
{ params: { slug: 'my-first-post' } },
{ params: { slug: 'hello-world' } }
],
fallback: false // or true or 'blocking'
};
}
export async function getStaticProps(context) {
const { slug } = context.params;
return {
props: {
slug,
content: `This is the content for ${slug}`
}
};
}
/**
* --------------------------------------------------
* 🧠 Summary:
*
* ✅ Static Routes = Simple files in /pages
* ✅ Dynamic Routes = Named with []
* ✅ Catch-all = [...slug]
* ✅ Fallback = Static Generation + Flexibility
*
* 📦 Next.js automatically handles routing for you, with great DX and flexibility.
*/