Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,299 --> 00:00:04,430
[Maximilian Schwarzm�ller] Now this was a long module
2
00:00:04,430 --> 00:00:07,060
with a lot of key concepts.
3
00:00:07,060 --> 00:00:09,300
In general, this module was about
4
00:00:09,300 --> 00:00:13,140
how Next.js pre-renders pages for us
5
00:00:13,140 --> 00:00:15,420
so that if we view at a page source
6
00:00:15,420 --> 00:00:18,640
we have some data in that page source,
7
00:00:18,640 --> 00:00:21,170
some data which is there right from the start
8
00:00:21,170 --> 00:00:23,070
and which is there for all the visible
9
00:00:23,070 --> 00:00:25,270
to search engine crawlers
10
00:00:25,270 --> 00:00:26,580
which is a key difference
11
00:00:26,580 --> 00:00:30,420
to standard React apps without service side rendering.
12
00:00:30,420 --> 00:00:31,830
Now you did learn
13
00:00:31,830 --> 00:00:36,620
that out of the box Next.js pre-renders your pages.
14
00:00:36,620 --> 00:00:39,030
It does that automatically
15
00:00:39,030 --> 00:00:43,260
but you can also force it to do so by adding getStaticProps
16
00:00:43,260 --> 00:00:45,120
because you add this function
17
00:00:45,120 --> 00:00:49,830
whenever you also wanna pre-fetch data for your component.
18
00:00:49,830 --> 00:00:53,100
So if your page component doesn't need any data
19
00:00:53,100 --> 00:00:56,630
of course you never add getStaticProps typically
20
00:00:56,630 --> 00:01:00,250
because it doesn't add anything to the page then
21
00:01:00,250 --> 00:01:02,800
but if your page does needs data like the list
22
00:01:02,800 --> 00:01:06,390
of products here, then you add getStaticProps
23
00:01:06,390 --> 00:01:10,740
So that during the build process, this data is pre-fetched
24
00:01:10,740 --> 00:01:14,130
and available for the page when it's pre-rendered
25
00:01:14,130 --> 00:01:18,143
so that the pre-rendered page contains pre-fetched data.
26
00:01:19,370 --> 00:01:21,650
Since geStaticProps executes
27
00:01:21,650 --> 00:01:23,470
during the build process
28
00:01:23,470 --> 00:01:26,540
you can execute node js code in there
29
00:01:26,540 --> 00:01:28,870
which wouldn't run in the browser.
30
00:01:28,870 --> 00:01:30,390
For example you can get access
31
00:01:30,390 --> 00:01:34,590
to the file system and read data from files.
32
00:01:34,590 --> 00:01:37,660
Since data can get outdated
33
00:01:37,660 --> 00:01:40,090
after the pages were pre-generated,
34
00:01:40,090 --> 00:01:43,220
you can also add the revalidate key here
35
00:01:43,220 --> 00:01:47,140
in the object you return from inside getStaticProps
36
00:01:47,140 --> 00:01:48,580
to tell Next.js
37
00:01:48,580 --> 00:01:52,440
that this page should be regenerated again and again
38
00:01:52,440 --> 00:01:56,260
even after the page was built and deployed
39
00:01:56,260 --> 00:02:01,260
at most every x seconds here at most once every 10 seconds.
40
00:02:02,460 --> 00:02:04,420
Therefore getStaticProps is great
41
00:02:04,420 --> 00:02:08,562
for ensuring that you are pre-rendering pages with data.
42
00:02:09,419 --> 00:02:13,630
If you have dynamic pages with a dynamic parameter
43
00:02:13,630 --> 00:02:18,630
then you also need to let Next.js know how many instances
44
00:02:19,060 --> 00:02:21,420
of that page should be pre-rendered
45
00:02:21,420 --> 00:02:24,540
because it could be an infinite amount in theory.
46
00:02:24,540 --> 00:02:27,050
You do that with getStaticPaths
47
00:02:27,050 --> 00:02:29,470
and you need to add this when using
48
00:02:29,470 --> 00:02:32,780
getStaticProps on such a dynamic page.
49
00:02:32,780 --> 00:02:34,440
Otherwise we'll get an error
50
00:02:34,440 --> 00:02:38,860
because Next.js doesn't know how many pages to prepare.
51
00:02:38,860 --> 00:02:42,600
GetStaticPaths returns an object with a paths' key
52
00:02:42,600 --> 00:02:46,700
which holds an array of param objects in the end
53
00:02:46,700 --> 00:02:49,990
where you basically just make Next.js aware
54
00:02:49,990 --> 00:02:52,370
of all the parameter values
55
00:02:52,370 --> 00:02:55,820
for this dynamic page that should be used
56
00:02:55,820 --> 00:02:58,450
for pre-rendering a page.
57
00:02:58,450 --> 00:03:01,100
And you can also reach out to a file system
58
00:03:01,100 --> 00:03:04,430
or database or whatever you need to do here
59
00:03:04,430 --> 00:03:07,430
and when you do that Next.js will pre-generate
60
00:03:07,430 --> 00:03:11,270
all the paths so all the instances of that page
61
00:03:11,270 --> 00:03:15,320
for the paths you tell it to pre-render.
62
00:03:15,320 --> 00:03:17,460
It will then call getStaticProps
63
00:03:17,460 --> 00:03:20,240
for every page instance that it creates,
64
00:03:20,240 --> 00:03:22,850
you can access the params there as you learned
65
00:03:22,850 --> 00:03:26,240
and then you can fetch and prepare the data there just
66
00:03:26,240 --> 00:03:29,610
as it's needed, return that to the component function,
67
00:03:29,610 --> 00:03:33,470
and then last but not least Next.js will execute
68
00:03:33,470 --> 00:03:36,500
this component function in the build process
69
00:03:36,500 --> 00:03:40,640
or on the server to pre-render this page as well
70
00:03:40,640 --> 00:03:42,663
with the appropriate data.
71
00:03:44,090 --> 00:03:48,180
Sometimes you don't wanna pre-build during the build process
72
00:03:48,180 --> 00:03:50,710
and even when using revalidate
73
00:03:50,710 --> 00:03:53,920
the data might not be updated enough.
74
00:03:53,920 --> 00:03:56,480
Instead you really might want to run logic
75
00:03:56,480 --> 00:04:00,130
for every incoming request either because you need access
76
00:04:00,130 --> 00:04:04,140
to the request or because the data changes all the time.
77
00:04:04,140 --> 00:04:07,700
In such cases, you can use getServerSideProps.
78
00:04:07,700 --> 00:04:11,070
It's an alternative and unlike getStaticProps,
79
00:04:11,070 --> 00:04:14,890
this really runs on the server and only on the server,
80
00:04:14,890 --> 00:04:17,529
not during the build process.
81
00:04:17,529 --> 00:04:21,620
Here you can get access to the request and response objects
82
00:04:21,620 --> 00:04:24,990
to the real node request and response objects
83
00:04:24,990 --> 00:04:27,770
and you still return props to component,
84
00:04:27,770 --> 00:04:29,580
which is then pre-rendered
85
00:04:29,580 --> 00:04:32,920
but pre-rendered on the fly on the Server
86
00:04:34,730 --> 00:04:37,280
You can also do that for dynamic pages
87
00:04:37,280 --> 00:04:39,890
and here you don't need getStaticPaths
88
00:04:39,890 --> 00:04:42,600
because this now really does only execute
89
00:04:42,600 --> 00:04:45,870
when it's needed not in advance.
90
00:04:45,870 --> 00:04:49,220
However sometimes that's all not what you are looking for.
91
00:04:49,220 --> 00:04:51,410
Sometimes you really need to fetch data
92
00:04:51,410 --> 00:04:55,110
from inside the client or you wanna to combine pre-rendering
93
00:04:55,110 --> 00:04:57,320
with client site data fetching.
94
00:04:57,320 --> 00:04:59,940
And that's what we saw in the last lectures.
95
00:04:59,940 --> 00:05:03,780
You can also still write regular React code,
96
00:05:03,780 --> 00:05:07,340
regular React JavaScript code for fetching data
97
00:05:07,340 --> 00:05:09,460
in your component function
98
00:05:09,460 --> 00:05:12,660
and this code will then not be executed on the server
99
00:05:12,660 --> 00:05:15,320
even though the page will still be pre-rendered
100
00:05:15,320 --> 00:05:17,210
but this code will be skipped
101
00:05:17,210 --> 00:05:19,590
since its inside of useEffect typically
102
00:05:19,590 --> 00:05:22,560
instead you'll just get some Snapshot of your page
103
00:05:22,560 --> 00:05:23,860
which is then returned
104
00:05:23,860 --> 00:05:27,250
and that code really only runs in the client.
105
00:05:27,250 --> 00:05:29,790
So when a visitor visits the page
106
00:05:29,790 --> 00:05:33,570
here you can also consider using the useSWR hook
107
00:05:33,570 --> 00:05:36,570
since that can make writing that code a bit easier
108
00:05:36,570 --> 00:05:40,180
and since that is a feature rich custom hook built
109
00:05:40,180 --> 00:05:42,990
by the Next.js team which for example
110
00:05:42,990 --> 00:05:46,480
also automatically re-fetches data if you will lose
111
00:05:46,480 --> 00:05:49,970
and regain focus in your browser tab.
112
00:05:49,970 --> 00:05:51,910
So that can also be nice.
113
00:05:51,910 --> 00:05:54,540
And as you saw you can even still combine that
114
00:05:54,540 --> 00:05:57,540
with preparing and pre-fetching data.
115
00:05:57,540 --> 00:05:59,860
And therefore this was a long module
116
00:05:59,860 --> 00:06:03,240
but data fetching and pre-rendering of pages
117
00:06:03,240 --> 00:06:08,240
with pre-fetched data is another key aspect off Next.js.
118
00:06:09,390 --> 00:06:14,000
The file-based routing was nice and already a great feature
119
00:06:14,000 --> 00:06:17,310
but this here pre-fetching and pre-rendering,
120
00:06:17,310 --> 00:06:18,370
that's the feature
121
00:06:18,370 --> 00:06:21,860
which really makes Next.js super powerful
122
00:06:21,860 --> 00:06:25,870
because now it's easy to optimize pages for search engines,
123
00:06:25,870 --> 00:06:29,750
it's easy to show users some data right from the start
124
00:06:29,750 --> 00:06:34,050
and you can still apply everything you know about React.
125
00:06:34,050 --> 00:06:37,050
And therefore let's now apply what we learned here
126
00:06:37,050 --> 00:06:40,520
to our demo project in the next section
127
00:06:40,520 --> 00:06:43,350
before we then dig deeper into Next again
10313
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.