Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,040 --> 00:00:05,100
Now, I did mention two extensions
2
00:00:05,100 --> 00:00:09,890
or variations of this concept here.
3
00:00:09,890 --> 00:00:12,040
And the nested dynamic segments
4
00:00:12,040 --> 00:00:15,710
that was one important aspect of routing
5
00:00:15,710 --> 00:00:18,400
in Next.js when it comes to dynamic routes.
6
00:00:18,400 --> 00:00:21,980
The other important variation or extension
7
00:00:21,980 --> 00:00:26,590
is that you can also define collect all routes
8
00:00:26,590 --> 00:00:30,720
or catch all routes, and what does this mean?
9
00:00:30,720 --> 00:00:34,600
Let's say, we also have a blog sub folder
10
00:00:34,600 --> 00:00:38,530
and now we might have different ways of loading blog posts.
11
00:00:38,530 --> 00:00:41,830
For example, we could get just the slug,
12
00:00:41,830 --> 00:00:44,060
just the idea of a blog post.
13
00:00:44,060 --> 00:00:45,660
So therefore, of course, in there
14
00:00:45,660 --> 00:00:48,850
we could add a file named [id].js.
15
00:00:48,850 --> 00:00:52,540
But maybe we also want to support other URLs
16
00:00:52,540 --> 00:00:55,300
for loading a single blog post.
17
00:00:55,300 --> 00:01:00,300
For example, we want a support /blog/the-id-of-a-post
18
00:01:02,910 --> 00:01:05,327
but maybe we also want to support
19
00:01:05,327 --> 00:01:10,327
/blog/2020/12/the-idea-of-a-post.
20
00:01:12,610 --> 00:01:14,900
To only search for this post
21
00:01:14,900 --> 00:01:18,640
if it was released in December 2020.
22
00:01:18,640 --> 00:01:23,430
Or we just want to load blog/2020/12
23
00:01:23,430 --> 00:01:28,430
to load all blog posts for that specific month in that year.
24
00:01:28,740 --> 00:01:32,650
So, we have different URL formats which we want to support,
25
00:01:32,650 --> 00:01:34,750
and maybe we want to support them all
26
00:01:34,750 --> 00:01:38,700
with the same component, so no matter what the path is
27
00:01:38,700 --> 00:01:40,960
and how many segments it has
28
00:01:40,960 --> 00:01:44,860
we always want to load the same component.
29
00:01:44,860 --> 00:01:48,800
And that's what we can do with such a catch all route.
30
00:01:48,800 --> 00:01:51,060
Instead of just using square brackets,
31
00:01:51,060 --> 00:01:54,040
and then any identifier of our choice
32
00:01:54,040 --> 00:01:58,010
we can add a syntax here or a special notation here,
33
00:01:58,010 --> 00:02:00,460
which you might know from JavaScript.
34
00:02:00,460 --> 00:02:02,680
We can add three dots here,
35
00:02:02,680 --> 00:02:05,800
like the spread operator in JavaScript.
36
00:02:05,800 --> 00:02:08,870
And then, again, any name of your choice.
37
00:02:08,870 --> 00:02:11,640
So, that could be ID, that could be slug.
38
00:02:11,640 --> 00:02:15,910
I'll go for slug here but, again, that name is up to you.
39
00:02:15,910 --> 00:02:18,730
The three dots are not though.
40
00:02:18,730 --> 00:02:21,210
This, of course, is no special file name
41
00:02:21,210 --> 00:02:24,080
from the operating system's perspective.
42
00:02:24,080 --> 00:02:27,470
But Next.js will treat it in a special way,
43
00:02:27,470 --> 00:02:30,960
just as it treats all files with square brackets
44
00:02:30,960 --> 00:02:33,900
in the file name in a special way.
45
00:02:33,900 --> 00:02:36,440
So, how does it treat that file?
46
00:02:36,440 --> 00:02:39,930
Well, we still define a React component in there,
47
00:02:39,930 --> 00:02:43,460
so we could have the blog posts page here, for example,
48
00:02:43,460 --> 00:02:45,370
to load all the blog posts.
49
00:02:45,370 --> 00:02:47,910
But then we don't know if we get, for example,
50
00:02:47,910 --> 00:02:50,630
a path with the year and the month,
51
00:02:50,630 --> 00:02:55,610
or just with the year, or with a specific blog post already,
52
00:02:55,610 --> 00:02:57,990
or some other filter criteria,
53
00:02:57,990 --> 00:03:01,133
depending on what want to support in our page.
54
00:03:02,030 --> 00:03:04,000
So, here, I'll return my good old div
55
00:03:04,000 --> 00:03:08,060
with the h1 tag of the blog posts,
56
00:03:08,060 --> 00:03:11,023
and then export this as a default.
57
00:03:11,930 --> 00:03:14,613
And I will already import useRouter here,
58
00:03:16,910 --> 00:03:21,520
useRouter from next/router, so that we can look
59
00:03:21,520 --> 00:03:25,490
into what the next/router gives us here.
60
00:03:25,490 --> 00:03:27,200
So, therefore, I'll get access
61
00:03:27,200 --> 00:03:28,850
to my router by calling useRouter
62
00:03:30,020 --> 00:03:35,020
and then console.log the good old router.query value again,
63
00:03:35,110 --> 00:03:40,110
like this, and if we now do that and save this,
64
00:03:41,080 --> 00:03:46,080
and we go to /blog/2020/12, for example,
65
00:03:47,700 --> 00:03:51,130
you see I do load the blog posts page.
66
00:03:51,130 --> 00:03:53,600
And, here, we get our query object
67
00:03:53,600 --> 00:03:58,600
with that slug property because I chose slug as a name here
68
00:03:58,770 --> 00:04:02,520
after the three dots, but the three dots are not part
69
00:04:02,520 --> 00:04:04,400
of that property name, as you can tell.
70
00:04:04,400 --> 00:04:08,230
And then, here, we get an array now and that's different.
71
00:04:08,230 --> 00:04:12,170
Before we always got a single value, a single string.
72
00:04:12,170 --> 00:04:13,970
Now, it's an array of strings
73
00:04:13,970 --> 00:04:17,450
for the different segments that were caught.
74
00:04:17,450 --> 00:04:20,079
So, here we caught the 2020,
75
00:04:20,079 --> 00:04:24,343
and the 12 segment separated by slashes in the URL.
76
00:04:25,610 --> 00:04:29,850
But because we defined this as a catch all path
77
00:04:29,850 --> 00:04:34,530
with the three dots here, next.js does catch anything
78
00:04:34,530 --> 00:04:38,770
after blog because we're defining this in the blog folder.
79
00:04:38,770 --> 00:04:42,620
Anything after blog is caught by this page component
80
00:04:42,620 --> 00:04:45,820
and is made available as an array
81
00:04:45,820 --> 00:04:47,580
through that slug property
82
00:04:47,580 --> 00:04:50,253
on that query object in the router object.
83
00:04:51,150 --> 00:04:53,470
And now we could be using these values
84
00:04:53,470 --> 00:04:56,870
to send a request to our database, to filter
85
00:04:56,870 --> 00:05:01,850
for blog posts where the year is 2020 and the month is 12.
86
00:05:01,850 --> 00:05:03,350
And, of course, you could encode
87
00:05:03,350 --> 00:05:08,350
whatever you want here, and you get those values.
88
00:05:09,740 --> 00:05:13,600
So, that's how this works and how you can utilize that.
89
00:05:13,600 --> 00:05:15,870
And that's another important feature,
90
00:05:15,870 --> 00:05:18,430
which sometimes can become important,
91
00:05:18,430 --> 00:05:20,790
and which sometimes can be helpful,
92
00:05:20,790 --> 00:05:24,040
since it allows you to write very dynamic paths,
93
00:05:24,040 --> 00:05:29,040
where not just the individual segment values are dynamic
94
00:05:29,640 --> 00:05:33,290
but even the number of segments are dynamic.
95
00:05:33,290 --> 00:05:37,020
And that's not always what you need, but sometimes it is.
96
00:05:37,020 --> 00:05:38,970
And therefore it is important to know
97
00:05:38,970 --> 00:05:42,613
that Next.js supports this syntax as well.
7844
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.