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.
1010
1111For 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
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