Skip to content

Commit 8c00af1

Browse files
leogaearon
authored andcommitted
Suggest serve for running in production (#1760)
* Suggest `serve` for serving the `build` directory * How to handle it with Node in prod (or other platforms) * Pretty newline added * Adjusted default port of static server * Remove `open` command from output * Removed constant assignment * Better explanation for not using having to use a static server * Cute newline added * Style nits
1 parent 4bc9e79 commit 8c00af1

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

packages/react-scripts/scripts/build.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ function build(previousFileSizes) {
101101
printFileSizesAfterBuild(stats, previousFileSizes);
102102
console.log();
103103

104-
const openCommand = process.platform === 'win32' ? 'start' : 'open';
105104
const appPackage = require(paths.appPackageJson);
106105
const publicUrl = paths.publicUrl;
107106
const publicPath = config.output.publicPath;
@@ -184,17 +183,14 @@ function build(previousFileSizes) {
184183
}
185184
const build = path.relative(process.cwd(), paths.appBuild);
186185
console.log(`The ${chalk.cyan(build)} folder is ready to be deployed.`);
187-
console.log('You may also serve it locally with a static server:');
186+
console.log('You may serve it with a static server:');
188187
console.log();
189188
if (useYarn) {
190-
console.log(` ${chalk.cyan('yarn')} global add pushstate-server`);
189+
console.log(` ${chalk.cyan('yarn')} global add serve`);
191190
} else {
192-
console.log(` ${chalk.cyan('npm')} install -g pushstate-server`);
191+
console.log(` ${chalk.cyan('npm')} install -g serve`);
193192
}
194-
console.log(` ${chalk.cyan('pushstate-server')} build`);
195-
console.log(
196-
` ${chalk.cyan(openCommand)} http://localhost:${process.env.PORT || 9000}`
197-
);
193+
console.log(` ${chalk.cyan('serve')} -s build`);
198194
console.log();
199195
}
200196
});

packages/react-scripts/template/README.md

+25-5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ You can find the most recent version of this guide [here](https://github.com/fac
6262
- [Developing Components in Isolation](#developing-components-in-isolation)
6363
- [Making a Progressive Web App](#making-a-progressive-web-app)
6464
- [Deployment](#deployment)
65+
- [Static Server](#static-server)
66+
- [Other Solutions](#other-solutions)
6567
- [Serving Apps with Client-Side Routing](#serving-apps-with-client-side-routing)
6668
- [Building for Relative Paths](#building-for-relative-paths)
6769
- [Azure](#azure)
@@ -1210,14 +1212,30 @@ You can turn your React app into a [Progressive Web App](https://developers.goog
12101212

12111213
## Deployment
12121214

1213-
`npm run build` creates a `build` directory with a production build of your app. Set up your favourite HTTP server so that a visitor to your site is served `index.html`, and requests to static paths like `/static/js/main.<hash>.js` are served with the contents of the `/static/js/main.<hash>.js` file. For example, Python contains a built-in HTTP server that can serve static files:
1215+
`npm run build` creates a `build` directory with a production build of your app. Set up your favourite HTTP server so that a visitor to your site is served `index.html`, and requests to static paths like `/static/js/main.<hash>.js` are served with the contents of the `/static/js/main.<hash>.js` file.
1216+
1217+
### Static Server
1218+
1219+
For environments using [Node](https://nodejs.org/), the easiest way to handle this would be to install [serve](https://github.com/zeit/serve) and let it handle the rest:
1220+
1221+
```sh
1222+
npm install -g serve
1223+
serve -s build
1224+
```
1225+
1226+
The last command shown above will serve your static site on the port **5000**. Like many of [serve](https://github.com/zeit/serve)’s internal settings, the port can be adjusted using the `-p` or `--port` flags.
1227+
1228+
Run this command to get a full list of the options available:
12141229

12151230
```sh
1216-
cd build
1217-
python -m SimpleHTTPServer 9000
1231+
serve -h
12181232
```
12191233

1220-
If you’re using [Node](https://nodejs.org/) and [Express](http://expressjs.com/) as a server, it might look like this:
1234+
### Other Solutions
1235+
1236+
You don’t necessarily need a static server in order to run a Create React App project in production. It works just as fine integrated into an existing dynamic one.
1237+
1238+
Here’s a programmatic example using [Node](https://nodejs.org/) and [Express](http://expressjs.com/):
12211239

12221240
```javascript
12231241
const express = require('express');
@@ -1233,7 +1251,9 @@ app.get('/', function (req, res) {
12331251
app.listen(9000);
12341252
```
12351253

1236-
Create React App is not opinionated about your choice of web server. Any static file server will do. The `build` folder with static assets is the only output produced by Create React App.
1254+
The choice of your server software isn’t important either. Since Create React App is completely platform-agnostic, there’s no need to explicitly use Node.
1255+
1256+
The `build` folder with static assets is the only output produced by Create React App.
12371257

12381258
However this is not quite enough if you use client-side routing. Read the next section if you want to support URLs like `/todos/42` in your single-page app.
12391259

0 commit comments

Comments
 (0)