Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,330 --> 00:00:04,180
So we added get static props
2
00:00:04,180 --> 00:00:06,000
with some dummy code,
3
00:00:06,000 --> 00:00:09,240
but at the moment we're really just,
4
00:00:09,240 --> 00:00:11,800
not doing anything that would require
5
00:00:11,800 --> 00:00:14,150
us to add that function.
6
00:00:14,150 --> 00:00:16,830
We could just prepare dummy data like this
7
00:00:16,830 --> 00:00:19,400
in the component function as well.
8
00:00:19,400 --> 00:00:22,420
Well, of course I started with such a simple example to
9
00:00:22,420 --> 00:00:25,720
just get started with get static props.
10
00:00:25,720 --> 00:00:30,260
A more realistic example would be that we have a data file
11
00:00:30,260 --> 00:00:32,960
like this dummy backend Json file
12
00:00:32,960 --> 00:00:34,853
from which we wanna load data.
13
00:00:35,690 --> 00:00:39,630
But we do wanna load the data when the page is prepared.
14
00:00:39,630 --> 00:00:41,410
We don't wanna reach out
15
00:00:41,410 --> 00:00:44,480
to that file through a HTTP request
16
00:00:44,480 --> 00:00:47,360
or anything like that from the client site.
17
00:00:47,360 --> 00:00:51,610
And therefore will now utilize the fact that any code inside
18
00:00:51,610 --> 00:00:56,610
of get static props is executed on the server site so to say
19
00:00:56,900 --> 00:00:59,487
with server site capabilities.
20
00:00:59,487 --> 00:01:02,400
And that means that we can now, for example,
21
00:01:02,400 --> 00:01:04,769
work with the file system.
22
00:01:04,769 --> 00:01:06,750
For this I'll add import at the top
23
00:01:06,750 --> 00:01:10,220
and I'll import Fs from Fs.
24
00:01:10,220 --> 00:01:14,873
Now this imports the file system module from node JS.
25
00:01:16,230 --> 00:01:18,160
This is not a third party package
26
00:01:18,160 --> 00:01:19,630
which we needed to install.
27
00:01:19,630 --> 00:01:23,580
It's one of the core node JS modules instead.
28
00:01:23,580 --> 00:01:26,190
And working with the Fs module,
29
00:01:26,190 --> 00:01:30,330
would fail if you try to do it on the client site,
30
00:01:30,330 --> 00:01:33,090
because browser site Java script
31
00:01:33,090 --> 00:01:35,790
can't access the file system.
32
00:01:35,790 --> 00:01:38,640
And especially not our project file system here
33
00:01:38,640 --> 00:01:43,260
because the entire project is not served to our visitors.
34
00:01:43,260 --> 00:01:45,770
But we can import this year nonetheless
35
00:01:45,770 --> 00:01:49,853
and use the file system module inside of get static prompts.
36
00:01:51,810 --> 00:01:53,980
So we can use Fs here.
37
00:01:53,980 --> 00:01:56,450
And next JS is very clever
38
00:01:56,450 --> 00:02:01,290
and sees which imports you only use in get static props
39
00:02:01,290 --> 00:02:04,650
or similar functions, which we'll learn about later.
40
00:02:04,650 --> 00:02:09,650
And then those imports are basically stripped out
41
00:02:09,720 --> 00:02:12,220
of the client site code bundle.
42
00:02:12,220 --> 00:02:14,470
So when the code for the client site,
43
00:02:14,470 --> 00:02:16,940
the react app code for the browser site,
44
00:02:16,940 --> 00:02:21,630
when that is prepared, that import will not be part of it.
45
00:02:21,630 --> 00:02:24,260
Next JS will ignore it for the client site,
46
00:02:24,260 --> 00:02:27,600
it will split your code in a clever way.
47
00:02:27,600 --> 00:02:30,420
So to component code in general will be part
48
00:02:30,420 --> 00:02:34,963
of the client site code, this import and this code won't be.
49
00:02:36,150 --> 00:02:40,030
And now we can use Fs to get access to this
50
00:02:40,030 --> 00:02:44,760
dummy backend json file with the read file
51
00:02:44,760 --> 00:02:47,350
or to read file sync function,
52
00:02:47,350 --> 00:02:50,620
read file sync synchronously reads the file
53
00:02:50,620 --> 00:02:54,030
and blocks the execution until it's done.
54
00:02:54,030 --> 00:02:58,950
Read file on the other end wants a call back to continue.
55
00:02:58,950 --> 00:03:00,730
And we can actually also import
56
00:03:00,730 --> 00:03:04,610
the file system from Fs/promises,
57
00:03:04,610 --> 00:03:07,630
which is a special version of this note JS module
58
00:03:07,630 --> 00:03:08,872
that uses promises.
59
00:03:08,872 --> 00:03:13,340
And when we do so read file actually returns a promise.
60
00:03:13,340 --> 00:03:15,320
And since this is an async function
61
00:03:15,320 --> 00:03:17,133
we can then just a wait this.
62
00:03:18,050 --> 00:03:20,300
Now read file just needs a path
63
00:03:20,300 --> 00:03:22,300
to the file which you wanna read.
64
00:03:22,300 --> 00:03:26,610
So we can construct this path here and this path
65
00:03:26,610 --> 00:03:28,610
or let's name it file path,
66
00:03:28,610 --> 00:03:32,940
this path should lead to the dummy backend Json file.
67
00:03:32,940 --> 00:03:37,123
Now to construct this path, we can import another module
68
00:03:38,180 --> 00:03:42,960
from node JS the path module from path.
69
00:03:42,960 --> 00:03:45,250
And that's a module with helpful
70
00:03:45,250 --> 00:03:48,263
functionalities for building paths.
71
00:03:49,395 --> 00:03:52,750
Now, here we can use path and execute to join method
72
00:03:52,750 --> 00:03:57,750
to build a path that can be consumed by read file.
73
00:03:57,850 --> 00:04:00,700
And here I wanna start from
74
00:04:00,700 --> 00:04:04,607
inside the current directory and then basically work my way
75
00:04:04,607 --> 00:04:09,520
to this data directory and the dummy backend Json files.
76
00:04:09,520 --> 00:04:12,884
Now for this, we basically wanna dive into the data folder
77
00:04:12,884 --> 00:04:17,130
and then grab this dummy backend Json file.
78
00:04:17,130 --> 00:04:21,560
And to do this, we, first of all needs to tell path join
79
00:04:21,560 --> 00:04:23,700
in which path we're starting
80
00:04:23,700 --> 00:04:26,470
and we can get to current working directory
81
00:04:26,470 --> 00:04:31,470
by using another node JS object or concept.
82
00:04:31,650 --> 00:04:34,330
And that's the process object
83
00:04:34,330 --> 00:04:38,330
which is globally available in node JS.
84
00:04:38,330 --> 00:04:42,750
And then on process, we can execute the CWD method
85
00:04:42,750 --> 00:04:45,800
which stands for current working directory.
86
00:04:45,800 --> 00:04:48,380
And this gives you the current working directory
87
00:04:48,380 --> 00:04:51,380
off this code file when it's executed.
88
00:04:51,380 --> 00:04:54,420
Now, the important thing here is that this current
89
00:04:54,420 --> 00:04:57,590
working directory will not be the pages folder.
90
00:04:57,590 --> 00:04:59,860
Instead when this file is executed,
91
00:04:59,860 --> 00:05:02,190
next JS will be executing it,
92
00:05:02,190 --> 00:05:06,020
and it will basically treat all the files as if they sit
93
00:05:06,020 --> 00:05:09,090
in our root project folder.
94
00:05:09,090 --> 00:05:10,920
So the current working directory
95
00:05:10,920 --> 00:05:13,160
will be the overall project folder
96
00:05:13,160 --> 00:05:14,690
instead of the pages folder.
97
00:05:14,690 --> 00:05:16,500
That's important to know.
98
00:05:16,500 --> 00:05:19,290
So with that, we tell node JS
99
00:05:19,290 --> 00:05:22,350
or the path joined methods to be precise,
100
00:05:22,350 --> 00:05:27,070
that we start in this overall project directory.
101
00:05:27,070 --> 00:05:28,250
Then we add a comma.
102
00:05:28,250 --> 00:05:30,470
And the next argument is data
103
00:05:30,470 --> 00:05:33,750
because we wanna dive into the data folder.
104
00:05:33,750 --> 00:05:36,600
We wanna build a path which starts
105
00:05:36,600 --> 00:05:38,290
at the current working directory.
106
00:05:38,290 --> 00:05:40,650
So to project directory and then dives
107
00:05:40,650 --> 00:05:42,350
into the data directory.
108
00:05:42,350 --> 00:05:44,320
And then we add another argument.
109
00:05:44,320 --> 00:05:47,150
We can add an infinite amount of arguments here,
110
00:05:47,150 --> 00:05:50,800
which holds the file name we wanna use.
111
00:05:50,800 --> 00:05:53,020
So in this case, dummybackend.Json
112
00:05:54,350 --> 00:05:58,130
This now builds an absolute path to this file,
113
00:05:58,130 --> 00:06:02,090
and that's now the path which we pass to read file.
114
00:06:02,090 --> 00:06:04,283
So this will now read this file.
115
00:06:05,420 --> 00:06:10,040
So with that, we get our Json data from that Json file
116
00:06:10,040 --> 00:06:12,160
and now to get our actual data
117
00:06:12,160 --> 00:06:16,670
we can call Json pars on the Json data.
118
00:06:16,670 --> 00:06:20,070
Json is a globally available object just
119
00:06:20,070 --> 00:06:21,730
as it is on the browser site
120
00:06:21,730 --> 00:06:24,020
you can use it in node JS as well
121
00:06:24,020 --> 00:06:26,720
and this (indistinct) Json data
122
00:06:26,720 --> 00:06:29,913
it converts it into a regular JavaScript object.
123
00:06:30,770 --> 00:06:34,790
And now that data will be an object with a product key,
124
00:06:34,790 --> 00:06:35,913
which holds an array.
125
00:06:37,080 --> 00:06:39,500
So now we can return our props,
126
00:06:39,500 --> 00:06:41,860
which are now based on that data
127
00:06:41,860 --> 00:06:44,559
and the products, which are part of my props
128
00:06:44,559 --> 00:06:47,320
are now data.products.
129
00:06:47,320 --> 00:06:51,140
So the products I fetched from this file are now passed
130
00:06:51,140 --> 00:06:54,850
as a value to products here in this object
131
00:06:54,850 --> 00:06:57,510
and our data for all ultimately passed (indistinct)
132
00:06:57,510 --> 00:06:59,860
props to dis component function
133
00:06:59,860 --> 00:07:03,830
when next JS executes disfunction for us.
134
00:07:03,830 --> 00:07:07,340
But it will first of all, go through that function.
135
00:07:07,340 --> 00:07:09,705
And hence, now if we save everything
136
00:07:09,705 --> 00:07:12,850
and you reload local hosts free thousand,
137
00:07:12,850 --> 00:07:15,840
you'll see free products here.
138
00:07:15,840 --> 00:07:18,010
Again, if you viewed a page source,
139
00:07:18,010 --> 00:07:21,340
you see that all the data is there right from the start.
140
00:07:21,340 --> 00:07:22,670
Here it is,
141
00:07:22,670 --> 00:07:24,630
so it was pre rendered
142
00:07:24,630 --> 00:07:28,429
And was pre-rendered with help of get static props
143
00:07:28,429 --> 00:07:31,701
which now executes a bunch of server site code
144
00:07:31,701 --> 00:07:34,530
that never reaches the client.
145
00:07:34,530 --> 00:07:39,473
It's only executed when next JS pre renders our page.
11426
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.