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:04,550
Now the starting page is working.
2
00:00:04,550 --> 00:00:07,530
What about the event detail page here?
3
00:00:07,530 --> 00:00:10,660
This page currently still fetches it's data
4
00:00:10,660 --> 00:00:13,100
from the dummy-data file.
5
00:00:13,100 --> 00:00:16,920
It's this eventId this dynamic page here.
6
00:00:16,920 --> 00:00:17,870
And of course here
7
00:00:17,870 --> 00:00:21,290
we now also want to get our concrete event
8
00:00:21,290 --> 00:00:24,680
the individual event for which we wanna display data
9
00:00:24,680 --> 00:00:28,113
we wanna get that from our firebase backend.
10
00:00:29,480 --> 00:00:33,100
Now for this, first of all, we can go to dummy-data
11
00:00:33,100 --> 00:00:36,770
and copy the getEventById function
12
00:00:36,770 --> 00:00:41,120
and add this to the api-utiljs file.
13
00:00:41,120 --> 00:00:45,230
Export it there but turn it into an async function
14
00:00:45,230 --> 00:00:47,020
and instead of DUMMY_EVENTS here
15
00:00:47,020 --> 00:00:48,857
we again wanna use AllEvents.
16
00:00:49,810 --> 00:00:52,120
So just as in getfeaturedevents,
17
00:00:52,120 --> 00:00:55,400
I'll get access to all events await this
18
00:00:55,400 --> 00:00:58,170
and then find my single event on the,
19
00:00:58,170 --> 00:01:00,483
well, overall events which I got here.
20
00:01:01,570 --> 00:01:03,200
Now that's step one.
21
00:01:03,200 --> 00:01:08,200
Step two is to use that in the dynamic event ID page.
22
00:01:08,290 --> 00:01:09,970
And again, the question is
23
00:01:09,970 --> 00:01:12,670
how do we wanna fetch the data?
24
00:01:12,670 --> 00:01:15,240
Now we could fetch it on the client side
25
00:01:15,240 --> 00:01:17,450
but that wouldn't make more sense here
26
00:01:17,450 --> 00:01:19,840
than it does on the homepage.
27
00:01:19,840 --> 00:01:22,960
It's still unlikely that it changes a lot.
28
00:01:22,960 --> 00:01:27,960
And this page for an individual event is even more important
29
00:01:28,040 --> 00:01:31,770
when it comes to search engines than the starting page.
30
00:01:31,770 --> 00:01:33,900
After all it's the single event page
31
00:01:33,900 --> 00:01:37,370
that has all the details for an event.
32
00:01:37,370 --> 00:01:39,970
So this should absolutely be crawlable.
33
00:01:39,970 --> 00:01:42,980
We definitely wanna have some data on this page
34
00:01:42,980 --> 00:01:45,210
right from the start.
35
00:01:45,210 --> 00:01:46,180
So therefore here,
36
00:01:46,180 --> 00:01:51,180
I definitely also want to pre-render this page with data.
37
00:01:51,640 --> 00:01:54,570
Again, the question is static generation
38
00:01:54,570 --> 00:01:56,330
with get static props
39
00:01:56,330 --> 00:01:59,880
or server-side rendering with get service props.
40
00:01:59,880 --> 00:02:04,880
And as often the answer probably is get static props.
41
00:02:05,440 --> 00:02:08,380
Because this again is not really a page
42
00:02:08,380 --> 00:02:11,390
where we need users specific data
43
00:02:11,390 --> 00:02:15,010
which changes all the time or anything like that.
44
00:02:15,010 --> 00:02:18,310
So, pre-generating, it sounds good to me.
45
00:02:18,310 --> 00:02:22,170
So in this event, ID page here in this page file
46
00:02:22,170 --> 00:02:26,041
we therefore also wanna export the async
47
00:02:26,041 --> 00:02:29,253
getStaticProps function again.
48
00:02:30,220 --> 00:02:33,110
Here however, we'll need the context
49
00:02:33,110 --> 00:02:34,610
because we'll need to know
50
00:02:34,610 --> 00:02:39,323
for which specific event Id we wanna load the event data.
51
00:02:40,530 --> 00:02:44,790
So we can get the event ID from context
52
00:02:44,790 --> 00:02:49,490
and they are the params and then their event Id
53
00:02:49,490 --> 00:02:53,360
because this here is the identifier
54
00:02:53,360 --> 00:02:56,300
I encoded in the file name.
55
00:02:56,300 --> 00:02:58,970
This gives us access to the event Id.
56
00:02:58,970 --> 00:03:00,840
Once we have the event Id,
57
00:03:00,840 --> 00:03:02,990
we wanna fetch these single event
58
00:03:02,990 --> 00:03:06,600
and therefore I remove the import of getEventById
59
00:03:06,600 --> 00:03:08,300
from dummy-data
60
00:03:08,300 --> 00:03:13,300
and instead import getEventsById from going up one level
61
00:03:15,270 --> 00:03:19,530
and another level helpers/api-util.
62
00:03:19,530 --> 00:03:21,590
So from this api-util file,
63
00:03:21,590 --> 00:03:24,173
which sends requests to Firebase.
64
00:03:25,020 --> 00:03:27,600
And now we wanna use this in getStaticProps
65
00:03:27,600 --> 00:03:30,410
to get that single event.
66
00:03:30,410 --> 00:03:33,560
For this we await getEventById
67
00:03:33,560 --> 00:03:36,680
because that's an async function returning a promise
68
00:03:36,680 --> 00:03:40,650
and we still pass the event Id to that function.
69
00:03:40,650 --> 00:03:44,330
And then here in the end we'll get that event
70
00:03:44,330 --> 00:03:45,530
for which we're looking.
71
00:03:46,580 --> 00:03:47,850
Now, once we get to that,
72
00:03:47,850 --> 00:03:52,850
we can return our object as we always do with the props key
73
00:03:52,930 --> 00:03:56,620
and then there, my props object will have an event
74
00:03:56,620 --> 00:04:00,270
or selected event key totally up to you
75
00:04:00,270 --> 00:04:03,793
which holds that single event, which was identified here.
76
00:04:05,120 --> 00:04:08,900
So now inside of my component function
77
00:04:08,900 --> 00:04:13,120
we can now work with that selected event prop.
78
00:04:13,120 --> 00:04:16,180
So we should accept props here,
79
00:04:16,180 --> 00:04:18,529
and then my event here,
80
00:04:18,529 --> 00:04:20,690
which I use throughout this component
81
00:04:20,690 --> 00:04:23,873
is actually props.selected event.
82
00:04:25,340 --> 00:04:27,690
Now, in this component function
83
00:04:27,690 --> 00:04:30,781
we now no longer need to use the router,
84
00:04:30,781 --> 00:04:34,550
they useRouter hook because we no longer need access
85
00:04:34,550 --> 00:04:36,480
to the event Id in there
86
00:04:36,480 --> 00:04:39,000
because data fetching doesn't happen
87
00:04:39,000 --> 00:04:41,380
inside of this component function anymore.
88
00:04:41,380 --> 00:04:46,010
So the concrete event Id doesn't matter to us here,
89
00:04:46,010 --> 00:04:47,140
and if it would,
90
00:04:47,140 --> 00:04:49,890
we could get it from this selected event prop
91
00:04:49,890 --> 00:04:54,330
because that holds the entire event, which we identified.
92
00:04:54,330 --> 00:04:56,810
So we no longer need useRouter here
93
00:04:56,810 --> 00:04:59,460
and we can therefore get rid of the import of course.
94
00:05:00,870 --> 00:05:03,460
Now with that, it wouldn't work though.
95
00:05:03,460 --> 00:05:05,403
And do you remember why?
96
00:05:06,470 --> 00:05:09,540
Because this is a dynamic page
97
00:05:09,540 --> 00:05:13,150
and there is an infinite amount of possible pages
98
00:05:13,150 --> 00:05:14,940
that could be generated.
99
00:05:14,940 --> 00:05:19,940
Next.js doesn't know which event Ids it should pre generate.
100
00:05:20,450 --> 00:05:21,580
And as a developer,
101
00:05:21,580 --> 00:05:24,480
we typically also don't know that in advance.
102
00:05:24,480 --> 00:05:26,180
If we have a dynamic page
103
00:05:26,180 --> 00:05:29,210
maybe with user generated content,
104
00:05:29,210 --> 00:05:32,930
then as a developer, we also can't anticipate
105
00:05:32,930 --> 00:05:35,910
which Ids we'll eventually have.
106
00:05:35,910 --> 00:05:39,000
That's why we need another function here.
107
00:05:39,000 --> 00:05:43,190
We need to also export another async function,
108
00:05:43,190 --> 00:05:46,603
the getStaticPaths function.
109
00:05:47,500 --> 00:05:52,500
This function will tell Next.js for which parameter values.
110
00:05:53,570 --> 00:05:57,630
So for which event Ids, it should pre-render this page
111
00:05:57,630 --> 00:06:00,020
and for which Ids, it shoould therefore call
112
00:06:00,020 --> 00:06:03,123
getStaticProps and this component function.
113
00:06:04,170 --> 00:06:09,040
For this in getStaticPaths, we have to return an object,
114
00:06:09,040 --> 00:06:13,150
an object with a paths key
115
00:06:14,150 --> 00:06:16,600
which holds an array full of objects
116
00:06:16,600 --> 00:06:20,670
where we specify our param values.
117
00:06:20,670 --> 00:06:24,250
So the first page which we wanna generate for example,
118
00:06:24,250 --> 00:06:29,250
should have a value for the event Id param of e1
119
00:06:29,360 --> 00:06:31,220
that's something we could add here.
120
00:06:31,220 --> 00:06:35,270
And now this single page for the event with e1 as an Id,
121
00:06:35,270 --> 00:06:37,470
would be pre-generated.
122
00:06:37,470 --> 00:06:38,730
But as I just said,
123
00:06:38,730 --> 00:06:41,670
we typically don't know all Ids in advance.
124
00:06:41,670 --> 00:06:44,790
So that's typically not what we wanna do.
125
00:06:44,790 --> 00:06:48,420
Instead, we wanna fetch the event Ids here
126
00:06:48,420 --> 00:06:53,420
dynamically as well, and derive this pops array dynamically.
127
00:06:54,070 --> 00:06:55,780
So for this, what we can do
128
00:06:55,780 --> 00:07:00,250
is we can go to api-util and use, getAllEvents,
129
00:07:00,250 --> 00:07:01,890
to get all events,
130
00:07:01,890 --> 00:07:04,250
then extract their Ids
131
00:07:04,250 --> 00:07:07,963
and derive the to be generated paths from that.
132
00:07:09,240 --> 00:07:13,410
So for this, I'll go back here to event Id
133
00:07:13,410 --> 00:07:16,760
and from the helpers/api-util file,
134
00:07:16,760 --> 00:07:19,147
I will also import getAllEvents.
135
00:07:20,070 --> 00:07:22,890
And now here in getStaticPaths,
136
00:07:22,890 --> 00:07:27,757
I will have all my events here by a waiting, getAllEvents
137
00:07:28,870 --> 00:07:32,200
and then all construct my paths array
138
00:07:32,200 --> 00:07:37,090
by taking my events and mapping this array of events
139
00:07:37,090 --> 00:07:42,040
into an array of objects
140
00:07:42,040 --> 00:07:45,080
where every object has a params key
141
00:07:45,080 --> 00:07:47,060
which holds another object
142
00:07:47,060 --> 00:07:52,060
where I then set the event Id to events.Id event.
143
00:07:52,500 --> 00:07:54,050
Event is the single event
144
00:07:54,050 --> 00:07:57,723
for which we're going through that events array here.
145
00:07:58,640 --> 00:08:03,490
So now paths is exactly the kind of array we need
146
00:08:03,490 --> 00:08:06,723
and hence I'll set paths: paths here.
147
00:08:08,400 --> 00:08:11,350
With that if we save this all,
148
00:08:11,350 --> 00:08:16,350
if we reload this events page for event e2,
149
00:08:17,420 --> 00:08:20,830
we get an error regarding the fallback key.
150
00:08:20,830 --> 00:08:25,730
And you might remember that we need to add this fallback key
151
00:08:25,730 --> 00:08:27,440
to let Next.js know
152
00:08:27,440 --> 00:08:31,400
if there are more possible event Id values
153
00:08:31,400 --> 00:08:34,090
which we did not pre generate here.
154
00:08:34,090 --> 00:08:36,750
So which it should then maybe generate dynamically
155
00:08:36,750 --> 00:08:41,750
on the fly, or if we did specify all possible paths here.
156
00:08:42,860 --> 00:08:45,510
Now here, I did specify all paths
157
00:08:45,510 --> 00:08:48,150
so we can set fall back to false
158
00:08:48,150 --> 00:08:49,880
letting Next.js know
159
00:08:49,880 --> 00:08:53,520
that if we try to load this page for an unknown Id,
160
00:08:53,520 --> 00:08:55,783
it should show the 404 page.
161
00:08:56,635 --> 00:09:00,020
And with that, if we set this and reload again,
162
00:09:00,020 --> 00:09:03,910
now it correctly loads the page for e2
163
00:09:03,910 --> 00:09:07,890
and that works for the April event as well.
164
00:09:07,890 --> 00:09:11,210
Now, if I try e5, which is an invalid Id,
165
00:09:11,210 --> 00:09:15,003
I get the 404 page instead, which also makes sense.
166
00:09:16,680 --> 00:09:20,320
So with that we're also handling this dynamic page
167
00:09:20,320 --> 00:09:22,170
successfully here.
168
00:09:22,170 --> 00:09:25,520
Before we now also dive into the slug page
169
00:09:25,520 --> 00:09:28,930
and this index page here for all events,
170
00:09:28,930 --> 00:09:32,180
before we do that, let's pause for a second
171
00:09:32,180 --> 00:09:35,160
and think about the two pages we worked on.
172
00:09:35,160 --> 00:09:38,890
Index.js here on the root level of the pages folder
173
00:09:38,890 --> 00:09:41,840
and dynamic event Id page.
174
00:09:41,840 --> 00:09:44,880
Maybe there's something we can optimize
175
00:09:44,880 --> 00:09:48,403
or where we could at least consider optimizations.
13688
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.