Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,210 --> 00:00:04,591
Now that we got the getAllPosts
2
00:00:04,591 --> 00:00:07,760
and getFeaturedPosts functions, let's use them.
3
00:00:07,760 --> 00:00:09,910
For example, on the starting page
4
00:00:09,910 --> 00:00:14,270
so that there we can get rid of that DUMMY_POSTS array
5
00:00:14,270 --> 00:00:17,920
and instead we load posts from our own data source.
6
00:00:17,920 --> 00:00:21,020
And when it comes to fetching data for a page,
7
00:00:21,020 --> 00:00:23,510
we also learned how that works.
8
00:00:23,510 --> 00:00:28,240
We wanna use getStaticProps or getServerSideProps.
9
00:00:28,240 --> 00:00:31,800
In reality, of course, we don't need either of the two.
10
00:00:31,800 --> 00:00:36,430
We could also use useEffect inside of our component
11
00:00:36,430 --> 00:00:40,330
and send an HTTP request to some API.
12
00:00:40,330 --> 00:00:42,880
But that means that the pre-rendered page
13
00:00:42,880 --> 00:00:45,100
won't contain any content,
14
00:00:45,100 --> 00:00:47,210
which isn't great for search engines
15
00:00:47,210 --> 00:00:49,720
and in this specific website here,
16
00:00:49,720 --> 00:00:51,760
we would also have a problem.
17
00:00:51,760 --> 00:00:54,000
We have no API.
18
00:00:54,000 --> 00:00:56,930
Our posts are fetched from the file system.
19
00:00:56,930 --> 00:00:59,800
And we can therefore do that in any code
20
00:00:59,800 --> 00:01:02,050
that runs on the server side
21
00:01:02,050 --> 00:01:04,120
and has access to that file system
22
00:01:04,120 --> 00:01:06,840
but we can't do it from inside our components.
23
00:01:06,840 --> 00:01:09,680
Those don't have access to the file system
24
00:01:09,680 --> 00:01:13,000
and there, we also can't send a request to any API
25
00:01:13,000 --> 00:01:14,570
that grants that access
26
00:01:14,570 --> 00:01:17,290
because we have no such API here.
27
00:01:17,290 --> 00:01:19,400
We could build an API route
28
00:01:19,400 --> 00:01:21,360
but why would we do that?
29
00:01:21,360 --> 00:01:23,650
Then we would be able to fetch data
30
00:01:23,650 --> 00:01:25,670
without having it pre-rendered
31
00:01:25,670 --> 00:01:29,430
and we would still have the search engine disadvantage.
32
00:01:29,430 --> 00:01:32,400
Therefore, it definitely makes most sense here
33
00:01:32,400 --> 00:01:36,160
to add the separate getStaticProps function here.
34
00:01:40,810 --> 00:01:43,800
In here, we wanna get all the featured posts
35
00:01:43,800 --> 00:01:45,400
and that's super simple
36
00:01:45,400 --> 00:01:48,480
because in that lib folder in which we just worked,
37
00:01:48,480 --> 00:01:52,720
in that file there, we export a getFeaturedPosts function,
38
00:01:52,720 --> 00:01:54,770
which does just that.
39
00:01:54,770 --> 00:01:56,730
So here in getStaticProps,
40
00:01:56,730 --> 00:01:58,470
we can get our featuredPosts
41
00:01:58,470 --> 00:01:59,870
by calling getFeaturedPosts.
42
00:02:01,410 --> 00:02:02,900
And you just need to make sure
43
00:02:02,900 --> 00:02:04,380
that you add a import,
44
00:02:04,380 --> 00:02:07,990
importing getFeaturedPosts from that lib folder
45
00:02:07,990 --> 00:02:09,713
and the posts-util file.
46
00:02:11,280 --> 00:02:13,630
If you do that, you've got the featuredPosts here
47
00:02:13,630 --> 00:02:16,030
and then you learned earlier in the course
48
00:02:16,030 --> 00:02:17,770
that in getStaticProps,
49
00:02:17,770 --> 00:02:20,870
we should return an object with a props key
50
00:02:20,870 --> 00:02:24,520
where we then can pass any props of our choice
51
00:02:24,520 --> 00:02:25,890
to the component.
52
00:02:25,890 --> 00:02:27,340
Now, here I guess it makes sense
53
00:02:27,340 --> 00:02:32,340
to set a posts prop, which holds those loaded featuredPosts.
54
00:02:32,770 --> 00:02:36,490
And that's already all we have to do in getStaticProps here.
55
00:02:36,490 --> 00:02:39,940
Nothing else is required for this use case.
56
00:02:39,940 --> 00:02:42,240
Now, we could also be using getServerSideProps
57
00:02:45,850 --> 00:02:48,110
to always fetch all the featuredPosts
58
00:02:48,110 --> 00:02:50,580
when a request reaches this page
59
00:02:50,580 --> 00:02:52,830
but that doesn't make sense here I would say.
60
00:02:52,830 --> 00:02:55,130
It will just slow down this page a lot
61
00:02:55,130 --> 00:02:57,650
because we have to go through all the files
62
00:02:57,650 --> 00:03:00,780
and go through all the posts for every request
63
00:03:00,780 --> 00:03:02,410
without gaining much.
64
00:03:02,410 --> 00:03:06,660
Our posts will probably not change every second.
65
00:03:06,660 --> 00:03:09,476
After all, we have to change our Markdown file
66
00:03:09,476 --> 00:03:11,810
to update them.
67
00:03:11,810 --> 00:03:14,130
So they are fairly stable.
68
00:03:14,130 --> 00:03:17,250
Most posts will probably never change.
69
00:03:17,250 --> 00:03:20,100
So getStaticProps makes sense.
70
00:03:20,100 --> 00:03:23,210
Now, we could add the revalidate key here
71
00:03:23,210 --> 00:03:25,960
to make it clear that getStaticProps
72
00:03:25,960 --> 00:03:30,160
should not just be executed once during the build process
73
00:03:30,160 --> 00:03:34,390
as you learned but also at least once every 100 seconds
74
00:03:34,390 --> 00:03:37,130
or every 60 seconds or whatever you want
75
00:03:37,130 --> 00:03:39,810
for a new incoming request.
76
00:03:39,810 --> 00:03:42,800
This would ensure that if our data changed
77
00:03:42,800 --> 00:03:47,180
after deployment, we still regenerate our posts
78
00:03:47,180 --> 00:03:50,200
and we therefore reflect that latest data
79
00:03:50,200 --> 00:03:53,620
without rebuilding and redeploying.
80
00:03:53,620 --> 00:03:55,500
We could do that but again,
81
00:03:55,500 --> 00:03:58,830
our posts will probably not change that much.
82
00:03:58,830 --> 00:04:00,960
So if you wanna set revalidate
83
00:04:00,960 --> 00:04:04,260
to have that built-in rebuilding step,
84
00:04:04,260 --> 00:04:07,720
you wanna definitely set it to a long duration,
85
00:04:07,720 --> 00:04:12,130
maybe 10 minutes or even longer, like half an hour
86
00:04:12,130 --> 00:04:14,920
and here I will not set it at all,
87
00:04:14,920 --> 00:04:16,750
which means getStaticProps
88
00:04:16,750 --> 00:04:20,670
will only be executed once outside of development,
89
00:04:20,670 --> 00:04:25,670
during development it will be executed for every request
90
00:04:26,220 --> 00:04:27,940
but outside of development,
91
00:04:27,940 --> 00:04:31,360
it will only be executed when we build the application
92
00:04:31,360 --> 00:04:34,180
for production and never thereafter.
93
00:04:34,180 --> 00:04:37,030
And I would say this is absolutely fine here
94
00:04:37,030 --> 00:04:38,663
for this scenario.
95
00:04:39,670 --> 00:04:43,090
Okay, so now with that, we have getStaticProps,
96
00:04:43,090 --> 00:04:44,950
we set the posts prop,
97
00:04:44,950 --> 00:04:46,880
which holds the featuredPosts
98
00:04:46,880 --> 00:04:48,820
and that means that in HomePage,
99
00:04:48,820 --> 00:04:50,330
we should accept props
100
00:04:50,330 --> 00:04:54,370
and then pass props.posts as a value
101
00:04:54,370 --> 00:04:56,980
to the posts prop on FeaturedPosts.
102
00:04:57,990 --> 00:04:59,897
If you now save all of that
103
00:04:59,897 --> 00:05:02,800
and we go back to the starting page,
104
00:05:02,800 --> 00:05:06,830
there we should see one featured post now
105
00:05:06,830 --> 00:05:10,160
and now here, the data is actually coming
106
00:05:10,160 --> 00:05:12,350
from our Markdown file.
107
00:05:12,350 --> 00:05:15,390
So if I change something there like the date
108
00:05:15,390 --> 00:05:18,360
from 15th to 16th,
109
00:05:18,360 --> 00:05:20,410
then since we're in development
110
00:05:20,410 --> 00:05:24,410
and getStaticProps runs for every request,
111
00:05:24,410 --> 00:05:25,960
when we'll see that if I reload,
112
00:05:25,960 --> 00:05:29,320
this switched to 16th as well.
113
00:05:29,320 --> 00:05:32,430
So this data is coming from the Markdown file.
114
00:05:32,430 --> 00:05:34,400
And now we can do something similar
115
00:05:34,400 --> 00:05:36,630
in the All Posts page.
116
00:05:36,630 --> 00:05:39,470
Of course, feel free to try this on your own first.
117
00:05:39,470 --> 00:05:41,310
Here is your chance to pause the video
118
00:05:41,310 --> 00:05:43,310
and try it on your own first.
119
00:05:43,310 --> 00:05:45,823
After a short pause, we'll implement it together.
120
00:05:49,630 --> 00:05:51,300
Were you successful?
121
00:05:51,300 --> 00:05:55,465
Let's implement it together on that All Posts page.
122
00:05:55,465 --> 00:05:58,710
There it's very similar to the starting page.
123
00:05:58,710 --> 00:06:01,300
So here in posts, index.js,
124
00:06:01,300 --> 00:06:03,890
we don't wanna use the DUMMY_POSTS any more,
125
00:06:03,890 --> 00:06:05,990
we wanna get rid of that.
126
00:06:05,990 --> 00:06:10,440
Instead, we wanna fetch posts from our actual data source.
127
00:06:10,440 --> 00:06:12,953
And again, for this, I'll use getStaticProps.
128
00:06:14,251 --> 00:06:17,770
So I'll export a function getStaticProps
129
00:06:17,770 --> 00:06:19,750
to fetch my posts here.
130
00:06:19,750 --> 00:06:23,500
Again, getServerSideProps makes no sense for me
131
00:06:23,500 --> 00:06:27,040
because we don't want to fetch all the posts
132
00:06:27,040 --> 00:06:28,630
for every request.
133
00:06:28,630 --> 00:06:31,630
That would not be that great for performance.
134
00:06:31,630 --> 00:06:34,330
Instead, doing it once during the build process
135
00:06:34,330 --> 00:06:35,720
should be enough.
136
00:06:35,720 --> 00:06:38,290
If we ever update our files thereafter
137
00:06:38,290 --> 00:06:40,540
and we wanna reflect those changes,
138
00:06:40,540 --> 00:06:42,950
we just have to rebuild and redeploy
139
00:06:42,950 --> 00:06:46,690
but we're probably not doing that multiple times every day.
140
00:06:46,690 --> 00:06:48,670
And if we ever add a new post,
141
00:06:48,670 --> 00:06:51,180
since it's part of the overall project folder,
142
00:06:51,180 --> 00:06:53,780
we will need to redeploy anyways.
143
00:06:53,780 --> 00:06:56,660
So that also won't be a problem.
144
00:06:56,660 --> 00:07:00,610
Hence here again I will just call getAllPosts
145
00:07:00,610 --> 00:07:02,960
and make sure to add that import
146
00:07:02,960 --> 00:07:05,273
to the getAllPosts function here.
147
00:07:07,350 --> 00:07:10,010
And this then gives us all the posts
148
00:07:10,010 --> 00:07:12,223
and we then just return an object
149
00:07:12,223 --> 00:07:16,760
with the props key, which holds the props object
150
00:07:16,760 --> 00:07:19,263
where we can set posts to allPosts.
151
00:07:20,420 --> 00:07:22,800
Again, we could add revalidate
152
00:07:22,800 --> 00:07:24,400
but for the reasons explained before,
153
00:07:24,400 --> 00:07:25,820
I'll not do it here
154
00:07:25,820 --> 00:07:27,830
because if we add a new post,
155
00:07:27,830 --> 00:07:30,270
we'll have to redeploy anyways
156
00:07:30,270 --> 00:07:33,150
since it's part of the overall project folder
157
00:07:33,150 --> 00:07:35,361
and if we change an existing post,
158
00:07:35,361 --> 00:07:36,960
we can also redeploy.
159
00:07:36,960 --> 00:07:40,170
We're probably not doing that all the time.
160
00:07:40,170 --> 00:07:42,610
Hence now here in the AllPostsPage,
161
00:07:42,610 --> 00:07:44,350
we get our props now
162
00:07:44,350 --> 00:07:47,089
and we now use props.posts,
163
00:07:47,089 --> 00:07:50,950
since I used posts here as a value for posts
164
00:07:50,950 --> 00:07:55,310
on AllPosts and yes, that is a lot of posts, I know.
165
00:07:55,310 --> 00:07:57,373
But now with that, if we save this,
166
00:07:58,240 --> 00:08:00,080
and reload the All Posts page,
167
00:08:00,080 --> 00:08:01,990
we only got one post left here
168
00:08:01,990 --> 00:08:04,300
and that is that one single post
169
00:08:04,300 --> 00:08:07,080
for which we added a Markdown file.
170
00:08:07,080 --> 00:08:10,090
Let's now work on the single post page
171
00:08:10,090 --> 00:08:12,590
and let's fetch the actual post data
172
00:08:12,590 --> 00:08:14,880
from the Markdown file here as well
173
00:08:14,880 --> 00:08:17,783
and let's make sure that everything is output correctly.
13234
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.