Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:03,000 --> 00:00:06,348
We've seen how we can use the Link component
2
00:00:06,348 --> 00:00:10,056
to enable client-side navigation in our app,
3
00:00:10,056 --> 00:00:12,123
meaning that clicking on a link
4
00:00:12,123 --> 00:00:15,711
will immediately update the contents on the page,
5
00:00:15,711 --> 00:00:18,468
without requesting a separate HTML
6
00:00:18,468 --> 00:00:20,495
document from the server.
7
00:00:20,576 --> 00:00:24,107
I also mentioned something about "prefetching".
8
00:00:24,107 --> 00:00:27,389
If I simply move the mouse over the link,
9
00:00:27,389 --> 00:00:29,098
even without clicking it,
10
00:00:29,098 --> 00:00:32,229
Next.js makes a request to the server,
11
00:00:32,229 --> 00:00:35,751
fetching some data for the "/about" route.
12
00:00:35,751 --> 00:00:38,437
It does this on mouse over only
13
00:00:38,437 --> 00:00:40,690
when using the dev server.
14
00:00:40,776 --> 00:00:42,872
This is one of those things that
15
00:00:42,872 --> 00:00:44,705
are different in production.
16
00:00:44,771 --> 00:00:47,690
So let's see how it works in that case.
17
00:00:47,690 --> 00:00:49,562
I'll stop the dev server,
18
00:00:49,562 --> 00:00:51,818
and type "npm run build",
19
00:00:51,982 --> 00:00:55,215
to create a build based on our latest code.
20
00:00:55,362 --> 00:00:58,162
We can now start the production server,
21
00:00:58,162 --> 00:01:01,135
and let's go and see how our app works,
22
00:01:01,135 --> 00:01:03,724
if we restart from the home page.
23
00:01:03,724 --> 00:01:06,337
We see a few network requests.
24
00:01:06,337 --> 00:01:09,145
If we filter only HTML documents,
25
00:01:09,145 --> 00:01:10,677
by clicking "Doc",
26
00:01:10,762 --> 00:01:14,108
it loaded a single HTML document.
27
00:01:14,108 --> 00:01:16,419
If we select "Fetch" instead,
28
00:01:16,419 --> 00:01:18,765
we can see three requests here,
29
00:01:18,765 --> 00:01:22,018
one for each link in our navigation bar.
30
00:01:22,018 --> 00:01:25,286
The first one shows "localhost" as the name,
31
00:01:25,286 --> 00:01:27,523
but it's really the root path,
32
00:01:27,523 --> 00:01:29,088
that's the home page.
33
00:01:29,162 --> 00:01:34,285
So, in production Next.js prefetches all our links
34
00:01:34,285 --> 00:01:37,245
right after loading the initial page.
35
00:01:37,245 --> 00:01:40,531
But what are these requests exactly?
36
00:01:40,531 --> 00:01:43,361
Let's look at the reviews one for example.
37
00:01:43,361 --> 00:01:46,914
Even though the URL is "/reviews",
38
00:01:46,914 --> 00:01:50,797
this request didn't return an HTML document,
39
00:01:50,797 --> 00:01:53,514
but some special data used by
40
00:01:53,514 --> 00:01:55,762
React Server Components.
41
00:01:55,856 --> 00:01:57,557
If we look at the Response,
42
00:01:57,956 --> 00:02:00,089
we can see the full data here.
43
00:02:00,089 --> 00:02:02,700
It's in a rather strange format, that
44
00:02:02,700 --> 00:02:05,170
we don't really need to understand.
45
00:02:05,240 --> 00:02:07,300
The important thing is that it
46
00:02:07,300 --> 00:02:09,221
contains all the information
47
00:02:09,289 --> 00:02:13,734
required by Next.js to render the Reviews page,
48
00:02:13,734 --> 00:02:16,625
if the user navigates to that route.
49
00:02:16,625 --> 00:02:19,175
In fact, if we clear the history,
50
00:02:19,175 --> 00:02:20,951
and select All requests
51
00:02:21,029 --> 00:02:24,024
we can now go and click the Reviews link,
52
00:02:24,024 --> 00:02:26,273
and we see the Reviews content,
53
00:02:26,273 --> 00:02:28,328
without any further request
54
00:02:28,328 --> 00:02:30,232
being sent to the server.
55
00:02:30,308 --> 00:02:33,854
So, Next.js prefetches all our links,
56
00:02:33,854 --> 00:02:36,984
so that it has all the data already available
57
00:02:36,984 --> 00:02:39,356
to display each different route
58
00:02:39,356 --> 00:02:41,575
if the user clicks on a link.
59
00:02:41,651 --> 00:02:44,880
This way navigation happens instantly.
60
00:02:44,880 --> 00:02:49,286
It does also mean that our app loads some extra data,
61
00:02:49,286 --> 00:02:50,782
for all the links.
62
00:02:50,865 --> 00:02:54,305
That's usually fine, because that data
63
00:02:54,305 --> 00:02:57,474
is only loaded after the main HTML.
64
00:02:57,564 --> 00:02:59,280
So the user will still see
65
00:02:59,280 --> 00:03:00,930
the initial page quickly,
66
00:03:00,996 --> 00:03:02,957
and then the link data will be
67
00:03:02,957 --> 00:03:04,657
fetched in the background.
68
00:03:04,723 --> 00:03:07,734
But what if we don't want this behaviour?
69
00:03:07,734 --> 00:03:10,039
Suppose that, for example,
70
00:03:10,039 --> 00:03:12,345
our About page is very big
71
00:03:12,434 --> 00:03:16,274
and we know that very few people actually open it.
72
00:03:16,274 --> 00:03:19,569
We could avoid loading the data for this link
73
00:03:19,569 --> 00:03:23,291
by setting the "prefetch" prop to be "false".
74
00:03:23,291 --> 00:03:25,438
This will disable prefetching
75
00:03:25,438 --> 00:03:27,289
for this particular link.
76
00:03:27,363 --> 00:03:29,798
Let's see what this means in practice.
77
00:03:29,798 --> 00:03:32,642
We'll need to rebuild our app first,
78
00:03:32,642 --> 00:03:35,797
to include this change into the production build.
79
00:03:35,797 --> 00:03:38,632
Then we can run "npm start" again,
80
00:03:38,632 --> 00:03:41,465
and go and test our pages in the browser.
81
00:03:41,465 --> 00:03:44,519
I'll start from the root path as usual.
82
00:03:44,519 --> 00:03:47,089
Once again, there will be a single
83
00:03:47,089 --> 00:03:49,206
HTML document to start with.
84
00:03:49,282 --> 00:03:51,909
But if we look at the Fetch requests,
85
00:03:51,909 --> 00:03:54,020
you can see that in this case
86
00:03:54,020 --> 00:03:56,131
it only prefetched two links:
87
00:03:56,204 --> 00:04:00,091
"localhost" (that's the Home page), and "reviews".
88
00:04:00,091 --> 00:04:03,955
It didn't load any data for the About page,
89
00:04:03,955 --> 00:04:06,331
because we explicitly disabled
90
00:04:06,331 --> 00:04:08,390
prefetching for that link.
91
00:04:08,470 --> 00:04:10,788
In fact, if we clear the history,
92
00:04:10,830 --> 00:04:13,771
we can still open the Reviews link without
93
00:04:13,771 --> 00:04:15,661
making any further request,
94
00:04:15,731 --> 00:04:18,188
because that one was prefetched.
95
00:04:18,188 --> 00:04:20,714
But let's try the About link.
96
00:04:20,714 --> 00:04:23,001
Note that nothing happens when
97
00:04:23,001 --> 00:04:24,755
we move the mouse over,
98
00:04:24,831 --> 00:04:28,012
so it's still different from the dev server.
99
00:04:28,012 --> 00:04:30,325
But when we actually click the link
100
00:04:30,325 --> 00:04:32,311
it does make a new request,
101
00:04:32,311 --> 00:04:36,147
fetching the data required for the About route.
102
00:04:36,147 --> 00:04:38,517
The data is still in the React
103
00:04:38,517 --> 00:04:40,493
Server Components format,
104
00:04:40,572 --> 00:04:43,284
not a full HTML document.
105
00:04:43,284 --> 00:04:44,909
That makes it smaller.
106
00:04:44,909 --> 00:04:46,917
You can see that it's just
107
00:04:46,917 --> 00:04:48,770
1 kilobyte in this case.
108
00:04:48,847 --> 00:04:52,424
While if we request the full HTML page,
109
00:04:52,424 --> 00:04:54,521
by reloading this URL,
110
00:04:54,521 --> 00:04:55,901
under documents,
111
00:04:56,081 --> 00:04:59,786
the full HTML is 2 kilobytes.
112
00:04:59,786 --> 00:05:03,661
So the data used by React Server Components
113
00:05:03,661 --> 00:05:06,851
is only half the size of the HTML.
114
00:05:06,851 --> 00:05:10,331
It should still load faster than the full page,
115
00:05:10,331 --> 00:05:13,087
even without prefetching the link.
116
00:05:13,087 --> 00:05:15,562
Although this is a very simple page.
117
00:05:15,562 --> 00:05:19,299
Anyway, the key point is that in production
118
00:05:19,299 --> 00:05:22,125
by default Next.js prefetches all
119
00:05:22,125 --> 00:05:24,694
the links in our initial page,
120
00:05:24,780 --> 00:05:26,969
to make navigation faster.
121
00:05:26,969 --> 00:05:30,124
But you can disable this behaviour if you want to
122
00:05:30,124 --> 00:05:32,443
by setting the "prefetch" prop
123
00:05:32,443 --> 00:05:34,144
on the Link component.
8962
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.