Skip to content

Commit cd79989

Browse files
committed
Add Create Files And Directories For Dynamic Routes as a nextjs til
1 parent 56cd253 commit cd79989

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1212

13-
_1022 TILs and counting..._
13+
_1023 TILs and counting..._
1414

1515
---
1616

@@ -461,6 +461,7 @@ _1022 TILs and counting..._
461461

462462
### Next.js
463463

464+
- [Create Files And Directories For Dynamic Routes](nextjs/create-files-and-directories-for-dynamic-routes.md)
464465
- [Define URL Redirects In The Next Config](nextjs/define-url-redirects-in-the-next-config.md)
465466
- [Remove A Query Param From The URL](nextjs/remove-a-query-param-from-the-url.md)
466467

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
```

0 commit comments

Comments
 (0)