Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,241 --> 00:00:03,540
Now step number two,
2
00:00:03,540 --> 00:00:06,490
is about those environment variables.
3
00:00:06,490 --> 00:00:08,750
And that's a very useful feature,
4
00:00:08,750 --> 00:00:12,200
since it allows us to use different values
5
00:00:12,200 --> 00:00:16,700
in parts of our code, during development and production.
6
00:00:16,700 --> 00:00:21,080
And Next.js has built-in support for environment variables.
7
00:00:21,080 --> 00:00:23,403
Now to explore them, I actually wanna explore
8
00:00:23,403 --> 00:00:25,750
another of concept first though.
9
00:00:25,750 --> 00:00:28,850
We can configure Next.js.
10
00:00:28,850 --> 00:00:31,730
Up to this point, this Next.js project
11
00:00:31,730 --> 00:00:36,100
worked with all the default settings Next.js assumes.
12
00:00:36,100 --> 00:00:40,370
And these are a sensible default, which often makes sense.
13
00:00:40,370 --> 00:00:43,120
But maybe you want to fine-tune them.
14
00:00:43,120 --> 00:00:44,510
And you can do this by adding
15
00:00:44,510 --> 00:00:47,270
a special file to your project
16
00:00:47,270 --> 00:00:51,330
a next.config.js file.
17
00:00:51,330 --> 00:00:54,940
This is a special file which has to be named like this
18
00:00:54,940 --> 00:00:58,020
and which has to sit in your root project folder,
19
00:00:58,020 --> 00:00:58,970
if you add it.
20
00:00:58,970 --> 00:01:02,490
And it's a file, which exports a JavaScript object
21
00:01:02,490 --> 00:01:06,010
with this Node.js export syntax.
22
00:01:06,010 --> 00:01:07,920
If you never worked with Node.js
23
00:01:07,920 --> 00:01:10,080
you might not know this syntax.
24
00:01:10,080 --> 00:01:14,050
This is in the end the equivalent to export default
25
00:01:14,050 --> 00:01:17,650
in front-end JavaScript so to say.
26
00:01:17,650 --> 00:01:20,810
So we export such an object here
27
00:01:20,810 --> 00:01:22,120
and that's now our object
28
00:01:22,120 --> 00:01:25,603
which allows you to set various configuration options.
29
00:01:26,470 --> 00:01:28,760
Now, if you Google for next config
30
00:01:28,760 --> 00:01:31,090
you'll find your official documentation
31
00:01:31,090 --> 00:01:32,900
on that config file.
32
00:01:32,900 --> 00:01:35,450
And here you learn all about that file
33
00:01:35,450 --> 00:01:38,010
and what you can configure there.
34
00:01:38,010 --> 00:01:42,080
You can configure a lot here, but for most of the settings
35
00:01:42,080 --> 00:01:44,290
the defaults are fine.
36
00:01:44,290 --> 00:01:46,970
But you can, for example add environment variables,
37
00:01:46,970 --> 00:01:48,570
something we'll do in a second.
38
00:01:48,570 --> 00:01:50,210
You could add a basePath
39
00:01:50,210 --> 00:01:54,100
if you're hosting your next website,
40
00:01:54,100 --> 00:01:56,310
not on the root level of some domain,
41
00:01:56,310 --> 00:01:58,310
but in some nested path.
42
00:01:58,310 --> 00:02:00,900
You can set up rewrites and redirects,
43
00:02:00,900 --> 00:02:03,090
if you have some hard-coded redirects,
44
00:02:03,090 --> 00:02:05,160
which should be taken into account
45
00:02:05,160 --> 00:02:09,380
when the application is deployed and much more.
46
00:02:09,380 --> 00:02:12,450
You can inject your own Webpack configuration
47
00:02:12,450 --> 00:02:15,720
to really tap into that build process.
48
00:02:15,720 --> 00:02:19,640
You can disable compression, it's enabled by default,
49
00:02:19,640 --> 00:02:22,310
but you can disable it if you don't wanna use it
50
00:02:22,310 --> 00:02:24,060
for whatever reason.
51
00:02:24,060 --> 00:02:27,210
Compression will make your JavaScript files
52
00:02:27,210 --> 00:02:28,470
and so on smaller.
53
00:02:28,470 --> 00:02:32,550
So you should enable it by default and much more.
54
00:02:32,550 --> 00:02:34,740
So you see, there are a couple of things you can set,
55
00:02:34,740 --> 00:02:39,620
but to be honest most of the settings will rarely be needed.
56
00:02:39,620 --> 00:02:42,600
And you will know when you need them.
57
00:02:42,600 --> 00:02:45,630
The most helpful settings are probably redirects
58
00:02:45,630 --> 00:02:48,340
and custom headers and environment variables
59
00:02:48,340 --> 00:02:50,280
and the basePath.
60
00:02:50,280 --> 00:02:52,930
For this project though, we need no rewrites.
61
00:02:52,930 --> 00:02:55,110
We don't need to change the basePath.
62
00:02:55,110 --> 00:02:57,200
We don't need any custom headers
63
00:02:57,200 --> 00:03:00,560
but we will explore environment variables.
64
00:03:00,560 --> 00:03:05,220
And the idea really is that in our API route here
65
00:03:05,220 --> 00:03:08,820
we swap out some of these values here
66
00:03:08,820 --> 00:03:12,330
from this connection string for dynamic values,
67
00:03:12,330 --> 00:03:16,260
which can then differ between development and production.
68
00:03:16,260 --> 00:03:19,400
So that we can have a development database connection
69
00:03:19,400 --> 00:03:21,390
and a production database.
70
00:03:21,390 --> 00:03:25,550
And we don't delete or manipulate production data
71
00:03:25,550 --> 00:03:28,380
with our development tests.
72
00:03:28,380 --> 00:03:32,310
And to make that work, I'll go to that next config file
73
00:03:32,310 --> 00:03:34,930
and add the env key.
74
00:03:34,930 --> 00:03:38,560
That's also what you see in the official docs here.
75
00:03:38,560 --> 00:03:42,670
This allows you to set up key value pairs of your choices
76
00:03:42,670 --> 00:03:47,220
and you will then be able to use your keys in your code
77
00:03:47,220 --> 00:03:52,220
both in the API routes, as well as in any other component.
78
00:03:53,040 --> 00:03:56,020
So you can use your environment variables
79
00:03:56,020 --> 00:03:58,023
anywhere in your code base.
80
00:03:59,270 --> 00:04:03,940
So here all of the setup a couple of environment variables
81
00:04:03,940 --> 00:04:08,670
and I'll start with MongoDB username.
82
00:04:08,670 --> 00:04:10,880
Now how you named that variable is up to you
83
00:04:10,880 --> 00:04:14,430
but I'll name it like this and set this to Maximilian.
84
00:04:14,430 --> 00:04:19,430
Then add the MongoDB password and set this to the password
85
00:04:19,930 --> 00:04:24,410
I have here in my connection string
86
00:04:25,980 --> 00:04:29,493
then I'll add the MongoDB cluster name.
87
00:04:30,400 --> 00:04:35,400
And here I will use this cluster zero thing here,
88
00:04:36,220 --> 00:04:39,470
so that if I had multiple clusters in my account
89
00:04:39,470 --> 00:04:42,940
I can use different clusters for different environments.
90
00:04:42,940 --> 00:04:44,920
So I'll add this here.
91
00:04:44,920 --> 00:04:49,587
And then I'll also add the MongoDB database key.
92
00:04:51,200 --> 00:04:53,400
And here, I wanna grab that database
93
00:04:53,400 --> 00:04:58,400
to which I'm connecting my-site and set this here.
94
00:04:58,890 --> 00:05:01,040
Now you might be wondering why I'm doing this here.
95
00:05:01,040 --> 00:05:02,880
What's the benefit.
96
00:05:02,880 --> 00:05:07,560
The benefit is that now we can tap into those values.
97
00:05:07,560 --> 00:05:09,150
We'll do that in a second.
98
00:05:09,150 --> 00:05:11,730
And when we then deploy our application
99
00:05:11,730 --> 00:05:14,330
we can override these values
100
00:05:14,330 --> 00:05:16,120
and you'll see how we can override them
101
00:05:16,120 --> 00:05:19,000
in a couple of minutes, once we deploy.
102
00:05:19,000 --> 00:05:21,010
And therefore we now need to switch
103
00:05:21,010 --> 00:05:23,810
to using environment variables.
104
00:05:23,810 --> 00:05:25,810
So back in the API route,
105
00:05:25,810 --> 00:05:30,810
I'll now set up my connection string here
106
00:05:31,330 --> 00:05:33,250
in a separate constant simply to make it
107
00:05:33,250 --> 00:05:34,890
a bit easier to work with it.
108
00:05:34,890 --> 00:05:37,210
And I'll set up a template literal here
109
00:05:37,210 --> 00:05:41,920
because now I need to mix hard-coded and dynamic values.
110
00:05:41,920 --> 00:05:46,920
And I'll still start with that MongoDB plus SRV part here
111
00:05:47,620 --> 00:05:49,330
but then after the two slashes
112
00:05:49,330 --> 00:05:54,230
I wanna inject the value from my environment variable
113
00:05:54,230 --> 00:05:58,123
from that MongoDB username environment variable.
114
00:05:59,650 --> 00:06:01,600
And here we see in the official docs
115
00:06:01,600 --> 00:06:03,860
that we get access to those values
116
00:06:03,860 --> 00:06:08,220
by tapping into the global process variable
117
00:06:08,220 --> 00:06:12,530
which is exposed by Node.js, then dote env
118
00:06:12,530 --> 00:06:15,470
and then into the key name of our choice.
119
00:06:15,470 --> 00:06:18,060
Now this placeholder will be replaced
120
00:06:18,060 --> 00:06:21,570
with the concrete value during the build process.
121
00:06:21,570 --> 00:06:23,040
That's important.
122
00:06:23,040 --> 00:06:25,673
So this will not be resolved to dynamically
123
00:06:25,673 --> 00:06:27,810
after we built the app
124
00:06:27,810 --> 00:06:31,590
but it will be swapped out during the build process.
125
00:06:31,590 --> 00:06:33,270
And that allows us to replace it
126
00:06:33,270 --> 00:06:35,580
with our environment variable value.
127
00:06:35,580 --> 00:06:37,890
And it allows us to use different values
128
00:06:37,890 --> 00:06:41,550
for building, for production, then during development.
129
00:06:41,550 --> 00:06:44,160
But again, we're going to see this in a second.
130
00:06:44,160 --> 00:06:47,600
So they are for now in contact.js here.
131
00:06:47,600 --> 00:06:51,510
I now want to use process.env.mongodb username here.
132
00:06:55,150 --> 00:06:58,530
Then we have a colon here.
133
00:06:58,530 --> 00:07:01,680
So this is hard-coded and there after the password
134
00:07:01,680 --> 00:07:05,283
which we get from process.env.mongodb_password.
135
00:07:07,660 --> 00:07:09,720
And here I'm using the keys.
136
00:07:09,720 --> 00:07:14,193
I have set up in my env part of that configuration.
137
00:07:15,100 --> 00:07:19,490
Now we also wanna replace the cluster and the database.
138
00:07:19,490 --> 00:07:22,020
So back here in contact.js.
139
00:07:22,020 --> 00:07:27,020
Now we have a @ after the password
140
00:07:28,240 --> 00:07:29,780
and then the cluster name.
141
00:07:29,780 --> 00:07:34,780
So here, I wanna tap into process.env.mongodb cluster name
142
00:07:36,150 --> 00:07:40,070
then thereafter, I have this hard-coded part.
143
00:07:40,070 --> 00:07:42,350
So I'll add that here again.
144
00:07:42,350 --> 00:07:47,350
And then inject process.env.mongodb database.
145
00:07:53,020 --> 00:07:57,507
And then there after add this query parameter like this.
146
00:08:00,080 --> 00:08:03,770
Okay, so now that's our dynamic connection string.
147
00:08:03,770 --> 00:08:06,680
That's now what I'll use here for connecting
148
00:08:06,680 --> 00:08:08,150
that connection string.
149
00:08:08,150 --> 00:08:11,140
And now here we use environment variables.
150
00:08:11,140 --> 00:08:12,860
Now that should work.
151
00:08:12,860 --> 00:08:17,860
If I now start the development server again with NPM run dev
152
00:08:18,060 --> 00:08:21,980
we should again be able to successfully test everything.
153
00:08:21,980 --> 00:08:26,980
Specifically I should still be able to send a message here.
154
00:08:27,500 --> 00:08:30,140
This should still work.
155
00:08:30,140 --> 00:08:33,313
Let's see sent message success.
156
00:08:34,600 --> 00:08:37,650
And we got no error here either.
157
00:08:37,650 --> 00:08:41,419
So that works and now we're using environment variables.
158
00:08:41,419 --> 00:08:44,290
Now, environment variables are only helpful
159
00:08:44,290 --> 00:08:47,120
if we don't always use the same values.
160
00:08:47,120 --> 00:08:49,430
And at the moment, the only thing we did
161
00:08:49,430 --> 00:08:52,770
is that we moved these hard-coded values
162
00:08:52,770 --> 00:08:56,240
from contact.js into next.config.js
163
00:08:56,240 --> 00:08:59,290
But there they are still always the same
164
00:08:59,290 --> 00:09:01,300
at the moment we don't differentiate
165
00:09:01,300 --> 00:09:03,893
between production and development.
166
00:09:04,730 --> 00:09:07,630
Now there will be certain hosting providers
167
00:09:07,630 --> 00:09:11,190
which allow us to push our code to them
168
00:09:11,190 --> 00:09:13,960
and then they will do the build for us.
169
00:09:13,960 --> 00:09:18,000
And then there we can define different environment variables
170
00:09:18,000 --> 00:09:20,660
which they should use during the build process.
171
00:09:20,660 --> 00:09:23,820
And we'll see an example for this later
172
00:09:23,820 --> 00:09:27,510
but we're not necessarily always using a hosting provider
173
00:09:27,510 --> 00:09:29,740
that does to build for us.
174
00:09:29,740 --> 00:09:32,490
And therefore for in next.config.js
175
00:09:32,490 --> 00:09:35,470
we can actually define different values
176
00:09:35,470 --> 00:09:37,530
for the environment variables,
177
00:09:37,530 --> 00:09:40,850
for the different phases we might be in.
178
00:09:40,850 --> 00:09:43,980
To be precise, when we run NPM run dev
179
00:09:45,260 --> 00:09:47,110
to start the development server
180
00:09:47,110 --> 00:09:49,900
we clearly are in development.
181
00:09:49,900 --> 00:09:53,130
Our current phase is development.
182
00:09:53,130 --> 00:09:56,270
If we run NPM run build though
183
00:09:56,270 --> 00:09:59,500
to trigger the next build script,
184
00:09:59,500 --> 00:10:01,490
or if we run the export script
185
00:10:01,490 --> 00:10:04,000
which I explained a couple of minutes ago
186
00:10:04,000 --> 00:10:07,580
then we are clearly building for production
187
00:10:07,580 --> 00:10:11,510
because that's the idea behind next build and next export.
188
00:10:11,510 --> 00:10:14,100
So then our face is production.
189
00:10:14,100 --> 00:10:17,060
We want to use the production values.
190
00:10:17,060 --> 00:10:21,200
And because we have this clear way of telling Next.js
191
00:10:21,200 --> 00:10:24,870
whether we're building for development or production.
192
00:10:24,870 --> 00:10:27,100
Next.js allows us to define
193
00:10:27,100 --> 00:10:29,900
different sets of configuration values
194
00:10:29,900 --> 00:10:32,283
for development and production.
195
00:10:33,260 --> 00:10:35,470
For this we just need to import something.
196
00:10:35,470 --> 00:10:39,230
And we do this with this strange looking import syntax
197
00:10:39,230 --> 00:10:42,910
which is the default Node.js import syntax.
198
00:10:42,910 --> 00:10:45,810
And we have to use it here in this config file
199
00:10:45,810 --> 00:10:49,360
because this config file is read in a way
200
00:10:49,360 --> 00:10:52,513
that other import statements won't work yet.
201
00:10:53,370 --> 00:10:56,750
Here we need to import from next constants
202
00:10:57,880 --> 00:11:00,080
and make sure you import exactly like this
203
00:11:00,080 --> 00:11:01,730
with the require function
204
00:11:01,730 --> 00:11:04,070
on the right side of the equal sign.
205
00:11:04,070 --> 00:11:07,420
And then here we can import different phases
206
00:11:07,420 --> 00:11:10,990
to be precise we have to face development server,
207
00:11:10,990 --> 00:11:15,070
which will be used if we run NPM run dev.
208
00:11:15,070 --> 00:11:17,930
We have the phase export which will be used
209
00:11:17,930 --> 00:11:21,570
if we do that export command.
210
00:11:21,570 --> 00:11:25,040
And then we got the production build and production server,
211
00:11:25,040 --> 00:11:29,390
production build will be used if we run NPM run build
212
00:11:29,390 --> 00:11:33,160
and production server will be used after that build
213
00:11:33,160 --> 00:11:38,150
for the server side code once our server is up and running.
214
00:11:38,150 --> 00:11:41,350
So what we could do here is we can import
215
00:11:41,350 --> 00:11:46,160
that phase development server and then utilize the fact
216
00:11:46,160 --> 00:11:50,670
that our export the thing here in this config file
217
00:11:50,670 --> 00:11:54,880
can be an object but can also be a function.
218
00:11:54,880 --> 00:11:58,170
And I'll change this to a function, to a function
219
00:11:58,170 --> 00:12:01,890
which returns this configuration object then.
220
00:12:01,890 --> 00:12:05,580
So we moved that into this returned object now
221
00:12:05,580 --> 00:12:09,210
and I'm doing this because when using the function form
222
00:12:09,210 --> 00:12:13,020
we can add a if check to check in which phase we are.
223
00:12:13,020 --> 00:12:14,950
So if this configuration is loaded
224
00:12:14,950 --> 00:12:18,430
for the development server or for production.
225
00:12:18,430 --> 00:12:21,000
And we can find out in which phase we are
226
00:12:21,000 --> 00:12:25,350
because there's function here will be executed by Next.js
227
00:12:25,350 --> 00:12:29,480
and it will get the phase parameter.
228
00:12:29,480 --> 00:12:31,340
And then we can check if the phase
229
00:12:31,340 --> 00:12:34,340
for which this config file is being read in.
230
00:12:34,340 --> 00:12:37,340
If that is equal to phase development server
231
00:12:38,460 --> 00:12:40,470
and then we can say, if that's the case
232
00:12:40,470 --> 00:12:44,330
if we are in development then we wanna return
233
00:12:44,330 --> 00:12:48,210
a different config object than we do otherwise.
234
00:12:48,210 --> 00:12:53,210
So now here I'll grab these settings and add them here.
235
00:12:53,290 --> 00:12:55,520
And now that's my configuration object
236
00:12:55,520 --> 00:13:00,170
for this Next.js project, if we are in development.
237
00:13:00,170 --> 00:13:04,320
If we make it past this if check, we are not in development
238
00:13:04,320 --> 00:13:05,720
and in all those cases
239
00:13:05,720 --> 00:13:08,810
I could now set up different values here.
240
00:13:08,810 --> 00:13:11,480
For example, I could use a different database.
241
00:13:11,480 --> 00:13:14,290
I could say here, I'm not using my-site
242
00:13:14,290 --> 00:13:16,880
but my-site-dev during development.
243
00:13:16,880 --> 00:13:20,040
And then I use my-site in production.
244
00:13:20,040 --> 00:13:23,680
If we change everything like that, and we save this file.
245
00:13:23,680 --> 00:13:27,180
If I start the development server again with NPM run dev,
246
00:13:27,180 --> 00:13:31,970
then we'll see that if I reload this context page
247
00:13:31,970 --> 00:13:36,970
and I now do test this again, it should still work.
248
00:13:39,250 --> 00:13:43,700
But if I then log into MongoDB and explore my collections,
249
00:13:43,700 --> 00:13:47,960
what we'll see is that once this loaded here
250
00:13:47,960 --> 00:13:51,460
that we now have this my-site-dev database
251
00:13:51,460 --> 00:13:53,350
with a messages collection
252
00:13:53,350 --> 00:13:56,540
and that collection contains our one message.
253
00:13:56,540 --> 00:13:58,490
For some reason it doesn't load him for me
254
00:13:58,490 --> 00:14:00,910
but we can see that there's one document in there
255
00:14:00,910 --> 00:14:04,430
and we can clearly see that it's the right database name.
256
00:14:04,430 --> 00:14:07,180
So now we're using this during development
257
00:14:07,180 --> 00:14:10,070
and we'll use different settings for production.
258
00:14:10,070 --> 00:14:12,960
And that allows us to not accidentally override
259
00:14:12,960 --> 00:14:17,840
or mess with data from our real users during development.
260
00:14:17,840 --> 00:14:21,280
And that is why environment variables can be very useful
261
00:14:21,280 --> 00:14:24,063
and are another feature you should be aware of.
21132
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.