Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,060 --> 00:00:04,300
Now for this course section
2
00:00:04,300 --> 00:00:09,300
as a demo project, I picked this routing project
3
00:00:09,450 --> 00:00:12,210
we worked on earlier in the course.
4
00:00:12,210 --> 00:00:14,520
This project where we have a couple of quotes
5
00:00:14,520 --> 00:00:18,650
and comments for the quotes and where we use React router.
6
00:00:18,650 --> 00:00:20,970
But of course you can deploy any
7
00:00:20,970 --> 00:00:23,540
of the example applications in this course,
8
00:00:23,540 --> 00:00:25,420
I just picked this one
9
00:00:25,420 --> 00:00:29,930
because there is one very specific optimization step
10
00:00:29,930 --> 00:00:33,840
I wanna show you, which is related to routing.
11
00:00:33,840 --> 00:00:36,530
And speaking of that let's assume
12
00:00:36,530 --> 00:00:38,560
we built our application,
13
00:00:38,560 --> 00:00:42,100
we tested the code and we're happy with our code.
14
00:00:42,100 --> 00:00:44,700
We think it works the way it should.
15
00:00:44,700 --> 00:00:48,300
Therefore, we can now move on to the next step in line,
16
00:00:48,300 --> 00:00:51,470
which is that we wanna optimize our code.
17
00:00:51,470 --> 00:00:53,620
Now with that, I don't mean,
18
00:00:53,620 --> 00:00:57,660
or at least I not only mean that we should write our code
19
00:00:57,660 --> 00:01:00,290
using best practices and so on,
20
00:01:00,290 --> 00:01:04,349
but I'm talking about specific performance improvements
21
00:01:04,349 --> 00:01:06,690
which we could consider.
22
00:01:06,690 --> 00:01:10,670
Now for one, that includes performance improvements
23
00:01:10,670 --> 00:01:15,410
for example, with React memo as it cost way early
24
00:01:15,410 --> 00:01:19,470
in the course already, where I introduced the React memo
25
00:01:19,470 --> 00:01:23,210
to a wide unnecessary component re-render cycles.
26
00:01:23,210 --> 00:01:25,480
In case you're not sure what I'm talking about,
27
00:01:25,480 --> 00:01:28,190
I linked to respect of lecture.
28
00:01:28,190 --> 00:01:30,530
But that's not what I wanna dive in here,
29
00:01:30,530 --> 00:01:32,920
I did talk about this earlier
30
00:01:32,920 --> 00:01:35,190
and whilst we're not using React memo
31
00:01:35,190 --> 00:01:37,010
in this demo application,
32
00:01:37,010 --> 00:01:40,260
it's now not the kind of change I wanna make here.
33
00:01:40,260 --> 00:01:43,860
Instead I wanna introduce another very important concept.
34
00:01:43,860 --> 00:01:46,610
The concept of lazy loading.
35
00:01:46,610 --> 00:01:51,450
Lazy loading means that we load certain chunks of our code
36
00:01:51,450 --> 00:01:56,440
certain parts of our code only when that code is needed.
37
00:01:56,440 --> 00:01:59,260
Because it is super important to keep in mind
38
00:01:59,260 --> 00:02:02,380
that with a React Single Page Application,
39
00:02:02,380 --> 00:02:07,160
you in the end, build one big JavaScript code bundle,
40
00:02:07,160 --> 00:02:11,120
and this entire bundle needs to be downloaded
41
00:02:11,120 --> 00:02:16,120
by every visitor of your website in order to use that site.
42
00:02:16,540 --> 00:02:19,710
So here if I start my development server again
43
00:02:19,710 --> 00:02:24,180
for this demo application this quotes application,
44
00:02:24,180 --> 00:02:27,770
it is important to understand that everything we see here
45
00:02:27,770 --> 00:02:30,210
and everything we're able to use,
46
00:02:30,210 --> 00:02:34,530
like for example being able to switch to this adequate page
47
00:02:34,530 --> 00:02:39,530
all of that is driven by JavaScript by our React code.
48
00:02:40,130 --> 00:02:42,800
And that means that when we visit this page,
49
00:02:42,800 --> 00:02:45,950
if I enter local host free thousand here
50
00:02:45,950 --> 00:02:47,810
and I visit this page,
51
00:02:47,810 --> 00:02:51,680
all that React code that needs to be downloaded
52
00:02:51,680 --> 00:02:56,680
in order for React to draw all this user interface
53
00:02:56,730 --> 00:03:01,460
on the screen and in order for this to be reactive.
54
00:03:01,460 --> 00:03:04,640
So therefore, this all what we see here
55
00:03:04,640 --> 00:03:06,700
and what we use here only works
56
00:03:06,700 --> 00:03:09,710
once all our code was downloaded.
57
00:03:09,710 --> 00:03:11,540
And that means that a user,
58
00:03:11,540 --> 00:03:14,800
a visitor of our website has to wait
59
00:03:14,800 --> 00:03:17,660
until all that code was downloaded
60
00:03:17,660 --> 00:03:21,130
until he or she sees something on the screen,
61
00:03:21,130 --> 00:03:24,430
and until our web application is usable.
62
00:03:24,430 --> 00:03:29,430
And therefore, we wanna ensure that this initial code bundle
63
00:03:29,840 --> 00:03:33,490
this initial first code bundle, which is downloaded
64
00:03:33,490 --> 00:03:38,400
is as small as possible and certain parts of our code
65
00:03:38,400 --> 00:03:41,010
of for example, the code that is related
66
00:03:41,010 --> 00:03:44,260
to adding a quote, is only downloaded
67
00:03:44,260 --> 00:03:48,070
if we actually visit this part of the page.
68
00:03:48,070 --> 00:03:51,000
Because if I enter low close, free thousand
69
00:03:51,000 --> 00:03:53,700
and I only see this starting screen,
70
00:03:53,700 --> 00:03:57,160
and I may be never even add a new quote,
71
00:03:57,160 --> 00:04:00,950
then there is no need to download all the code
72
00:04:00,950 --> 00:04:04,250
that is related to this add a quote feature.
73
00:04:04,250 --> 00:04:08,290
It will only make that initial download bigger unnecessarily
74
00:04:08,290 --> 00:04:10,450
because we don't need that code.
75
00:04:10,450 --> 00:04:13,310
And that's therefore what lazy loading is about.
76
00:04:13,310 --> 00:04:17,120
We wanna split our code into multiple chunks,
77
00:04:17,120 --> 00:04:20,360
into multiple bundles, which are then each
78
00:04:20,360 --> 00:04:23,390
only downloaded when they are needed.
79
00:04:23,390 --> 00:04:26,050
And lazy loading is easy to implement
80
00:04:26,050 --> 00:04:27,980
if you are using routing
81
00:04:27,980 --> 00:04:31,320
because you can then split your code by route.
82
00:04:31,320 --> 00:04:33,810
So that the code for a specific route
83
00:04:33,810 --> 00:04:37,880
is only downloaded when that route is visited.
84
00:04:37,880 --> 00:04:41,450
And therefore let's now implement lazy loading here.
85
00:04:41,450 --> 00:04:45,350
In app JS, we are defining our main routes.
86
00:04:45,350 --> 00:04:49,420
And at the moment we do that by importing
87
00:04:49,420 --> 00:04:53,350
our different page components from the pages folder
88
00:04:53,350 --> 00:04:55,750
and rendering them here.
89
00:04:55,750 --> 00:05:00,750
Now, even though those different pages are only visited
90
00:05:01,290 --> 00:05:05,730
when the URL matches the POS to find here,
91
00:05:05,730 --> 00:05:08,780
all that code is downloaded in advance,
92
00:05:08,780 --> 00:05:11,920
because we have all these import statements here.
93
00:05:11,920 --> 00:05:16,920
And they are all evaluated when our code is bundled up,
94
00:05:17,080 --> 00:05:19,510
which will be part of that build process,
95
00:05:19,510 --> 00:05:21,450
which we'll see in a couple of minutes
96
00:05:21,450 --> 00:05:23,920
and which is also done behind the scenes
97
00:05:23,920 --> 00:05:26,500
when we run that development server.
98
00:05:26,500 --> 00:05:30,030
So we download all the code for all those pages
99
00:05:30,030 --> 00:05:33,650
and for all the components those pages then use,
100
00:05:33,650 --> 00:05:36,163
even though we might not visit all pages.
101
00:05:37,170 --> 00:05:40,650
Now we can easily add code splitting here,
102
00:05:40,650 --> 00:05:45,650
by adding another import and by importing React from React.
103
00:05:46,750 --> 00:05:50,850
And then we can replace those page imports,
104
00:05:50,850 --> 00:05:54,850
with a slight alternative syntax.
105
00:05:54,850 --> 00:05:57,650
For example, let's say, we only wanna download
106
00:05:57,650 --> 00:06:00,290
the code for this new quote component,
107
00:06:00,290 --> 00:06:03,550
when a user really visits this Route.
108
00:06:03,550 --> 00:06:07,170
For this we can remove this import of new quote
109
00:06:07,170 --> 00:06:11,020
and instead add constant named new quote.
110
00:06:11,020 --> 00:06:12,810
I picked this constant name
111
00:06:12,810 --> 00:06:15,870
because I'm using new quote here, down here
112
00:06:15,870 --> 00:06:17,453
when I rendered this component.
113
00:06:18,450 --> 00:06:22,080
Now in here, I now wanna store the result
114
00:06:22,080 --> 00:06:25,960
of calling React.lazy.
115
00:06:25,960 --> 00:06:27,430
That's a built-in method
116
00:06:27,430 --> 00:06:30,220
that will help us with code splitting.
117
00:06:30,220 --> 00:06:34,070
React lazy then once of function,
118
00:06:34,070 --> 00:06:37,230
and here I use an inline arrow function,
119
00:06:37,230 --> 00:06:41,150
which resolves to a dynamic import.
120
00:06:41,150 --> 00:06:43,510
And this syntax might look strange now,
121
00:06:43,510 --> 00:06:47,840
but that is a standard modern JavaScript syntax,
122
00:06:47,840 --> 00:06:52,840
which is supported by this React project set up in the end.
123
00:06:53,040 --> 00:06:56,470
Here as a result of this function,
124
00:06:56,470 --> 00:06:58,650
this anonymous function which are defined here,
125
00:06:58,650 --> 00:07:01,163
we call import as a function.
126
00:07:02,030 --> 00:07:05,040
So here we use the import statement like this,
127
00:07:05,040 --> 00:07:08,170
now we call import as a function.
128
00:07:08,170 --> 00:07:12,350
And cutest function we pass the path of this import.
129
00:07:12,350 --> 00:07:14,250
So pages new quote,
130
00:07:14,250 --> 00:07:17,633
what we previously had up here after from.
131
00:07:19,080 --> 00:07:20,800
Now, this function here
132
00:07:20,800 --> 00:07:25,530
which we pass to React lazy, will be executed by React,
133
00:07:25,530 --> 00:07:28,660
when this new quote component is needed.
134
00:07:28,660 --> 00:07:30,060
And that's the key difference.
135
00:07:30,060 --> 00:07:32,750
It will not be executed in advance
136
00:07:32,750 --> 00:07:37,160
to download the code in advance, but only when it's needed.
137
00:07:37,160 --> 00:07:40,350
And that will therefore create a separate code chunk
138
00:07:40,350 --> 00:07:41,650
which is only downloaded
139
00:07:41,650 --> 00:07:43,823
when this new quote page is visited.
140
00:07:44,970 --> 00:07:48,270
Now, I'm still using the new quote component down there,
141
00:07:48,270 --> 00:07:53,080
but this component is resolved and loaded dynamically,
142
00:07:53,080 --> 00:07:54,580
thanks to React lazy
143
00:07:54,580 --> 00:07:57,730
with this anonymous function past to it.
144
00:07:57,730 --> 00:08:01,160
Now with this alone though, it wouldn't work.
145
00:08:01,160 --> 00:08:04,750
With this if we reload and click on add a quote,
146
00:08:04,750 --> 00:08:09,430
we fail here and if I directly try to visit slash new quote
147
00:08:09,430 --> 00:08:13,220
we get an error, that React component suspended
148
00:08:13,220 --> 00:08:17,180
while rendering but no fallback UI was specified.
149
00:08:17,180 --> 00:08:18,290
Now, what does this mean?
150
00:08:18,290 --> 00:08:20,740
And where is this error coming from?
151
00:08:20,740 --> 00:08:23,860
The problem we have here is that
152
00:08:23,860 --> 00:08:27,710
we are downloading this code only when it's needed.
153
00:08:27,710 --> 00:08:30,940
That's the entire idea behind lazy loading.
154
00:08:30,940 --> 00:08:34,620
But the problem with that of course, is that this download
155
00:08:34,620 --> 00:08:39,020
can take a couple of milliseconds or even seconds maybe.
156
00:08:39,020 --> 00:08:41,429
Now, whilst we're downloading this code,
157
00:08:41,429 --> 00:08:44,420
React is of course not able to continue,
158
00:08:44,420 --> 00:08:46,520
we can't load this component yet
159
00:08:46,520 --> 00:08:48,790
until the download completed.
160
00:08:48,790 --> 00:08:52,432
And that's why we need to define a fallback UI,
161
00:08:52,432 --> 00:08:55,610
some fallback content which can be shown
162
00:08:55,610 --> 00:08:58,610
if this download takes a bit longer.
163
00:08:58,610 --> 00:09:02,520
And for this, we need to import another thing from React,
164
00:09:02,520 --> 00:09:06,160
we need to import the suspense component.
165
00:09:06,160 --> 00:09:10,233
A special component provided by the React library itself.
166
00:09:11,270 --> 00:09:14,560
We need to wrap this around the code,
167
00:09:14,560 --> 00:09:16,370
where we use React lazy.
168
00:09:16,370 --> 00:09:19,190
So for example here, ends out of the layout,
169
00:09:19,190 --> 00:09:22,310
we can now use this suspense component
170
00:09:22,310 --> 00:09:24,593
and wrap this around the switch statement.
171
00:09:25,530 --> 00:09:27,890
And then on this suspense component
172
00:09:27,890 --> 00:09:30,113
we need to set a fallback prop.
173
00:09:30,990 --> 00:09:35,160
Fallback once some JS X code as a value,
174
00:09:35,160 --> 00:09:37,010
which is shown as a fallback,
175
00:09:37,010 --> 00:09:40,440
for example a paragraph where we say loading,
176
00:09:40,440 --> 00:09:43,910
or this loading spinner maybe, which we also have.
177
00:09:43,910 --> 00:09:47,470
I could create a div with a class name of centered,
178
00:09:47,470 --> 00:09:49,900
that's a globally available class,
179
00:09:49,900 --> 00:09:52,880
and in there I will put my loading spinner,
180
00:09:52,880 --> 00:09:54,830
and for this of course you need to import
181
00:09:54,830 --> 00:09:57,000
the loading spinner, which is a component
182
00:09:57,000 --> 00:09:59,593
that already exists in this project.
183
00:10:01,360 --> 00:10:04,100
With this div, with this loading spinner
184
00:10:04,100 --> 00:10:05,950
would be shown as a fallback,
185
00:10:05,950 --> 00:10:09,230
if loading the code for this new quote component
186
00:10:09,230 --> 00:10:10,523
takes a bit longer.
187
00:10:11,730 --> 00:10:13,780
And now with this if we saved as,
188
00:10:13,780 --> 00:10:16,290
I can visit slash new quote,
189
00:10:16,290 --> 00:10:20,090
and we briefly sometimes see that loading spinner
190
00:10:20,090 --> 00:10:22,870
but typically we don't because downloading
191
00:10:22,870 --> 00:10:25,030
the code is roughly quick,
192
00:10:25,030 --> 00:10:28,480
and if I go to all quotes and I load this page,
193
00:10:28,480 --> 00:10:31,490
I'm always able to navigate to add a quote.
194
00:10:31,490 --> 00:10:35,160
Now what's interesting is that if you go to all quotes
195
00:10:35,160 --> 00:10:37,470
or to just local hosts, free thousand,
196
00:10:37,470 --> 00:10:42,290
and you reload this page, if you open the developer tools
197
00:10:42,290 --> 00:10:45,310
and go to network to see all the network requests
198
00:10:45,310 --> 00:10:47,010
that are being made,
199
00:10:47,010 --> 00:10:49,290
if you now click on add a quote,
200
00:10:49,290 --> 00:10:53,670
you see this request to this JavaScript file.
201
00:10:53,670 --> 00:10:55,610
The name might differ for you,
202
00:10:55,610 --> 00:10:58,040
but you see a request that is being sent
203
00:10:58,040 --> 00:11:00,230
to another JavaScript file.
204
00:11:00,230 --> 00:11:02,710
And that is that JavaScript file
205
00:11:02,710 --> 00:11:05,290
with the code that is related
206
00:11:05,290 --> 00:11:08,380
to this form and to handling this form.
207
00:11:08,380 --> 00:11:12,040
And this code is now only downloaded on the mode,
208
00:11:12,040 --> 00:11:14,420
when we visit, add a quote.
209
00:11:14,420 --> 00:11:16,563
That's lazy loading in action.
210
00:11:18,620 --> 00:11:20,920
Now, of course we're not restricted
211
00:11:20,920 --> 00:11:24,060
to using lazy loading for it and you quote page,
212
00:11:24,060 --> 00:11:26,700
we can also use it for a quote detail.
213
00:11:26,700 --> 00:11:30,270
Here we can define a quote detailed constant
214
00:11:30,270 --> 00:11:33,240
and use React lazy like we did it before,
215
00:11:33,240 --> 00:11:38,240
but now for ./pages quote detail.
216
00:11:38,450 --> 00:11:41,780
And now we can and we need to get rid of this input.
217
00:11:43,170 --> 00:11:44,440
We can of course do the same
218
00:11:44,440 --> 00:11:47,623
for the not found page React lazy,
219
00:11:49,270 --> 00:11:53,410
and then import pages not found
220
00:11:53,410 --> 00:11:58,150
and get rid of this import and so on for all the pages.
221
00:11:58,150 --> 00:12:01,070
We can also do this for all quotes,
222
00:12:01,070 --> 00:12:03,840
but of course if we assume that most users
223
00:12:03,840 --> 00:12:06,270
will land on this page anyways,
224
00:12:06,270 --> 00:12:08,600
loading it lazily is a bit redundant
225
00:12:08,600 --> 00:12:11,390
because the code will be needed anyways.
226
00:12:11,390 --> 00:12:14,050
But still since users could also start
227
00:12:14,050 --> 00:12:16,550
on the new quote page theoretically,
228
00:12:16,550 --> 00:12:20,150
we might wanna load this page lazily as well.
229
00:12:20,150 --> 00:12:25,150
So also import ./pages, all quotes here
230
00:12:25,430 --> 00:12:27,973
and get rid of the import down there or up there.
231
00:12:29,490 --> 00:12:32,320
And now all the code is loaded lazily,
232
00:12:32,320 --> 00:12:36,110
and our applications should still work fine here.
233
00:12:36,110 --> 00:12:39,410
If we reload this all still works
234
00:12:39,410 --> 00:12:43,740
but now it uses lazy loading to split our code.
235
00:12:43,740 --> 00:12:47,160
And lazy loading can be an important building block
236
00:12:47,160 --> 00:12:48,550
of an application therefore,
237
00:12:48,550 --> 00:12:51,683
because it makes that initial code bundles smaller.
238
00:12:52,930 --> 00:12:55,480
Now for a very simple applications
239
00:12:55,480 --> 00:13:00,170
like this one arguably still is, it is totally optional,
240
00:13:00,170 --> 00:13:04,600
but especially for large applications with lots of pages
241
00:13:04,600 --> 00:13:08,630
where all the pages then also have a lot of components
242
00:13:08,630 --> 00:13:10,580
and complex logic attached,
243
00:13:10,580 --> 00:13:14,520
in such applications lazy loading can really shine
244
00:13:14,520 --> 00:13:16,870
and can make the initial loading
245
00:13:16,870 --> 00:13:19,500
off your website much faster,
246
00:13:19,500 --> 00:13:22,740
because not all the code is loaded at once
247
00:13:22,740 --> 00:13:25,053
but only the code that's needed.
19910
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.