Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,050 --> 00:00:05,010
So now we did add static generation
2
00:00:05,010 --> 00:00:09,540
and server side rendering to this mini application here.
3
00:00:09,540 --> 00:00:13,080
What about client-side data fetching though?
4
00:00:13,080 --> 00:00:15,820
Do we need that as well?
5
00:00:15,820 --> 00:00:18,370
To be honest in this application,
6
00:00:18,370 --> 00:00:21,420
I would argue that we don't really need it,
7
00:00:21,420 --> 00:00:23,847
but we could still add it on a page
8
00:00:23,847 --> 00:00:27,220
where I think that it would also be okay.
9
00:00:27,220 --> 00:00:29,450
And that's this filter page,
10
00:00:29,450 --> 00:00:34,450
there we are currently using getServerSideProps, that works,
11
00:00:34,580 --> 00:00:36,910
but I think here client-side site data fetching
12
00:00:36,910 --> 00:00:38,710
could also be okay.
13
00:00:38,710 --> 00:00:41,700
By using that the page would load a bit quicker
14
00:00:41,700 --> 00:00:44,779
because we don't need to pre-render it on the server,
15
00:00:44,779 --> 00:00:48,400
but of course therefore the data would initially be missing.
16
00:00:48,400 --> 00:00:50,400
So we need to show some loading state
17
00:00:50,400 --> 00:00:52,430
until the data is there.
18
00:00:52,430 --> 00:00:55,282
But I think this page would be a fine page
19
00:00:55,282 --> 00:00:57,700
for a client-side data fetching,
20
00:00:57,700 --> 00:01:02,700
because it might be nice to get to this page quickly.
21
00:01:02,730 --> 00:01:06,830
And in addition, it's not that important for search engines.
22
00:01:06,830 --> 00:01:09,785
I mean this filtered list of events,
23
00:01:09,785 --> 00:01:14,040
that's not the main thing search engines should be crawling,
24
00:01:14,040 --> 00:01:17,230
for them, the featured events, all events
25
00:01:17,230 --> 00:01:20,673
and the event details are more relevant.
26
00:01:21,660 --> 00:01:23,410
So therefore I think we could use
27
00:01:23,410 --> 00:01:25,560
client-side data fetching here
28
00:01:25,560 --> 00:01:27,823
on that filtered events page,
29
00:01:28,710 --> 00:01:30,009
that does not mean
30
00:01:30,009 --> 00:01:33,570
that you typically replace getServiceSideProps
31
00:01:33,570 --> 00:01:35,840
with client-side data fetching.
32
00:01:35,840 --> 00:01:40,840
It just happens to fit in quite well in this exact scenario.
33
00:01:41,070 --> 00:01:44,380
Therefore, in this slug page,
34
00:01:44,380 --> 00:01:47,380
I'll comment this code here back in,
35
00:01:47,380 --> 00:01:50,070
because now for a client-side data fetching,
36
00:01:50,070 --> 00:01:54,210
we again need access to the route parameters
37
00:01:54,210 --> 00:01:56,770
inside of this component function
38
00:01:56,770 --> 00:01:59,260
because that is where we are going to write
39
00:01:59,260 --> 00:02:01,310
that data fetching code.
40
00:02:01,310 --> 00:02:03,120
And we definitely, for example,
41
00:02:03,120 --> 00:02:05,310
still need some loading state,
42
00:02:05,310 --> 00:02:07,633
which should eventually be displayed.
43
00:02:08,500 --> 00:02:13,260
Now I wanna do the data fetching with this useSWR hook,
44
00:02:13,260 --> 00:02:15,920
which i showed you in the last course section.
45
00:02:15,920 --> 00:02:19,200
So I'll quit the development server temporarily
46
00:02:19,200 --> 00:02:22,550
and install the SWR package,
47
00:02:22,550 --> 00:02:25,560
which brings us that hook,
48
00:02:25,560 --> 00:02:29,143
and thereafter we can restart the development server.
49
00:02:30,950 --> 00:02:33,680
Now here in the slug page,
50
00:02:33,680 --> 00:02:38,680
we can import useSWR from this SWR package.
51
00:02:38,970 --> 00:02:41,123
And then we can start using it.
52
00:02:41,960 --> 00:02:44,760
Here after I got my filter data,
53
00:02:44,760 --> 00:02:47,940
so after I got the route parameters,
54
00:02:47,940 --> 00:02:52,940
I will call useSWR and then we wanna send a request
55
00:02:54,120 --> 00:02:59,120
and I wanna send a request to firebase for filtered events.
56
00:03:00,180 --> 00:03:04,328
Now again, we can add that filtering logic with firebase,
57
00:03:04,328 --> 00:03:07,400
but since it's not the focus here,
58
00:03:07,400 --> 00:03:11,310
I'll cheat and that will instead fetch all events
59
00:03:11,310 --> 00:03:14,330
and then filter here in this component,
60
00:03:14,330 --> 00:03:16,010
which is of course sub optimal,
61
00:03:16,010 --> 00:03:18,720
but good enough for this example.
62
00:03:18,720 --> 00:03:21,030
So therefore again, I'll grab this link,
63
00:03:21,030 --> 00:03:23,500
which fetches all events
64
00:03:24,620 --> 00:03:29,250
and use that as that key for useSWR.
65
00:03:29,250 --> 00:03:30,850
And that then as you learned,
66
00:03:30,850 --> 00:03:32,680
gives us back an object
67
00:03:32,680 --> 00:03:35,180
where we get access to the fetched data,
68
00:03:35,180 --> 00:03:39,623
and also to a possible error we might be getting.
69
00:03:40,650 --> 00:03:44,470
Now, we also still need to transform the data though
70
00:03:44,470 --> 00:03:45,740
that doesn't change.
71
00:03:45,740 --> 00:03:49,110
This transformation still needs to be applied.
72
00:03:49,110 --> 00:03:52,210
So therefore I'll copy that transformation logic,
73
00:03:52,210 --> 00:03:56,360
and add it to this slug page with help of useEffect,
74
00:03:56,360 --> 00:03:59,530
which we need to import from react.
75
00:03:59,530 --> 00:04:00,780
And whilst we're there,
76
00:04:00,780 --> 00:04:03,340
we can also already import useState
77
00:04:03,340 --> 00:04:05,283
because we'll need that as well.
78
00:04:06,780 --> 00:04:10,007
So now we're here after using useSWR,
79
00:04:11,020 --> 00:04:15,090
I'll execute useEffect and pass an effect function to it.
80
00:04:15,090 --> 00:04:17,899
And as a dependency, I'll add data
81
00:04:17,899 --> 00:04:19,920
because this effect should rerun,
82
00:04:19,920 --> 00:04:23,140
whenever the day that we fetched changes.
83
00:04:23,140 --> 00:04:25,880
Then I'll check if we do have data,
84
00:04:25,880 --> 00:04:29,150
because if we don't, there's nothing we need to do.
85
00:04:29,150 --> 00:04:32,180
And if we do have data, I wanna transform it.
86
00:04:32,180 --> 00:04:34,680
So I paste in my transformation logic
87
00:04:34,680 --> 00:04:39,680
to transform the incoming data into such an events array.
88
00:04:39,890 --> 00:04:42,860
However, that array should not be returned here,
89
00:04:42,860 --> 00:04:45,720
instead it should be set as state,
90
00:04:45,720 --> 00:04:48,070
so we can register some state here,
91
00:04:48,070 --> 00:04:49,723
which is initially undefined.
92
00:04:50,710 --> 00:04:53,920
The events state
93
00:04:57,010 --> 00:05:00,033
with setEvents as a state updating function.
94
00:05:01,100 --> 00:05:04,910
And then in here, I'll call setEvents
95
00:05:04,910 --> 00:05:08,730
and set this equal to my events which I have here.
96
00:05:08,730 --> 00:05:12,040
So to this events array, not events state,
97
00:05:12,040 --> 00:05:13,655
in case this is confusing,
98
00:05:13,655 --> 00:05:16,355
I can also name this loadedEvents and setLoadedEvents.
99
00:05:20,720 --> 00:05:23,010
And then now they are setLoadedEvents
100
00:05:23,010 --> 00:05:25,913
equal to the events array we're generating here.
101
00:05:27,110 --> 00:05:28,810
Now, of course it's very possible
102
00:05:28,810 --> 00:05:31,220
that we don't have any loadedEvents,
103
00:05:31,220 --> 00:05:33,080
especially when this component is rendered
104
00:05:33,080 --> 00:05:34,750
for the first time.
105
00:05:34,750 --> 00:05:37,750
So before fetching data has even started.
106
00:05:37,750 --> 00:05:40,350
So if we not have loadedEvents,
107
00:05:40,350 --> 00:05:43,813
then I wanna show this loading output here.
108
00:05:44,888 --> 00:05:47,880
Otherwise we make it past this if check,
109
00:05:47,880 --> 00:05:50,610
and then in this case,
110
00:05:50,610 --> 00:05:54,040
we get the filtered year and month we convert that
111
00:05:54,040 --> 00:05:58,680
and we can then use that data to extract just events
112
00:05:58,680 --> 00:06:00,350
which you wanna display here.
113
00:06:00,350 --> 00:06:02,630
Again that is sub-optimal,
114
00:06:02,630 --> 00:06:06,022
normally we would want to fetch just the right events,
115
00:06:06,022 --> 00:06:09,220
but since this would be very firebase specific,
116
00:06:09,220 --> 00:06:11,463
we're going to go with this approach here.
117
00:06:12,400 --> 00:06:17,400
So therefore here, I then wanna use my filtering logic here
118
00:06:17,580 --> 00:06:22,580
from inside the get filteredEvents function in api-util,
119
00:06:22,660 --> 00:06:25,023
copy that over and add it here.
120
00:06:27,300 --> 00:06:29,730
So that i have my filteredEvents here
121
00:06:29,730 --> 00:06:32,980
and instead of all events, that's loadedEvents,
122
00:06:32,980 --> 00:06:35,420
so this state we just added
123
00:06:35,420 --> 00:06:38,420
that will then filter the events appropriately.
124
00:06:38,420 --> 00:06:41,750
However here, I don't wanna compare this with year,
125
00:06:41,750 --> 00:06:44,490
but with numYear and here with numMonth,
126
00:06:47,980 --> 00:06:49,793
so with these two constants.
127
00:06:51,130 --> 00:06:52,970
Now we've got the filtered events,
128
00:06:52,970 --> 00:06:55,720
props.hasError will never be a thing
129
00:06:55,720 --> 00:06:58,320
since we're not using server-side rendering anymore,
130
00:06:58,320 --> 00:07:02,670
which is where this prop was coming from before.
131
00:07:02,670 --> 00:07:06,410
Now we won't use it anymore, I'll soon remove it.
132
00:07:06,410 --> 00:07:09,560
So that's no longer something we should check.
133
00:07:09,560 --> 00:07:12,710
Instead here, I wanna bring back this if check,
134
00:07:12,710 --> 00:07:15,680
which we outsourced to get server-side props.
135
00:07:15,680 --> 00:07:18,670
Since we're moving back to client-side data fetching,
136
00:07:18,670 --> 00:07:21,610
I'll bring it back onto the client-side.
137
00:07:21,610 --> 00:07:25,513
So this is my if condition now,
138
00:07:28,109 --> 00:07:29,800
like this,
139
00:07:29,800 --> 00:07:31,940
and it might make sense to check this
140
00:07:31,940 --> 00:07:36,913
before we try to filter, so I'll grab this code here
141
00:07:37,970 --> 00:07:40,290
and actually check this here
142
00:07:40,290 --> 00:07:42,753
before we derive the filtered events.
143
00:07:44,550 --> 00:07:48,173
So if we make it past this check, we can filter for events.
144
00:07:49,100 --> 00:07:51,220
We no longer need that line now.
145
00:07:51,220 --> 00:07:54,370
And then we check if we, do we have filtered events?
146
00:07:54,370 --> 00:07:57,390
Or to be precise, if we don't have filtered events,
147
00:07:57,390 --> 00:07:59,490
in which case we show this fallback
148
00:07:59,490 --> 00:08:04,370
and otherwise we still wanna display the filtered events.
149
00:08:04,370 --> 00:08:08,030
And down there we can now again use numYear and numMonth
150
00:08:08,030 --> 00:08:11,763
since I brought back those values here.
151
00:08:13,640 --> 00:08:14,560
So with all of that,
152
00:08:14,560 --> 00:08:17,200
we should be fetching the data on the client-side
153
00:08:17,200 --> 00:08:19,840
with the useSWR hook.
154
00:08:19,840 --> 00:08:21,870
Now here we might have an error.
155
00:08:21,870 --> 00:08:25,780
So that's also something this error, this http error,
156
00:08:25,780 --> 00:08:29,010
which we might be getting, which we should consider,
157
00:08:29,010 --> 00:08:30,070
and I'll simply check here.
158
00:08:30,070 --> 00:08:35,070
Or if we have an error, then I wanna show this error output.
159
00:08:35,120 --> 00:08:37,453
Of course you could also handle it separately.
160
00:08:38,480 --> 00:08:39,477
And with all that added,
161
00:08:39,477 --> 00:08:42,490
we brought client-side data fetching back.
162
00:08:42,490 --> 00:08:43,780
And that means of course,
163
00:08:43,780 --> 00:08:47,240
that we should now remove getServiceSideProps.
164
00:08:47,240 --> 00:08:50,710
Having both together, doesn't make a lot of sense.
165
00:08:50,710 --> 00:08:53,456
It can make sense to have, getStaticProps
166
00:08:53,456 --> 00:08:55,940
with client-side fetching
167
00:08:55,940 --> 00:08:58,290
so that you have some pre-rendered page
168
00:08:58,290 --> 00:09:01,580
and you then update it with client-side fetching,
169
00:09:01,580 --> 00:09:03,610
but getServiceSideProps
170
00:09:03,610 --> 00:09:05,900
and client-side fetching makes no sense
171
00:09:05,900 --> 00:09:09,000
because getServiceSideProps is re-executed
172
00:09:09,000 --> 00:09:11,310
for every request anyways.
173
00:09:11,310 --> 00:09:14,680
So you are guaranteed to have the latest data there.
174
00:09:14,680 --> 00:09:17,120
So if you use client-side data fetching,
175
00:09:17,120 --> 00:09:18,474
as we are doing it here,
176
00:09:18,474 --> 00:09:20,610
it makes no sense to use that
177
00:09:20,610 --> 00:09:23,300
in conjunction with getServiceSideProps.
178
00:09:23,300 --> 00:09:27,480
At least not if your concern is having up-to-date data.
179
00:09:27,480 --> 00:09:30,910
it could make sense if you need to look into request headers
180
00:09:30,910 --> 00:09:31,904
or anything like that,
181
00:09:31,904 --> 00:09:35,453
but we're not doing that here, so we can comment this out.
182
00:09:36,570 --> 00:09:39,380
And with all that, if we save all the code again,
183
00:09:39,380 --> 00:09:41,180
if I reload this page here
184
00:09:41,180 --> 00:09:45,730
for a filter where I have no events I see that,
185
00:09:45,730 --> 00:09:49,280
if I enter something invalid, I see that.
186
00:09:49,280 --> 00:09:53,513
And if I do a set of valid filter, I see that.
187
00:09:54,443 --> 00:09:57,240
And if I reload, I see loading at that.
188
00:09:57,240 --> 00:10:00,250
And of course, if we now inspect the page source,
189
00:10:00,250 --> 00:10:03,150
this page is now missing the event data,
190
00:10:03,150 --> 00:10:06,060
instead it only contains that loading text,
191
00:10:06,060 --> 00:10:08,230
because when it was pre-rendered,
192
00:10:08,230 --> 00:10:10,190
we didn't pre-fetch the data,
193
00:10:10,190 --> 00:10:13,000
we didn't prepare the data for the pre rendering.
194
00:10:13,000 --> 00:10:16,280
Since I commented out, getServiceSideProps
195
00:10:16,280 --> 00:10:19,150
and therefore all Next.js saw
196
00:10:19,150 --> 00:10:21,380
when it pre-rendered this page,
197
00:10:21,380 --> 00:10:26,380
was in the end, this loading state this year,
198
00:10:26,640 --> 00:10:28,950
because we don't have any loaded events
199
00:10:28,950 --> 00:10:33,950
the first time this component is rendered by Next.js.
200
00:10:34,670 --> 00:10:37,200
Now as explained using client-side data fetching,
201
00:10:37,200 --> 00:10:39,160
is totally optional here.
202
00:10:39,160 --> 00:10:42,100
There's nothing wrong with getServiceSideProps,
203
00:10:42,100 --> 00:10:44,530
but now we also saw that in action again.
204
00:10:44,530 --> 00:10:46,960
And therefore, hopefully it's now clear
205
00:10:46,960 --> 00:10:50,210
how those different data fetching approaches work,
206
00:10:50,210 --> 00:10:53,533
and when you might wanna consider using which.
16362
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.