File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
10
10
11
11
For a steady stream of TILs, [ sign up for my newsletter] ( https://tinyletter.com/jbranchaud ) .
12
12
13
- _ 1022 TILs and counting..._
13
+ _ 1023 TILs and counting..._
14
14
15
15
---
16
16
@@ -461,6 +461,7 @@ _1022 TILs and counting..._
461
461
462
462
### Next.js
463
463
464
+ - [ Create Files And Directories For Dynamic Routes] ( nextjs/create-files-and-directories-for-dynamic-routes.md )
464
465
- [ Define URL Redirects In The Next Config] ( nextjs/define-url-redirects-in-the-next-config.md )
465
466
- [ Remove A Query Param From The URL] ( nextjs/remove-a-query-param-from-the-url.md )
466
467
Original file line number Diff line number Diff line change
1
+ # Create Files And Directories For Dynamic Routes
2
+
3
+ [ Next.js] ( https://nextjs.org/ ) allows you to go beyond static, predefined pages
4
+ and routes with [ dynamic
5
+ routing] ( https://nextjs.org/docs/routing/dynamic-routes ) .
6
+
7
+ The common example is a ` posts ` route that includes a _ slug_ to dynmically
8
+ reference a particular post. The template for that page can be defined at
9
+ ` pages/posts/[slug].js ` . Notice the square brackets around the slug, that tells
10
+ Next that it is a dynamic route and whatever matches against the slug should be
11
+ included in ` router.query ` as ` slug ` .
12
+
13
+ Let's try to create that file:
14
+
15
+ ``` bash
16
+ $ touch pages/posts/[slug].js
17
+ zsh: no matches found: pages/posts/[slug].js
18
+ ```
19
+
20
+ That failed. To create this kind of file from the command-line, you are going
21
+ to need to escape the square brackets:
22
+
23
+ ``` bash
24
+ $ touch pages/posts/\[ slug\] .js
25
+ ```
26
+
27
+ You can do the same if you use dynamic routing in your directory structure:
28
+
29
+ ``` bash
30
+ $ mkdir -p pages/posts/\[ year\] /\[ month\] /\[ day\]
31
+ ```
32
+
33
+ And now we have the following structure:
34
+
35
+ ``` bash
36
+ $ exa --tree pages/posts
37
+ pages/posts
38
+ ├── [slug].js
39
+ └── [year]
40
+ └── [month]
41
+ └── [day]
42
+ ```
You can’t perform that action at this time.
0 commit comments