1
00:00:03,000 --> 00:00:06,248
In this video we'll talk about the difference

2
00:00:06,248 --> 00:00:09,279
between running our app in the dev server,

3
00:00:09,351 --> 00:00:11,588
and running it in production.

4
00:00:11,588 --> 00:00:14,183
We've already seen that, when running

5
00:00:14,183 --> 00:00:16,358
a local server with "next dev",

6
00:00:16,428 --> 00:00:19,742
every time we request a page in the browser

7
00:00:19,742 --> 00:00:22,594
it will be re-rendered by the server.

8
00:00:22,671 --> 00:00:25,299
The dev server is designed to

9
00:00:25,299 --> 00:00:27,473
help us develop our app.

10
00:00:27,564 --> 00:00:29,234
While we're working on the code

11
00:00:29,234 --> 00:00:32,008
we want any changes we make to be visible

12
00:00:32,008 --> 00:00:34,375
in the browser as soon as possible.

13
00:00:34,443 --> 00:00:37,428
That's why the dev server regenerates

14
00:00:37,428 --> 00:00:38,961
the page each time,

15
00:00:39,042 --> 00:00:42,092
based on the latest code in our project.

16
00:00:42,092 --> 00:00:45,768
But that's not how things work in production.

17
00:00:45,768 --> 00:00:47,983
Let's see how to build and run

18
00:00:47,983 --> 00:00:49,680
our app for production.

19
00:00:49,754 --> 00:00:51,781
I'll stop the dev server.

20
00:00:52,274 --> 00:00:54,060
And, if we look at the help

21
00:00:54,060 --> 00:00:55,779
for the "next" tool again,

22
00:00:55,845 --> 00:00:58,213
it provides a "build" command,

23
00:00:58,213 --> 00:01:00,207
that we can use to create an

24
00:01:00,207 --> 00:01:02,130
optimized production build.

25
00:01:02,201 --> 00:01:04,498
And also a "start" command,

26
00:01:04,498 --> 00:01:07,083
that lets us run the Next.js

27
00:01:07,083 --> 00:01:09,484
server in production mode.

28
00:01:09,576 --> 00:01:11,637
So, let's add some more scripts

29
00:01:11,637 --> 00:01:13,100
to our "package.json".

30
00:01:13,166 --> 00:01:16,385
We want "build", to run "next build".

31
00:01:16,846 --> 00:01:18,800
And then a "start" script,

32
00:01:19,706 --> 00:01:22,370
that will execute "next start".

33
00:01:22,370 --> 00:01:24,439
If we save, we can then run those

34
00:01:24,439 --> 00:01:25,943
scripts in the terminal.

35
00:01:26,006 --> 00:01:28,124
Let me maximize this panel, so

36
00:01:28,124 --> 00:01:30,030
we can see the full output.

37
00:01:30,100 --> 00:01:33,541
First, we want to execute "npm run build",

38
00:01:33,541 --> 00:01:36,219
that will generate an "optimized

39
00:01:36,219 --> 00:01:37,725
production build".

40
00:01:37,808 --> 00:01:39,731
As you can see, it's pretty quick.

41
00:01:39,731 --> 00:01:42,806
The build command prints a useful summary.

42
00:01:42,806 --> 00:01:45,263
You can see that it generated a route

43
00:01:45,263 --> 00:01:47,189
for each page in our project:

44
00:01:47,255 --> 00:01:51,986
the root path, "/about", "/reviews", and so on.

45
00:01:51,986 --> 00:01:54,707
And, if you notice, there is a little

46
00:01:54,707 --> 00:01:56,472
circle before each path.

47
00:01:56,545 --> 00:01:59,335
At the bottom, it explains that the

48
00:01:59,335 --> 00:02:01,885
circle means it's a static page.

49
00:02:01,965 --> 00:02:04,201
One that's "automatically

50
00:02:04,201 --> 00:02:06,437
rendered as static HTML".

51
00:02:06,526 --> 00:02:10,068
So, the build step generated static HTML

52
00:02:10,068 --> 00:02:12,902
files for each one of our pages.

53
00:02:12,990 --> 00:02:14,686
In fact, you can see that,

54
00:02:14,686 --> 00:02:16,252
during the build process

55
00:02:16,317 --> 00:02:19,349
it also rendered our HomePage component.

56
00:02:19,349 --> 00:02:22,562
It will have rendered all our components,

57
00:02:22,562 --> 00:02:24,913
both the pages and the layout.

58
00:02:24,991 --> 00:02:26,962
It's just that we only log

59
00:02:26,962 --> 00:02:28,932
a message in the HomePage.

60
00:02:29,007 --> 00:02:32,991
But that's how it generates the static HTML.

61
00:02:32,991 --> 00:02:36,257
Now, all these pages have been generated

62
00:02:36,257 --> 00:02:38,381
inside the ".next" folder.

63
00:02:38,463 --> 00:02:41,263
If we look under "server", and then "app",

64
00:02:41,263 --> 00:02:43,624
you can see that there are HTML

65
00:02:43,624 --> 00:02:45,453
files for all our pages:

66
00:02:45,529 --> 00:02:50,174
"about.html", "index.html", and so on.

67
00:02:50,174 --> 00:02:53,896
Let's take a quick look at "index.html".

68
00:02:53,896 --> 00:02:55,801
I'll format the document,

69
00:02:55,801 --> 00:02:58,036
it was generated as a single

70
00:02:58,036 --> 00:03:00,111
line to minimize its size.

71
00:03:00,191 --> 00:03:04,698
Anyway, you can see here that this HTML contains

72
00:03:04,698 --> 00:03:08,363
the elements rendered from our HomePage component,

73
00:03:08,363 --> 00:03:11,685
and also from the RootLayout component.

74
00:03:11,685 --> 00:03:16,238
So all our React components have been pre-rendered

75
00:03:16,238 --> 00:03:19,243
to HTML during the build process.

76
00:03:19,334 --> 00:03:23,101
This way the Next.js server can simply

77
00:03:23,101 --> 00:03:25,876
return that index.html file,

78
00:03:25,975 --> 00:03:29,496
whenever a client requests the home page.

79
00:03:29,496 --> 00:03:32,296
Don't worry about the exact structure

80
00:03:32,296 --> 00:03:34,036
of this ".next" folder.

81
00:03:34,112 --> 00:03:37,330
It's all done automatically by the "next" command.

82
00:03:37,330 --> 00:03:40,017
Now, let's see how to run our app.

83
00:03:40,017 --> 00:03:42,500
We can simply type "npm start",

84
00:03:42,500 --> 00:03:45,432
and this will launch the Next.js server.

85
00:03:45,432 --> 00:03:49,448
By default it runs on localhost on port 3000,

86
00:03:49,448 --> 00:03:51,652
just like the dev server.

87
00:03:51,652 --> 00:03:54,286
But with "next start" it's running

88
00:03:54,286 --> 00:03:55,758
in production mode.

89
00:03:55,836 --> 00:03:57,567
Let's open the home page,

90
00:03:57,567 --> 00:04:00,064
and I'll do a hard-reload, just to make

91
00:04:00,064 --> 00:04:02,561
sure the browser doesn't use the cache.

92
00:04:02,625 --> 00:04:04,772
The home page of course looks

93
00:04:04,772 --> 00:04:06,549
the same as in dev mode.

94
00:04:06,623 --> 00:04:09,011
However, if we reload the page,

95
00:04:09,011 --> 00:04:11,498
we don't see any messages printed

96
00:04:11,498 --> 00:04:12,929
to the server logs.

97
00:04:13,005 --> 00:04:15,370
So, the HomePage doesn't get

98
00:04:15,370 --> 00:04:17,314
re-rendered every time,

99
00:04:17,398 --> 00:04:20,156
like when running it in the dev server.

100
00:04:20,156 --> 00:04:24,397
As we've seen, the build step generated a static

101
00:04:24,397 --> 00:04:27,577
HTML file inside the ".next" folder,

102
00:04:27,666 --> 00:04:30,250
so the production server will simply

103
00:04:30,250 --> 00:04:32,117
return that existing file,

104
00:04:32,189 --> 00:04:34,649
which is faster than regenerating

105
00:04:34,649 --> 00:04:36,140
the page every time.

106
00:04:36,215 --> 00:04:40,034
The production server is optimized for speed.

107
00:04:40,034 --> 00:04:43,303
Let's take a look at the network requests as well.

108
00:04:43,303 --> 00:04:46,427
When we load the page from the production server,

109
00:04:46,427 --> 00:04:49,667
it should be faster than with the dev server,

110
00:04:49,667 --> 00:04:52,152
although this is a very simple page, and

111
00:04:52,152 --> 00:04:54,202
we're running everything locally,

112
00:04:54,264 --> 00:04:57,327
so it's not a particularly meaningful test.

113
00:04:57,327 --> 00:04:59,250
Now, since we're talking about

114
00:04:59,250 --> 00:05:01,172
running our app in production,

115
00:05:01,236 --> 00:05:04,313
there are actually a few different ways

116
00:05:04,313 --> 00:05:06,838
we can deploy a Next.js project.

117
00:05:06,917 --> 00:05:09,611
And it's better if you know from the start

118
00:05:09,611 --> 00:05:11,968
how you plan to make your application

119
00:05:11,968 --> 00:05:13,496
available to your users.

120
00:05:13,560 --> 00:05:16,353
What we've done in this video is run

121
00:05:16,353 --> 00:05:18,680
our app in the Node.js server.

122
00:05:18,758 --> 00:05:21,511
We did an "npm run build" first,

123
00:05:21,511 --> 00:05:24,797
and then "npm start" to launch the server,

124
00:05:24,797 --> 00:05:28,198
using our local Node.js installation.

125
00:05:28,198 --> 00:05:30,633
You could follow exactly the same

126
00:05:30,633 --> 00:05:32,699
process on a server machine,

127
00:05:32,773 --> 00:05:35,249
rather than on your local machine,

128
00:05:35,249 --> 00:05:39,046
you could then allow HTTP access to that server,

129
00:05:39,046 --> 00:05:42,587
and you'll have a website that people can visit.

130
00:05:42,587 --> 00:05:45,714
But there are easier solutions as well.

131
00:05:45,714 --> 00:05:50,074
Next.js is developed by a company called Vercel,

132
00:05:50,074 --> 00:05:52,560
that offers a cloud platform where,

133
00:05:52,560 --> 00:05:53,909
among other things,

134
00:05:53,980 --> 00:05:57,203
you can deploy your Next.js application.

135
00:05:57,203 --> 00:05:59,846
It's not the only platform that provides

136
00:05:59,846 --> 00:06:01,630
this kind of functionality.

137
00:06:01,696 --> 00:06:05,721
You can do something similar with AWS Amplify,

138
00:06:05,721 --> 00:06:07,384
Netlify, and so on.

139
00:06:07,471 --> 00:06:10,136
The advantage of these platforms is that

140
00:06:10,136 --> 00:06:12,950
you don't need to set up and maintain

141
00:06:12,950 --> 00:06:14,547
the servers yourself.

142
00:06:14,623 --> 00:06:17,383
If you want the greatest flexibility

143
00:06:17,383 --> 00:06:20,824
in terms of where you can deploy your website,

144
00:06:20,824 --> 00:06:24,677
then you can do a static HTML export.

145
00:06:24,677 --> 00:06:27,702
This lets you build your project into a folder

146
00:06:27,702 --> 00:06:30,943
containing only static HTML, JavaScript,

147
00:06:30,943 --> 00:06:32,239
and other files,

148
00:06:32,320 --> 00:06:34,636
and you can then upload that

149
00:06:34,636 --> 00:06:36,704
folder to any web server.

150
00:06:36,786 --> 00:06:40,825
But not all of the Next.js features are compatible

151
00:06:40,825 --> 00:06:44,387
with exporting your app as static HTML.

152
00:06:44,387 --> 00:06:46,251
As we'll see later in the course,

153
00:06:46,251 --> 00:06:48,792
some features require a server

154
00:06:48,792 --> 00:06:50,571
running with Node.js.

155
00:06:50,656 --> 00:06:53,019
In the first part of this course

156
00:06:53,019 --> 00:06:55,013
we'll only use static pages

157
00:06:55,086 --> 00:06:57,567
thatÂ are compatible with doing

158
00:06:57,567 --> 00:06:59,304
a static HTML export.

159
00:06:59,387 --> 00:07:02,537
I'll tell you when we start using features

160
00:07:02,537 --> 00:07:04,863
that require a Node.js runtime.

161
00:07:04,938 --> 00:07:08,068
In fact, once our app is a bit more complete,

162
00:07:08,068 --> 00:07:11,612
we'll also see how to deploy it to Vercel,

163
00:07:11,612 --> 00:07:13,553
or as a static website.

164
00:07:13,637 --> 00:07:15,623
The most important takeaway

165
00:07:15,623 --> 00:07:17,316
from this video is that

166
00:07:17,389 --> 00:07:19,691
when we run our app in dev mode

167
00:07:19,691 --> 00:07:22,429
some things work a bit differently

168
00:07:22,429 --> 00:07:25,086
from how they work in production.

169
00:07:25,167 --> 00:07:28,701
In particular, the dev server will re-render

170
00:07:28,701 --> 00:07:31,672
our pages every time we request them,

171
00:07:31,753 --> 00:07:34,728
but that's not the case in production,

172
00:07:34,728 --> 00:07:36,842
at least with static pages.

