Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,505 --> 00:00:05,140
So let's now fetch our actual post data
2
00:00:05,140 --> 00:00:07,810
for this single post here.
3
00:00:07,810 --> 00:00:09,060
Now, the cool thing is that,
4
00:00:09,060 --> 00:00:12,030
in the lib folder, in the posts-util file,
5
00:00:12,030 --> 00:00:15,200
we already got this get post data function,
6
00:00:15,200 --> 00:00:17,850
and we can basically utilize this.
7
00:00:17,850 --> 00:00:19,400
The only problem we'll have is
8
00:00:19,400 --> 00:00:21,650
that it expects a file name.
9
00:00:21,650 --> 00:00:23,240
It expects a file name,
10
00:00:23,240 --> 00:00:26,840
and, when we load, a single post here,
11
00:00:26,840 --> 00:00:28,940
we'll only have to slug.
12
00:00:28,940 --> 00:00:30,270
Which is the file name,
13
00:00:30,270 --> 00:00:32,093
without the file extension.
14
00:00:33,130 --> 00:00:36,050
So we either, convert this,
15
00:00:36,050 --> 00:00:37,510
slug to a file name,
16
00:00:37,510 --> 00:00:39,300
by adding an extension here,
17
00:00:39,300 --> 00:00:42,200
when reaching out to, get post data,
18
00:00:42,200 --> 00:00:45,610
or we make, get post data more flexible.
19
00:00:45,610 --> 00:00:49,640
We could, for example named this, post identifier,
20
00:00:49,640 --> 00:00:51,840
to make it a bit more generic,
21
00:00:51,840 --> 00:00:54,960
then, we create the post slug,
22
00:00:54,960 --> 00:00:57,940
right away, as a first step,
23
00:00:57,940 --> 00:01:01,570
by taking our post identifier,
24
00:01:01,570 --> 00:01:03,410
and replacing the extension.
25
00:01:03,410 --> 00:01:04,569
And if it doesn't have
26
00:01:04,569 --> 00:01:06,420
that extension, that's no problem.
27
00:01:06,420 --> 00:01:08,750
So if we received just this slug,
28
00:01:08,750 --> 00:01:10,500
replace will just do nothing,
29
00:01:10,500 --> 00:01:12,430
which is all defined.
30
00:01:12,430 --> 00:01:14,690
And then here when we need the file path,
31
00:01:14,690 --> 00:01:17,620
we don't just pass in the post identifier,
32
00:01:17,620 --> 00:01:19,930
but instead, we create a string
33
00:01:19,930 --> 00:01:21,430
with a template literal,
34
00:01:21,430 --> 00:01:23,260
and we use post slug,
35
00:01:23,260 --> 00:01:26,170
which is guaranteed to not have an extension,
36
00:01:26,170 --> 00:01:28,410
and add.md here.
37
00:01:28,410 --> 00:01:30,700
So we always create a file, path
38
00:01:30,700 --> 00:01:33,410
or a file name with extension here.
39
00:01:33,410 --> 00:01:36,140
And if we restructure our code like this,
40
00:01:36,140 --> 00:01:38,110
it should already work.
41
00:01:38,110 --> 00:01:40,780
Now we can call, get post data,
42
00:01:40,780 --> 00:01:43,430
with a file name, with an extension,
43
00:01:43,430 --> 00:01:45,800
or with just a slug.
44
00:01:45,800 --> 00:01:47,930
Therefore now we can export this,
45
00:01:47,930 --> 00:01:49,180
to then use it,
46
00:01:49,180 --> 00:01:51,840
in our single post page.
47
00:01:51,840 --> 00:01:54,970
Because here, I wanna get a single post,
48
00:01:54,970 --> 00:01:57,130
for a given slug.
49
00:01:57,130 --> 00:01:59,630
Now for that, here in post detailed page,
50
00:01:59,630 --> 00:02:01,723
I'll again export, a function,
51
00:02:02,940 --> 00:02:05,950
to get static props function.
52
00:02:05,950 --> 00:02:07,390
Again for the same reasons
53
00:02:07,390 --> 00:02:08,370
as mentioned before,
54
00:02:08,370 --> 00:02:11,080
get (indistinct) I props could be used here,
55
00:02:11,080 --> 00:02:12,800
but isn't better.
56
00:02:12,800 --> 00:02:16,150
And then here we get this, context object.
57
00:02:16,150 --> 00:02:18,850
We get this in all get static props functions,
58
00:02:18,850 --> 00:02:21,160
but we haven't needed it before.
59
00:02:21,160 --> 00:02:22,480
Now we do need it,
60
00:02:22,480 --> 00:02:27,480
because we can get our params, out of context.
61
00:02:27,570 --> 00:02:29,470
That's what you also learned in the course.
62
00:02:29,470 --> 00:02:31,530
Context has a params key,
63
00:02:31,530 --> 00:02:35,090
and that's simply, is, then an object,
64
00:02:35,090 --> 00:02:37,670
with the, concrete values,
65
00:02:37,670 --> 00:02:39,180
of all the dynamic segments
66
00:02:39,180 --> 00:02:40,940
that might lead to this file.
67
00:02:40,940 --> 00:02:41,800
So in this case,
68
00:02:41,800 --> 00:02:42,860
we'll be able to,
69
00:02:42,860 --> 00:02:47,260
extract the concrete slug value, from params.
70
00:02:47,260 --> 00:02:49,350
Because we have that dynamic segment,
71
00:02:49,350 --> 00:02:50,803
in this file name.
72
00:02:52,040 --> 00:02:54,580
So here we then have to, slug,
73
00:02:54,580 --> 00:02:56,533
which we get from params.
74
00:02:58,000 --> 00:02:59,650
And now we can use that slug
75
00:02:59,650 --> 00:03:02,030
to get the data for a single post,
76
00:03:02,030 --> 00:03:04,330
with get post data,
77
00:03:04,330 --> 00:03:05,870
so importing that function
78
00:03:05,870 --> 00:03:09,220
from the lib folder into posts-util file,
79
00:03:09,220 --> 00:03:12,050
and passing the slug as our argument.
80
00:03:12,050 --> 00:03:14,770
That will then give us the data, for that file,
81
00:03:14,770 --> 00:03:17,420
it will read that data for that file,
82
00:03:17,420 --> 00:03:19,510
and it's able to read that because,
83
00:03:19,510 --> 00:03:22,540
get static props executes during the build process,
84
00:03:22,540 --> 00:03:25,820
or on the server, not in the client.
85
00:03:25,820 --> 00:03:30,340
And then therefore we get our, post data here.
86
00:03:30,340 --> 00:03:32,820
And hence we can then return our object,
87
00:03:32,820 --> 00:03:34,530
as we always have,
88
00:03:34,530 --> 00:03:37,170
where we set the props for our component.
89
00:03:37,170 --> 00:03:39,150
And here we can add the post prop,
90
00:03:39,150 --> 00:03:41,183
which holds that post data.
91
00:03:42,340 --> 00:03:44,440
And here we could make a case
92
00:03:44,440 --> 00:03:46,910
for adding revalidate.
93
00:03:46,910 --> 00:03:49,980
Here, we're not going through all the posts.
94
00:03:49,980 --> 00:03:52,710
We're fetching the data for a single post.
95
00:03:52,710 --> 00:03:54,680
So this will be very fast.
96
00:03:54,680 --> 00:03:58,580
And if we set revalidate to let's say 600 here,
97
00:03:58,580 --> 00:04:00,040
then we ensure,
98
00:04:00,040 --> 00:04:03,000
that if we ever updated a markdown file,
99
00:04:03,000 --> 00:04:06,210
without rebuilding the entire application,
100
00:04:06,210 --> 00:04:07,810
that then still,
101
00:04:07,810 --> 00:04:11,010
we do get that latest data,
102
00:04:11,010 --> 00:04:13,890
at least once every 10 minutes.
103
00:04:13,890 --> 00:04:14,780
So that we don't have
104
00:04:14,780 --> 00:04:17,190
to rebuild the entire application,
105
00:04:17,190 --> 00:04:19,329
just because we fixed a typo
106
00:04:19,329 --> 00:04:21,550
in one of our markdown files.
107
00:04:21,550 --> 00:04:23,730
We could do this here, because here,
108
00:04:23,730 --> 00:04:25,880
rebuilding after deployment,
109
00:04:25,880 --> 00:04:28,220
will be much faster than if we do it
110
00:04:28,220 --> 00:04:29,700
for the other pages,
111
00:04:29,700 --> 00:04:32,470
where we have to go through all the post files,
112
00:04:32,470 --> 00:04:33,750
which takes a bit longer.
113
00:04:33,750 --> 00:04:35,420
And therefor would slow down,
114
00:04:35,420 --> 00:04:36,670
some of the requests.
115
00:04:36,670 --> 00:04:40,110
Here, it would not slow down the requests at all,
116
00:04:40,110 --> 00:04:41,920
or not by much at least.
117
00:04:41,920 --> 00:04:45,000
And we only do it once every 10 minutes anyways.
118
00:04:45,000 --> 00:04:46,960
Of course you can tweak this duration
119
00:04:46,960 --> 00:04:49,430
to a duration that fits your needs though.
120
00:04:49,430 --> 00:04:51,343
You can make it longer or shorter.
121
00:04:52,780 --> 00:04:55,990
But with that, we now have get static props here.
122
00:04:55,990 --> 00:04:58,760
However, since this is a dynamic page,
123
00:04:58,760 --> 00:05:02,810
you'll learn that get static props can't work on its own.
124
00:05:02,810 --> 00:05:04,440
We need to pair it,
125
00:05:04,440 --> 00:05:06,600
with get static paths,
126
00:05:06,600 --> 00:05:08,440
to let Next know,
127
00:05:08,440 --> 00:05:13,083
which concrete slug values it should pre-generate.
128
00:05:13,980 --> 00:05:14,930
And therefore we need
129
00:05:14,930 --> 00:05:17,460
to export another function here.
130
00:05:17,460 --> 00:05:21,733
We need to export the get static paths function.
131
00:05:23,070 --> 00:05:26,020
This returns an object,
132
00:05:26,020 --> 00:05:28,630
with all the paths that should be prepared.
133
00:05:28,630 --> 00:05:30,360
So with all the concrete values
134
00:05:30,360 --> 00:05:32,460
for a slug that should be prepared.
135
00:05:32,460 --> 00:05:35,000
And that's an array full of objects,
136
00:05:35,000 --> 00:05:36,620
where we set params,
137
00:05:36,620 --> 00:05:38,230
to another nested object,
138
00:05:38,230 --> 00:05:41,040
and then provide our concrete slug values.
139
00:05:41,040 --> 00:05:43,040
That's what you learned.
140
00:05:43,040 --> 00:05:46,450
Now we can, not prepare anything,
141
00:05:46,450 --> 00:05:47,960
and basically just set paths
142
00:05:47,960 --> 00:05:48,860
to an empty array,
143
00:05:50,030 --> 00:05:53,100
and set fallback to true.
144
00:05:53,100 --> 00:05:55,100
Then the data will be prepared
145
00:05:55,100 --> 00:05:56,850
and fetched on the mound,
146
00:05:56,850 --> 00:05:59,170
when we visit this page.
147
00:05:59,170 --> 00:06:00,680
With fallback true,
148
00:06:00,680 --> 00:06:02,770
we should also render some,
149
00:06:02,770 --> 00:06:04,690
fallback content though,
150
00:06:04,690 --> 00:06:08,460
for this scenario that the post hasn't been loaded yet.
151
00:06:08,460 --> 00:06:10,650
Or we set fall back to blocking,
152
00:06:10,650 --> 00:06:13,500
to wait until it has been generated.
153
00:06:13,500 --> 00:06:15,250
This could be fine.
154
00:06:15,250 --> 00:06:17,391
And it could be fine if you have a blog
155
00:06:17,391 --> 00:06:19,630
with, thousands of posts,
156
00:06:19,630 --> 00:06:22,340
where a lot of those posts are,
157
00:06:22,340 --> 00:06:25,210
rarely read and rarely visited,
158
00:06:25,210 --> 00:06:28,410
and you don't wanna pre-generate all those posts.
159
00:06:28,410 --> 00:06:31,770
Then, using a pattern like this could make sense.
160
00:06:31,770 --> 00:06:34,700
Or you pre-generate some of your posts,
161
00:06:34,700 --> 00:06:36,950
your most popular posts,
162
00:06:36,950 --> 00:06:38,920
and not all of them.
163
00:06:38,920 --> 00:06:40,820
In this blog here,
164
00:06:40,820 --> 00:06:43,470
where we will only have a couple of posts,
165
00:06:43,470 --> 00:06:45,110
only a couple of dozens,
166
00:06:45,110 --> 00:06:46,590
or hundreds of posts,
167
00:06:46,590 --> 00:06:49,420
pre-rendering all posts, isn't too difficult
168
00:06:49,420 --> 00:06:50,840
and isn't too much work.
169
00:06:50,840 --> 00:06:53,470
And hence I'll set fall back to false,
170
00:06:53,470 --> 00:06:57,980
and explicitly define all paths in advance.
171
00:06:57,980 --> 00:07:00,370
And for defining all paths in advance,
172
00:07:00,370 --> 00:07:03,540
I need to know, which paths we have.
173
00:07:03,540 --> 00:07:05,930
Now we can get all those paths
174
00:07:05,930 --> 00:07:07,300
by using another function
175
00:07:07,300 --> 00:07:09,800
from the posts-util folder.
176
00:07:09,800 --> 00:07:11,880
They're in get all posts.
177
00:07:11,880 --> 00:07:14,573
We get all our post files.
178
00:07:15,540 --> 00:07:18,360
Then we all to read all the data from there.
179
00:07:18,360 --> 00:07:20,210
Now that's a bit overkill here.
180
00:07:20,210 --> 00:07:22,520
I'm not interested in all the data here.
181
00:07:22,520 --> 00:07:26,480
I'm just interested in getting the post files in this case.
182
00:07:26,480 --> 00:07:30,040
Hence, I will actually refactor this,
183
00:07:30,040 --> 00:07:32,430
and put this here, this code,
184
00:07:32,430 --> 00:07:33,960
where I read all the files
185
00:07:33,960 --> 00:07:35,790
from the posts directory,
186
00:07:35,790 --> 00:07:38,120
into a separate function,
187
00:07:38,120 --> 00:07:40,120
maybe here at the very top,
188
00:07:40,120 --> 00:07:44,173
which I'll name get posts files,
189
00:07:45,310 --> 00:07:48,590
and they are all returned fs readdir sync,
190
00:07:48,590 --> 00:07:50,220
and I'll export this function,
191
00:07:50,220 --> 00:07:53,193
so that we can also use it outside of this file.
192
00:07:54,320 --> 00:07:56,410
And then down there and get all posts.
193
00:07:56,410 --> 00:07:59,380
I'll now call, get posts files,
194
00:07:59,380 --> 00:08:00,943
so that this works again.
195
00:08:02,780 --> 00:08:05,690
So that allows me to just get all the post file names,
196
00:08:05,690 --> 00:08:07,910
without doing all the sorting and mapping,
197
00:08:07,910 --> 00:08:10,360
which will slow down the process a bit,
198
00:08:10,360 --> 00:08:11,790
which is just not needed,
199
00:08:11,790 --> 00:08:15,410
for pre-rendering the individual post files.
200
00:08:15,410 --> 00:08:18,290
So this gives me all the post file names,
201
00:08:18,290 --> 00:08:20,880
with the file extensions included though.
202
00:08:20,880 --> 00:08:22,770
We should keep that in mind,
203
00:08:22,770 --> 00:08:26,030
and therefore in slug JS,
204
00:08:26,030 --> 00:08:29,223
I now can call, get posts files.
205
00:08:30,410 --> 00:08:32,429
So I'll import that function
206
00:08:32,429 --> 00:08:34,120
and call it, from the lib folder
207
00:08:34,120 --> 00:08:36,400
and the posts-util file.
208
00:08:36,400 --> 00:08:39,990
Now this will give me all the posts file names,
209
00:08:39,990 --> 00:08:42,900
and here I'm actually just interested in the slugs.
210
00:08:42,900 --> 00:08:45,970
So to file names without the extensions.
211
00:08:45,970 --> 00:08:48,090
Hence I'll create a slugs constant.
212
00:08:48,090 --> 00:08:50,630
And that's just as post file names,
213
00:08:50,630 --> 00:08:52,920
where I map every file name,
214
00:08:52,920 --> 00:08:55,580
such that I removed the extension.
215
00:08:55,580 --> 00:08:58,860
So I'll just again, replace,
216
00:08:58,860 --> 00:09:00,700
this regular expression,
217
00:09:00,700 --> 00:09:02,260
which we also used here.
218
00:09:02,260 --> 00:09:04,350
So I'll just copy that code in the end,
219
00:09:04,350 --> 00:09:07,363
and replace that with an empty string.
220
00:09:10,190 --> 00:09:15,190
So that now maps all my file names into, just slug strings.
221
00:09:15,250 --> 00:09:17,690
And now we can generate paths here
222
00:09:17,690 --> 00:09:20,440
by going through all the slugs,
223
00:09:20,440 --> 00:09:25,280
and mapping every slug into, an object,
224
00:09:25,280 --> 00:09:27,420
hence the, parentheses around it,
225
00:09:27,420 --> 00:09:29,180
so that, this is not treated
226
00:09:29,180 --> 00:09:30,470
as the function body,
227
00:09:30,470 --> 00:09:33,053
but as an immediately returned object,
228
00:09:33,910 --> 00:09:35,670
where we have the params key,
229
00:09:35,670 --> 00:09:37,720
which holds another nested object,
230
00:09:37,720 --> 00:09:40,993
where we set slug equal to, slug here.
231
00:09:42,830 --> 00:09:45,350
And we could also do this in one step.
232
00:09:45,350 --> 00:09:46,740
I'm doing it in two steps here
233
00:09:46,740 --> 00:09:49,030
to make it a bit more readable.
234
00:09:49,030 --> 00:09:52,460
So this will now generate an array of path objects,
235
00:09:52,460 --> 00:09:55,970
and we are generating all our paths in advance.
236
00:09:55,970 --> 00:09:59,400
So we are pre-generating this, single post page,
237
00:09:59,400 --> 00:10:02,003
for all our posts in advance.
238
00:10:03,070 --> 00:10:04,590
Now this should work.
239
00:10:04,590 --> 00:10:05,830
In get static props,
240
00:10:05,830 --> 00:10:07,820
we return our post prop.
241
00:10:07,820 --> 00:10:09,360
So now we can use this here,
242
00:10:09,360 --> 00:10:12,140
in the post detailed page and get props,
243
00:10:12,140 --> 00:10:15,640
and set a post prop on post content,
244
00:10:15,640 --> 00:10:18,260
and pass props.post into that,
245
00:10:18,260 --> 00:10:20,650
and then go to post content,
246
00:10:20,650 --> 00:10:22,470
to use this post prop instead
247
00:10:22,470 --> 00:10:24,680
of the dummy post there.
248
00:10:24,680 --> 00:10:25,970
So in post content,
249
00:10:25,970 --> 00:10:28,480
I'll get rid of that dummy post,
250
00:10:28,480 --> 00:10:31,590
and instead accept props here.
251
00:10:31,590 --> 00:10:35,520
And then here, we can, first of all,
252
00:10:35,520 --> 00:10:39,380
extract our posts from the props,
253
00:10:39,380 --> 00:10:43,950
and then use, post.slug here,
254
00:10:43,950 --> 00:10:48,950
and post.image, and here, post.title,
255
00:10:49,760 --> 00:10:53,163
and of course here also, post.content.
256
00:10:54,510 --> 00:10:56,100
If we do all of this,
257
00:10:56,100 --> 00:10:58,090
if we save everything,
258
00:10:58,090 --> 00:11:00,358
and reload this page, it should work.
259
00:11:00,358 --> 00:11:02,480
We should see the image, the title
260
00:11:02,480 --> 00:11:04,090
and the actual content
261
00:11:04,090 --> 00:11:07,210
as we wrote it in the markdown file.
262
00:11:07,210 --> 00:11:08,570
Now it is of course up to you,
263
00:11:08,570 --> 00:11:09,940
to then add styling,
264
00:11:09,940 --> 00:11:13,080
for the different content elements you might have.
265
00:11:13,080 --> 00:11:16,540
You can do this with the post content module CSS file.
266
00:11:16,540 --> 00:11:18,570
Here by using the content class,
267
00:11:18,570 --> 00:11:21,290
and then selecting different elements in your content.
268
00:11:21,290 --> 00:11:22,530
You can style them,
269
00:11:22,530 --> 00:11:24,390
and change link colors,
270
00:11:24,390 --> 00:11:26,650
change to display of tables,
271
00:11:26,650 --> 00:11:27,963
whatever you might have.
272
00:11:29,750 --> 00:11:33,700
But with that, we are fetching our actual post data.
273
00:11:33,700 --> 00:11:34,720
And therefore now,
274
00:11:34,720 --> 00:11:36,390
we can click on a post here,
275
00:11:36,390 --> 00:11:38,970
see it, click on it here, see it.
276
00:11:38,970 --> 00:11:40,803
And it just works.
19832
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.