Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,170 --> 00:00:03,080
So, let's explore
2
00:00:03,080 --> 00:00:05,860
this first form of pre-rendering,
3
00:00:05,860 --> 00:00:09,550
or of data fetching with Next.js.
4
00:00:09,550 --> 00:00:12,680
And that would be the Static Generation,
5
00:00:12,680 --> 00:00:15,510
which I did mention is typically recommended,
6
00:00:15,510 --> 00:00:18,680
but of course, I will explain all the details now
7
00:00:18,680 --> 00:00:21,160
and throughout this section.
8
00:00:21,160 --> 00:00:23,090
Now, with Static Generation,
9
00:00:23,090 --> 00:00:24,630
there is a simple idea.
10
00:00:24,630 --> 00:00:29,480
The idea is that you pre-generate a page during build time.
11
00:00:29,480 --> 00:00:33,000
And pre-generate means that all the HTML code
12
00:00:33,000 --> 00:00:35,620
and all the data that makes up the content,
13
00:00:35,620 --> 00:00:38,090
is prepared in advance.
14
00:00:38,090 --> 00:00:40,680
So, on the server side though,
15
00:00:40,680 --> 00:00:42,110
that can be confusing here.
16
00:00:42,110 --> 00:00:43,830
With that I really mean,
17
00:00:43,830 --> 00:00:45,880
during the build process,
18
00:00:45,880 --> 00:00:48,970
where you are allowed to execute code
19
00:00:48,970 --> 00:00:52,150
that would normally only run on the server side, though.
20
00:00:52,150 --> 00:00:55,680
Code that interacts with the file system, for example.
21
00:00:55,680 --> 00:00:58,640
And you're going to see it in action very soon.
22
00:00:58,640 --> 00:01:01,960
So, data and pages are prepared during build time
23
00:01:01,960 --> 00:01:05,860
when you build your application before you deploy it.
24
00:01:05,860 --> 00:01:10,330
Now, because pages are then pre-built during build time,
25
00:01:10,330 --> 00:01:12,230
once you deploy them,
26
00:01:12,230 --> 00:01:15,250
they can be cached by the server,
27
00:01:15,250 --> 00:01:18,190
by the CDN that might be serving your app.
28
00:01:18,190 --> 00:01:20,640
And therefore, incoming requests
29
00:01:20,640 --> 00:01:25,640
can be served instantly with those pre-built pages.
30
00:01:25,820 --> 00:01:28,060
Now, then, as I mentioned before,
31
00:01:28,060 --> 00:01:30,680
those pages after they were served,
32
00:01:30,680 --> 00:01:33,810
are still hydrated with your React app.
33
00:01:33,810 --> 00:01:37,360
So, you still have a regular React app in the end.
34
00:01:37,360 --> 00:01:39,470
The only difference is that the pages
35
00:01:39,470 --> 00:01:41,320
which are sent to your clients,
36
00:01:41,320 --> 00:01:43,130
are not empty initially
37
00:01:43,130 --> 00:01:45,590
but pre populated with content.
38
00:01:45,590 --> 00:01:48,350
Thereafter, you can still do whatever you want to do.
39
00:01:48,350 --> 00:01:51,330
It's still a regular React app.
40
00:01:51,330 --> 00:01:54,880
So, therefore, now, one main question, of course is,
41
00:01:54,880 --> 00:01:57,340
how do we tell Next.js
42
00:01:57,340 --> 00:02:00,680
that a certain page should be pre-generated?
43
00:02:00,680 --> 00:02:02,960
And how do we tell Next.js
44
00:02:02,960 --> 00:02:06,850
which data is needed to pre-generate a page?
45
00:02:06,850 --> 00:02:08,300
And the answer is,
46
00:02:08,300 --> 00:02:10,449
there is a specific function
47
00:02:10,449 --> 00:02:15,220
which we can export from inside our page components.
48
00:02:15,220 --> 00:02:19,620
And that's important only from inside our page components,
49
00:02:19,620 --> 00:02:22,160
not from our React components,
50
00:02:22,160 --> 00:02:24,960
but only from inside the component files
51
00:02:24,960 --> 00:02:27,100
that live in our pages folder.
52
00:02:27,100 --> 00:02:28,180
From in there,
53
00:02:28,180 --> 00:02:33,097
we can export the special async function, getStaticProps.
54
00:02:34,640 --> 00:02:36,860
Now, that name matters.
55
00:02:36,860 --> 00:02:40,900
It must be written exactly like this, getStaticProps,
56
00:02:40,900 --> 00:02:45,840
because that is then a function Next.js will watch out for.
57
00:02:45,840 --> 00:02:48,430
Its async, which means it returns a promise,
58
00:02:48,430 --> 00:02:50,830
and you can use the await keyword in there.
59
00:02:50,830 --> 00:02:55,020
And then, the special thing is that in this function,
60
00:02:55,020 --> 00:02:56,780
you can run any code
61
00:02:56,780 --> 00:03:00,380
that would normally run on the server side only.
62
00:03:00,380 --> 00:03:01,460
So, in that function,
63
00:03:01,460 --> 00:03:03,700
you don't run the client side code,
64
00:03:03,700 --> 00:03:06,140
you're not restricted to that
65
00:03:06,140 --> 00:03:10,170
and you don't have access to certain client side API,
66
00:03:10,170 --> 00:03:13,860
you don't have access to the window object, for example,
67
00:03:13,860 --> 00:03:16,580
but instead, you can run any code you want,
68
00:03:16,580 --> 00:03:20,030
that normally would only run on the server side.
69
00:03:20,030 --> 00:03:21,650
And even better than that,
70
00:03:21,650 --> 00:03:25,930
code that you write inside of this getStaticProps function,
71
00:03:25,930 --> 00:03:29,030
will not be included in the code bundle
72
00:03:29,030 --> 00:03:31,620
that's sent back to your clients.
73
00:03:31,620 --> 00:03:33,630
So, any code you put in there,
74
00:03:33,630 --> 00:03:36,350
will never be seen by your clients.
75
00:03:36,350 --> 00:03:38,980
So, if you for example have code in there
76
00:03:38,980 --> 00:03:41,800
that contains database credentials,
77
00:03:41,800 --> 00:03:44,920
you typically wouldn't want to expose those credentials
78
00:03:44,920 --> 00:03:46,320
on the client side.
79
00:03:46,320 --> 00:03:49,740
You can safely write it inside of getStaticProps
80
00:03:49,740 --> 00:03:53,990
because that code will never end up on the client side.
81
00:03:53,990 --> 00:03:56,800
And therefore, let's now take a closer look at that
82
00:03:56,800 --> 00:03:58,910
and let's start writing some code there,
83
00:03:58,910 --> 00:04:02,573
so that we fully understand this function and how it works.
6462
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.