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:05,980
So, I outlined a potential problem we could be facing
2
00:00:05,980 --> 00:00:07,470
in the last lecture.
3
00:00:07,470 --> 00:00:12,293
Now Next.js can help us with a concept called pre-rendering.
4
00:00:13,160 --> 00:00:15,880
Imagine that you have some page,
5
00:00:15,880 --> 00:00:18,760
some route which then loads some page file
6
00:00:18,760 --> 00:00:21,740
in the pages folder, which needs data.
7
00:00:21,740 --> 00:00:25,090
Now when a request is sent to this route,
8
00:00:25,090 --> 00:00:28,070
so if a user wants to visit this page,
9
00:00:28,070 --> 00:00:30,670
then what Next.js does
10
00:00:30,670 --> 00:00:35,490
is it will return a pre-rendered page.
11
00:00:35,490 --> 00:00:38,560
And that's a difference to standard React.
12
00:00:38,560 --> 00:00:43,420
With standard React, you get back an empty HTML file
13
00:00:43,420 --> 00:00:45,690
and all the JavaScript code.
14
00:00:45,690 --> 00:00:48,520
And then that JavaScript code runs
15
00:00:48,520 --> 00:00:50,860
and brings something onto the screen.
16
00:00:50,860 --> 00:00:52,890
And that happens super fast
17
00:00:52,890 --> 00:00:55,800
so users won't really see a problem there.
18
00:00:55,800 --> 00:00:58,340
But then any data which might be needed
19
00:00:58,340 --> 00:01:01,520
in addition to the general DOM structure
20
00:01:01,520 --> 00:01:02,660
that should be showed,
21
00:01:02,660 --> 00:01:05,489
such data then is loaded from a server
22
00:01:05,489 --> 00:01:07,660
and that can take a while.
23
00:01:07,660 --> 00:01:10,300
Now not if the page is pre-rendered.
24
00:01:10,300 --> 00:01:13,540
And that is what Next.js does for us.
25
00:01:13,540 --> 00:01:15,840
Instead of loading data only
26
00:01:15,840 --> 00:01:19,320
after the page was sent back to the client,
27
00:01:19,320 --> 00:01:22,750
Next.js pre-renders a page
28
00:01:22,750 --> 00:01:26,290
and pre-renders all the HTML content
29
00:01:26,290 --> 00:01:28,760
with all the data that might be needed.
30
00:01:28,760 --> 00:01:30,920
It loads that in advance
31
00:01:30,920 --> 00:01:35,020
and then pre-generates the finished HTML page
32
00:01:35,020 --> 00:01:39,910
so that it's this finished, fully populated HTML page
33
00:01:39,910 --> 00:01:44,270
which can be sent back to the client, so to the visitor.
34
00:01:44,270 --> 00:01:47,180
So that's great for SEO.
35
00:01:47,180 --> 00:01:49,330
Now we also of course still want
36
00:01:49,330 --> 00:01:51,760
to have an interactive React app.
37
00:01:51,760 --> 00:01:54,140
After all, there is a reason why we use React,
38
00:01:54,140 --> 00:01:58,290
and we don't just build a HTML and CSS app.
39
00:01:58,290 --> 00:02:00,150
Instead, we might be using React
40
00:02:00,150 --> 00:02:02,320
for handling user interaction,
41
00:02:02,320 --> 00:02:05,420
for watching a forum and showing validation errors,
42
00:02:05,420 --> 00:02:07,860
for reacting to button clicks.
43
00:02:07,860 --> 00:02:09,590
And therefore Next.js
44
00:02:09,590 --> 00:02:13,340
will not just send back this pre-rendered page,
45
00:02:13,340 --> 00:02:16,370
but it will also send back all the JavaScript code
46
00:02:16,370 --> 00:02:18,050
that belongs to it.
47
00:02:18,050 --> 00:02:19,370
And it will do something
48
00:02:19,370 --> 00:02:22,480
which is called hydrating that page.
49
00:02:22,480 --> 00:02:25,290
So it will send back that JavaScript code,
50
00:02:25,290 --> 00:02:29,770
and that code will then take over that pre-rendered page,
51
00:02:29,770 --> 00:02:32,830
and again let React do its job.
52
00:02:32,830 --> 00:02:33,810
That is great
53
00:02:33,810 --> 00:02:37,130
because now we have an interactive page or app.
54
00:02:37,130 --> 00:02:40,480
But we did send back that pre-rendered page initially,
55
00:02:40,480 --> 00:02:43,410
so that all the core content was already there
56
00:02:43,410 --> 00:02:44,970
right from the start,
57
00:02:44,970 --> 00:02:48,700
and so that search engine crawlers, for example,
58
00:02:48,700 --> 00:02:52,860
also see that entire page with all the content.
59
00:02:52,860 --> 00:02:55,090
If it's not interactive at that point,
60
00:02:55,090 --> 00:02:57,450
that doesn't matter to those crawlers
61
00:02:57,450 --> 00:03:01,213
because they are only interested in the content anyways.
62
00:03:02,070 --> 00:03:04,950
So that's the idea of pre-rendering.
63
00:03:04,950 --> 00:03:09,950
We, or to be precise, Next.js, prepare a page in advance
64
00:03:11,120 --> 00:03:14,540
by pre-building all the HTML content
65
00:03:14,540 --> 00:03:17,050
and by pre-loading all the data
66
00:03:17,050 --> 00:03:18,980
that will eventually be needed.
67
00:03:18,980 --> 00:03:22,880
And it is worth pointing out that this pre-rendering
68
00:03:22,880 --> 00:03:26,260
only affects the initial load.
69
00:03:26,260 --> 00:03:30,800
So when we visit a page and we load our first page,
70
00:03:30,800 --> 00:03:33,080
this page is pre-rendered.
71
00:03:33,080 --> 00:03:35,280
Once we are on a website
72
00:03:35,280 --> 00:03:38,120
that's powered by Next.js and React,
73
00:03:38,120 --> 00:03:40,390
and once that page is hydrated,
74
00:03:40,390 --> 00:03:43,940
which happens right after this first rendering,
75
00:03:43,940 --> 00:03:45,330
once that is the case,
76
00:03:45,330 --> 00:03:49,280
we have a standard single page application again.
77
00:03:49,280 --> 00:03:51,250
So then React takes over
78
00:03:51,250 --> 00:03:53,530
and handles everything on the front end.
79
00:03:53,530 --> 00:03:55,540
If the page changes thereafter,
80
00:03:55,540 --> 00:03:59,470
if we visit a different page of that same website,
81
00:03:59,470 --> 00:04:01,860
that page is not pre-rendered,
82
00:04:01,860 --> 00:04:05,700
but instead created in the client with React.
83
00:04:05,700 --> 00:04:09,000
It's just this initial page which we visit
84
00:04:09,000 --> 00:04:10,390
which is pre-rendered.
85
00:04:10,390 --> 00:04:13,280
So that there we don't get this empty page
86
00:04:13,280 --> 00:04:15,030
until React is ready,
87
00:04:15,030 --> 00:04:17,329
but we get this pre-rendered page
88
00:04:17,329 --> 00:04:20,673
which contains all that initial content already.
89
00:04:21,579 --> 00:04:23,180
And for achieving this,
90
00:04:23,180 --> 00:04:26,950
Next.js has two forms of pre-rendering,
91
00:04:26,950 --> 00:04:30,150
and we are, of course, going to learn about both,
92
00:04:30,150 --> 00:04:31,780
which you can choose from.
93
00:04:31,780 --> 00:04:34,760
So you can choose which form you wanna use.
94
00:04:34,760 --> 00:04:37,970
The first form, and the recommended form,
95
00:04:37,970 --> 00:04:40,810
is static generation.
96
00:04:40,810 --> 00:04:45,810
The other form, the alternative, is server-side rendering.
97
00:04:46,030 --> 00:04:48,760
Now that might not tell you much right now.
98
00:04:48,760 --> 00:04:51,950
I will explain both throughout this section.
99
00:04:51,950 --> 00:04:55,560
The main difference is that with static generation,
100
00:04:55,560 --> 00:04:58,200
all the pages are pre-generated
101
00:04:58,200 --> 00:05:00,880
in advance during build time.
102
00:05:00,880 --> 00:05:04,180
So when you build your application for production,
103
00:05:04,180 --> 00:05:08,620
before you deploy it, you prepare all those pages.
104
00:05:08,620 --> 00:05:13,620
With server-side rendering, pages are created just in time
105
00:05:13,670 --> 00:05:17,460
after deployment when a request reaches the server.
106
00:05:17,460 --> 00:05:20,050
Now there are also ways of mixing that
107
00:05:20,050 --> 00:05:23,070
and I will dive deeply into both concepts
108
00:05:23,070 --> 00:05:26,123
and when to use which throughout this section.
109
00:05:27,000 --> 00:05:29,050
I hope it's clear what the core idea
110
00:05:29,050 --> 00:05:30,830
behind pre-rendering is, though,
111
00:05:30,830 --> 00:05:34,200
and therefore, let's now explore these different concepts
112
00:05:34,200 --> 00:05:36,183
of pre-rendering pages.
8627
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.