Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,160 --> 00:00:03,640
Now getServiceSideProps
2
00:00:03,640 --> 00:00:05,450
can sometimes be useful.
3
00:00:05,450 --> 00:00:08,010
I also want to mention how you would use it
4
00:00:08,010 --> 00:00:10,010
with a dynamic page.
5
00:00:10,010 --> 00:00:13,850
Here before, when we used getStaticProps,
6
00:00:13,850 --> 00:00:16,340
we also needed getStaticPaths
7
00:00:16,340 --> 00:00:20,810
to let Next.js know which instances of this page
8
00:00:20,810 --> 00:00:23,260
should be pre-generated.
9
00:00:23,260 --> 00:00:27,320
Now when using getServiceSideProps, that's not the case.
10
00:00:27,320 --> 00:00:32,320
If I do have a [Uid].js file, Uid in square brackets,
11
00:00:33,620 --> 00:00:36,970
so that I have a dynamic page for a single user
12
00:00:36,970 --> 00:00:38,930
for different user IDs.
13
00:00:38,930 --> 00:00:43,020
If we use getServiceSideProps we don't need
14
00:00:43,020 --> 00:00:45,423
and we can't use getStaticPaths.
15
00:00:46,700 --> 00:00:49,100
So here, if I have the UserIdPage
16
00:00:50,350 --> 00:00:52,200
and I get some props here
17
00:00:52,200 --> 00:00:57,093
and here I then simply return h1 tag where I put props,
18
00:00:58,040 --> 00:01:01,663
id something like this, and we then export this.
19
00:01:02,850 --> 00:01:04,099
If we do all of that,
20
00:01:04,099 --> 00:01:09,100
and I then also have this async getServerSideProps function
21
00:01:10,830 --> 00:01:11,993
which we export.
22
00:01:12,850 --> 00:01:15,080
If we have this, then you will see
23
00:01:15,080 --> 00:01:17,930
that we don't need getStaticPaths at all.
24
00:01:17,930 --> 00:01:20,220
Here I just returned an object,
25
00:01:20,220 --> 00:01:23,050
and now I'm interested in the concrete param value
26
00:01:23,050 --> 00:01:24,780
which I'm getting here.
27
00:01:24,780 --> 00:01:27,890
So we can get access to that through context.
28
00:01:27,890 --> 00:01:31,610
From there we can still extract params as I mentioned,
29
00:01:31,610 --> 00:01:34,840
and from params we can still get our userId
30
00:01:34,840 --> 00:01:39,410
by accessing uid, since that's the identifier I picked
31
00:01:39,410 --> 00:01:41,620
between square brackets.
32
00:01:41,620 --> 00:01:43,420
And then in the props which we passed
33
00:01:43,420 --> 00:01:45,420
to the component function,
34
00:01:45,420 --> 00:01:49,230
we could have the id prop, since I'm accessing that here
35
00:01:49,230 --> 00:01:51,447
and set this equal to userid-
36
00:01:53,950 --> 00:01:58,950
and then let's say the userId we extracted from the URL,
37
00:01:59,010 --> 00:02:02,250
just to have some dummy code here that proves
38
00:02:02,250 --> 00:02:04,680
that this code actually did execute
39
00:02:04,680 --> 00:02:07,720
before this component function code ran.
40
00:02:07,720 --> 00:02:10,020
With that if you wanna test this,
41
00:02:10,020 --> 00:02:13,570
we first of all need to make sure we get rid of [pid].js
42
00:02:13,570 --> 00:02:18,050
because otherwise we have two dynamic segment pages
43
00:02:18,050 --> 00:02:21,020
on the same level in our pages folder,
44
00:02:21,020 --> 00:02:25,570
so Next.js wouldn't know if a value after slash
45
00:02:25,570 --> 00:02:28,880
nothing should be a product ID or a user ID,
46
00:02:28,880 --> 00:02:33,030
and therefore if I move to pitfall
47
00:02:33,030 --> 00:02:34,953
into the products folder here.
48
00:02:36,630 --> 00:02:39,710
And in index.js I update my links
49
00:02:39,710 --> 00:02:43,990
to go to /products/some ID,
50
00:02:43,990 --> 00:02:47,380
now we resolve the disc clash because now we know
51
00:02:47,380 --> 00:02:50,890
that if it's just slash something unrecognized,
52
00:02:50,890 --> 00:02:54,410
it will always target this user ID route.
53
00:02:54,410 --> 00:02:58,740
And now with that, if I bring back that development server,
54
00:02:58,740 --> 00:03:03,740
if we visit let's say /u1 for user one,
55
00:03:04,580 --> 00:03:08,620
we see userid-u1 here so it worked.
56
00:03:08,620 --> 00:03:13,620
And it did work without us adding getStaticPaths.
57
00:03:13,890 --> 00:03:18,310
Why? Because this runs on the server only anyways.
58
00:03:18,310 --> 00:03:22,500
So Next.js does not pre generate any pages at all,
59
00:03:22,500 --> 00:03:25,200
and therefore of course, it also doesn't need to know
60
00:03:25,200 --> 00:03:27,630
which pages to pre-generate
61
00:03:27,630 --> 00:03:30,590
because there is no pre-generation.
62
00:03:30,590 --> 00:03:33,620
So unlike in the other case with getStaticProps
63
00:03:34,580 --> 00:03:37,170
where we do pre-generate pages
64
00:03:37,170 --> 00:03:40,890
and where we therefore for do need to tell Next.js
65
00:03:40,890 --> 00:03:44,950
for which param values pages should be pre-generated,
66
00:03:44,950 --> 00:03:48,890
with getServiceSideProps that's simply not an issue
67
00:03:48,890 --> 00:03:51,520
because we run that server side code
68
00:03:51,520 --> 00:03:54,020
for every request anyways.
69
00:03:54,020 --> 00:03:55,730
So there is no pre-generation
70
00:03:55,730 --> 00:03:58,960
and therefore no need to define those dynamic paths
71
00:03:58,960 --> 00:04:00,680
in advance.
72
00:04:00,680 --> 00:04:02,403
I hope that makes sense.
5620
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.