Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,090 --> 00:00:04,860
Now let's start with the starting page.
2
00:00:04,860 --> 00:00:06,610
Here on this starting page,
3
00:00:06,610 --> 00:00:09,380
we want the featured events.
4
00:00:09,380 --> 00:00:12,100
So we need to load those featured events
5
00:00:12,100 --> 00:00:14,340
onto this index.js page.
6
00:00:14,340 --> 00:00:16,600
So into this index.js component here,
7
00:00:16,600 --> 00:00:18,630
this homepage component.
8
00:00:18,630 --> 00:00:22,400
The question now is how do we wanna load the data?
9
00:00:22,400 --> 00:00:26,550
Now we could do it with client site data fetching.
10
00:00:26,550 --> 00:00:29,770
We could use useEffect with the fetch API
11
00:00:29,770 --> 00:00:34,070
or install and use the useSWR hook here
12
00:00:34,070 --> 00:00:36,010
which we learned about.
13
00:00:36,010 --> 00:00:39,210
We could do that, but does this make sense?
14
00:00:39,210 --> 00:00:41,970
Is this the best solution here?
15
00:00:41,970 --> 00:00:45,130
I would argue that here we actually wanna use,
16
00:00:45,130 --> 00:00:48,790
one of the two forms of pre-rendering instead.
17
00:00:48,790 --> 00:00:52,740
So static generation or server-side rendering.
18
00:00:52,740 --> 00:00:56,410
Because the starting page, which we're looking at here,
19
00:00:56,410 --> 00:00:59,070
this is a page which should be understood
20
00:00:59,070 --> 00:01:01,120
by search engine crawlers.
21
00:01:01,120 --> 00:01:05,060
It's important that they are able to understand our site
22
00:01:05,060 --> 00:01:08,310
and well, direct traffic to our site.
23
00:01:08,310 --> 00:01:11,650
And as a visitor on this page, it would also be nice
24
00:01:11,650 --> 00:01:13,930
if we see something instantly.
25
00:01:13,930 --> 00:01:15,440
It's also not likely
26
00:01:15,440 --> 00:01:19,050
that this data changes multiple times per second
27
00:01:19,050 --> 00:01:20,250
or minute even.
28
00:01:20,250 --> 00:01:23,650
So there isn't really a reason for loading this
29
00:01:23,650 --> 00:01:25,080
on the client side.
30
00:01:25,080 --> 00:01:27,850
It's also not some user specific data
31
00:01:27,850 --> 00:01:31,310
and it's also not data behind a login screen
32
00:01:31,310 --> 00:01:33,340
or anything like that.
33
00:01:33,340 --> 00:01:36,030
So here I would argue, it makes a lot of sense
34
00:01:36,030 --> 00:01:39,720
to pre-render this page with some data.
35
00:01:39,720 --> 00:01:42,530
That then just leaves one question,
36
00:01:42,530 --> 00:01:44,130
should we use getServersideProps
37
00:01:45,440 --> 00:01:49,700
for real server-side pre-rendering where the page
38
00:01:49,700 --> 00:01:54,700
is pre-rendered on the server on the fly for every request
39
00:01:54,910 --> 00:01:59,250
or do we want getStaticProps to pre-render the page,
40
00:01:59,250 --> 00:02:04,160
during the build process and potentially also on the server
41
00:02:04,160 --> 00:02:07,040
if we set revalidate to a certain value
42
00:02:07,040 --> 00:02:11,630
so that we have a mostly updated page.
43
00:02:11,630 --> 00:02:13,350
I would argue for this kind of page,
44
00:02:13,350 --> 00:02:15,560
we don't need server-side rendering.
45
00:02:15,560 --> 00:02:18,970
We don't need to pre-render it for every request,
46
00:02:18,970 --> 00:02:23,120
instead using getStaticProps makes most sense here,
47
00:02:23,120 --> 00:02:24,310
I would say.
48
00:02:24,310 --> 00:02:27,133
So we can export this async function,
49
00:02:27,133 --> 00:02:32,133
getStaticProps here in this index.js file.
50
00:02:32,160 --> 00:02:36,020
Get our context, though here, we won't need it.
51
00:02:36,020 --> 00:02:37,690
So we can also omit it.
52
00:02:37,690 --> 00:02:38,830
And then in here,
53
00:02:38,830 --> 00:02:43,830
return an object with the props for this homepage component.
54
00:02:45,000 --> 00:02:46,690
Now the props should be an object
55
00:02:46,690 --> 00:02:51,690
and in there I want to have my featured events
56
00:02:51,800 --> 00:02:54,390
which now should be fetched from Firebase,
57
00:02:54,390 --> 00:02:57,940
not from the dummy-data.js file anymore.
58
00:02:57,940 --> 00:03:00,150
We're past this world.
59
00:03:00,150 --> 00:03:02,200
Now we have the Firebase backend
60
00:03:02,200 --> 00:03:04,880
and we wanna fetch that data from there.
61
00:03:04,880 --> 00:03:09,310
Now, how do we fetch the featuredEvents from Firebase?
62
00:03:09,310 --> 00:03:12,370
Actually, when it comes to querying Firebase,
63
00:03:12,370 --> 00:03:15,430
you can adjust the HTTP request
64
00:03:15,430 --> 00:03:18,600
which is being sent with some query parameters
65
00:03:18,600 --> 00:03:21,490
to filter the data you are retrieving.
66
00:03:21,490 --> 00:03:25,250
If you search for Firebase realtime database filter,
67
00:03:25,250 --> 00:03:28,360
you'll find this retrieving data article,
68
00:03:28,360 --> 00:03:31,190
and there you'll learn how you can adjust
69
00:03:31,190 --> 00:03:34,390
to the URL to which you send the request,
70
00:03:34,390 --> 00:03:37,440
with query parameters QL,
71
00:03:37,440 --> 00:03:40,260
filter the data which you are retrieving.
72
00:03:40,260 --> 00:03:42,880
And you can filter by a specific child keys,
73
00:03:42,880 --> 00:03:45,573
look for a certain values and so on.
74
00:03:46,450 --> 00:03:50,030
Now you can do that and feel free to dive into the docs
75
00:03:50,030 --> 00:03:55,020
to implement it such that you do filter on Firebase side
76
00:03:55,020 --> 00:03:57,890
for retrieving only selected data.
77
00:03:57,890 --> 00:03:59,570
I will not do that here,
78
00:03:59,570 --> 00:04:01,800
because this is not a Firebase course.
79
00:04:01,800 --> 00:04:04,530
And it's really just a dummy backend.
80
00:04:04,530 --> 00:04:07,380
Instead, I will add a new file here.
81
00:04:07,380 --> 00:04:11,770
Let's say in a helpers folder,
82
00:04:11,770 --> 00:04:16,680
a file which I'll name api-util.js.
83
00:04:16,680 --> 00:04:18,440
So which has utility functions
84
00:04:18,440 --> 00:04:21,220
for working with an API and in there,
85
00:04:21,220 --> 00:04:23,490
I will now add a function
86
00:04:23,490 --> 00:04:26,180
which gives me the featured events,
87
00:04:26,180 --> 00:04:29,430
but I will actually always fetch all events first
88
00:04:29,430 --> 00:04:32,400
and then filter them just here in JavaScript
89
00:04:33,240 --> 00:04:35,850
which is of course not ideal because it means
90
00:04:35,850 --> 00:04:39,690
that we always fetch all the data, even if we don't need it.
91
00:04:39,690 --> 00:04:42,760
But as I just said, that's just not the focus here,
92
00:04:42,760 --> 00:04:45,100
it would be very Firebase specific
93
00:04:45,100 --> 00:04:48,390
if we write the Firebase query logic here
94
00:04:48,390 --> 00:04:51,160
and that, well, basically leads nowhere.
95
00:04:51,160 --> 00:04:53,690
So therefore I'll go to dummy-data.js
96
00:04:53,690 --> 00:04:58,610
and copy the getFeaturedEvents function from there
97
00:04:58,610 --> 00:05:02,323
and add it here in the api-util.js file.
98
00:05:03,270 --> 00:05:06,830
But I'll also add a number of functions here, getAllEvents.
99
00:05:09,430 --> 00:05:12,180
And in there, I will write the logic
100
00:05:12,180 --> 00:05:16,570
for fetching all events data from Firebase.
101
00:05:16,570 --> 00:05:19,030
So I'll turn this into an async function
102
00:05:19,030 --> 00:05:21,680
so that I can use async await here.
103
00:05:21,680 --> 00:05:23,720
I'll also convert getFeaturedEvents
104
00:05:23,720 --> 00:05:25,700
into an async function.
105
00:05:25,700 --> 00:05:30,700
And then here in getAllEvents, I will call fetch.
106
00:05:31,610 --> 00:05:34,070
So the built-in fetch API,
107
00:05:34,070 --> 00:05:36,060
which is available in the browser
108
00:05:36,060 --> 00:05:40,260
and thanks to next JS also can be used inside
109
00:05:40,260 --> 00:05:42,183
of getStaticProps and getServersideProps.
110
00:05:43,960 --> 00:05:48,940
And here, we'll then send a request to this Firebase URL.
111
00:05:48,940 --> 00:05:53,360
So to this rest API URL, which then triggers Firebase
112
00:05:53,360 --> 00:05:56,110
to talk to our database here.
113
00:05:56,110 --> 00:06:00,640
There we wanna reach out into the events node.
114
00:06:00,640 --> 00:06:02,910
So this node, which I added here
115
00:06:03,970 --> 00:06:05,710
and then because it's Firebase,
116
00:06:05,710 --> 00:06:08,330
we need to add .json at the end here.
117
00:06:08,330 --> 00:06:10,293
That's Firebase specific.
118
00:06:11,250 --> 00:06:14,430
Now, since we use async here,
119
00:06:14,430 --> 00:06:16,390
we can also await this
120
00:06:16,390 --> 00:06:20,163
to get our response like this without then.
121
00:06:21,120 --> 00:06:24,270
Under the hood, async awaits still uses then,
122
00:06:24,270 --> 00:06:25,520
but we can write it
123
00:06:25,520 --> 00:06:28,330
in this more convenient way here.
124
00:06:28,330 --> 00:06:32,380
And we can get our data by awaiting response.json.
125
00:06:34,470 --> 00:06:37,930
We also might want to implement error handling here,
126
00:06:37,930 --> 00:06:40,400
but for the moment, I'll skip it.
127
00:06:40,400 --> 00:06:42,460
Instead here, we then have the data.
128
00:06:42,460 --> 00:06:44,680
And as you learned in the last section,
129
00:06:44,680 --> 00:06:48,290
Firebase returns your data as an object.
130
00:06:48,290 --> 00:06:51,230
So I'll transform my events here.
131
00:06:51,230 --> 00:06:53,550
I'll add them as an empty array
132
00:06:53,550 --> 00:06:56,730
and then loop through my data.
133
00:06:56,730 --> 00:06:58,930
So through all the keys and data,
134
00:06:58,930 --> 00:07:01,520
so through all the entries and events.
135
00:07:01,520 --> 00:07:05,140
So these are my keys, e1, e2 and e3.
136
00:07:05,140 --> 00:07:07,130
And I'll loop through them.
137
00:07:07,130 --> 00:07:09,990
And for every key, I'll push a new event
138
00:07:09,990 --> 00:07:14,800
into this events array, where the id is equal to key
139
00:07:14,800 --> 00:07:19,200
and where I then wanna distribute all of the nested data
140
00:07:19,200 --> 00:07:23,590
which is stored under that key into that events object.
141
00:07:23,590 --> 00:07:26,890
And to not manually write all these key value pairs,
142
00:07:26,890 --> 00:07:28,870
I'll use the spread operator
143
00:07:28,870 --> 00:07:31,830
and just copy everything from data
144
00:07:31,830 --> 00:07:33,380
for the given key.
145
00:07:33,380 --> 00:07:36,480
So from this nested object here
146
00:07:36,480 --> 00:07:40,193
into this object, which we push onto events.
147
00:07:41,970 --> 00:07:45,840
That should ensure that events is an array full of objects,
148
00:07:45,840 --> 00:07:50,640
with all these fields here and with an id field,
149
00:07:50,640 --> 00:07:53,253
which is e1, e2 or e3.
150
00:07:54,490 --> 00:07:58,090
And then I will return events here.
151
00:07:58,090 --> 00:08:00,610
Now, overall, we still return a promise here,
152
00:08:00,610 --> 00:08:01,970
because of async,
153
00:08:01,970 --> 00:08:05,950
but that promise will then resolve to this events array.
154
00:08:05,950 --> 00:08:08,020
And in getFeaturedEvents,
155
00:08:08,020 --> 00:08:10,540
we can now get all events
156
00:08:10,540 --> 00:08:15,070
by awaiting the result of calling, getAllEvents.
157
00:08:15,070 --> 00:08:18,490
So this function I just worked on, I call it here,
158
00:08:18,490 --> 00:08:20,040
since it returns a promise,
159
00:08:20,040 --> 00:08:21,960
I await that promise
160
00:08:21,960 --> 00:08:24,840
which I can because I added async here.
161
00:08:24,840 --> 00:08:28,610
Then I have all events and I return all events,
162
00:08:28,610 --> 00:08:31,950
filtering for the events that are featured.
163
00:08:31,950 --> 00:08:35,870
Again, that means that we always load all events first,
164
00:08:35,870 --> 00:08:38,222
but here for this demo, that is okay.
165
00:08:39,200 --> 00:08:41,397
Now it's getFeaturedEvents,
166
00:08:41,397 --> 00:08:45,060
this getFeaturedEvents, not the one from dummy-data
167
00:08:45,060 --> 00:08:47,780
which I wanna use in index.js.
168
00:08:47,780 --> 00:08:52,780
So here we wanna import getFeaturedEvents from,
169
00:08:56,230 --> 00:08:59,843
and then I'll dive into the helpers folder into api-util.
170
00:09:01,640 --> 00:09:05,280
And now here in getStaticProps,
171
00:09:05,280 --> 00:09:08,380
we can get the featured events
172
00:09:08,380 --> 00:09:13,090
by awaiting the result of getFeaturedEvents.
173
00:09:13,090 --> 00:09:16,760
So of calling this function, which we just worked on
174
00:09:16,760 --> 00:09:18,500
which I now imported.
175
00:09:18,500 --> 00:09:22,220
We can use await because that is an async function.
176
00:09:22,220 --> 00:09:24,250
And then it's these featuredEvents
177
00:09:24,250 --> 00:09:27,873
which I'll set as a value for the featuredEvents prop.
178
00:09:28,760 --> 00:09:31,150
Now we can also name this prop just events.
179
00:09:31,150 --> 00:09:34,160
It doesn't have to be named featuredEvents.
180
00:09:34,160 --> 00:09:36,590
And with that in getStaticProps,
181
00:09:36,590 --> 00:09:40,410
we return an object with our featuredEvents
182
00:09:40,410 --> 00:09:43,710
in props and the featuredEvents ultimately,
183
00:09:43,710 --> 00:09:46,650
are fetched from Firebase.
184
00:09:46,650 --> 00:09:51,240
So now in the component function here, which is executed
185
00:09:51,240 --> 00:09:55,230
after getStaticProps, we now should accept props.
186
00:09:55,230 --> 00:09:56,850
So the props which are set
187
00:09:56,850 --> 00:10:01,470
by getStaticProps, and then here we no longer need
188
00:10:01,470 --> 00:10:03,590
to call getFeaturedEvents.
189
00:10:03,590 --> 00:10:05,700
And we don't wanna call it here,
190
00:10:05,700 --> 00:10:08,510
because we don't wanna fetch them on the client,
191
00:10:08,510 --> 00:10:12,190
instead, I just forward prop.events.
192
00:10:12,190 --> 00:10:16,403
So my events here to EventList.
193
00:10:17,480 --> 00:10:18,800
And now let's see if that works.
194
00:10:18,800 --> 00:10:20,590
If we save that.
195
00:10:20,590 --> 00:10:25,280
If we go back to the starting page and reload,
196
00:10:25,280 --> 00:10:26,320
that's looking good.
197
00:10:26,320 --> 00:10:29,790
I see my events and I can still click on them.
198
00:10:29,790 --> 00:10:33,450
And if I reload and view the page source here,
199
00:10:33,450 --> 00:10:38,450
we see that the event data is actually part
200
00:10:38,970 --> 00:10:40,810
of that HTML file
201
00:10:40,810 --> 00:10:43,350
which was sent back by the server.
202
00:10:43,350 --> 00:10:48,120
So pre-rendering this page with the event data worked.
203
00:10:48,120 --> 00:10:50,820
And here we are using static generation,
204
00:10:50,820 --> 00:10:52,603
because we're using getStaticProps.
205
00:10:53,690 --> 00:10:57,400
So this applies what we learned with Firebase
206
00:10:57,400 --> 00:11:00,483
and with getStaticProps to this homepage.
16105
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.