This project repo is no longer being maintained. For more information on code-gov repos, go to GSA/code-gov.
This project was bootstrapped with Create React App.
Below you will find some information on how to perform common tasks.
You can find the most recent version of this guide here.
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:
cd build
python -m SimpleHTTPServer 9000
If you're using Node and Express as a server, it might look like this:
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static('./build'));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, './build', 'index.html'));
});
app.listen(9000);
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.
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.
If you use routers that use the HTML5 pushState
history API under the hood (for example, React Router with browserHistory
), many static file servers will fail. For example, if you used React Router with a route for /todos/42
, the development server will respond to localhost:3000/todos/42
properly, but an Express serving a production build as above will not.
This is because when there is a fresh page load for a /todos/42
, the server looks for the file build/todos/42
and does not find it. The server needs to be configured to respond to a request to /todos/42
by serving index.html
. For example, we can amend our Express example above to serve index.html
for any unknown paths:
app.use(express.static('./build'));
-app.get('/', function (req, res) {
+app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, './build', 'index.html'));
});
Now requests to /todos/42
will be handled correctly both in development and in production.
By default, Create React App produces a build assuming your app is hosted at the server root.
To override this, specify the homepage
in your package.json
, for example:
"homepage": "http://mywebsite.com/relativepath",
This will let Create React App correctly infer the root path to use in the generated HTML file.
Note: this feature is available with
[email protected]
and higher.
The step below is important!
If you skip it, your app will not deploy correctly.
Open your package.json
and add a homepage
field:
"homepage": "https://myusername.github.io/my-app",
Create React App uses the homepage
field to determine the root URL in the built HTML file.
Now, whenever you run npm run build
, you will see a cheat sheet with instructions on how to deploy to GitHub Pages.
To publish it at https://myusername.github.io/my-app, run:
npm install --save-dev gh-pages
Add the following script in your package.json
:
// ...
"scripts": {
// ...
"deploy": "npm run build&&gh-pages -d build"
}
(Note: the lack of whitespace is intentional.)
Then run:
npm run deploy
Finally, make sure GitHub Pages option in your GitHub project settings is set to use the gh-pages
branch:
You can configure a custom domain with GitHub Pages by adding a CNAME
file to the public/
folder.
GitHub Pages doesn't support routers that use the HTML5 pushState
history API under the hood (for example, React Router using browserHistory
). This is because when there is a fresh page load for a url like http://user.github.io/todomvc/todos/42
, where /todos/42
is a frontend route, the GitHub Pages server returns 404 because it knows nothing of /todos/42
. If you want to add a router to a project hosted on GitHub Pages, here are a couple of solutions:
- You could switch from using HTML5 history API to routing with hashes. If you use React Router, you can switch to
hashHistory
for this effect, but the URL will be longer and more verbose (for example,http://user.github.io/todomvc/#/todos/42?_k=yknaj
). Read more about different history implementations in React Router. - Alternatively, you can use a trick to teach GitHub Pages to handle 404 by redirecting to your
index.html
page with a special redirect parameter. You would need to add a404.html
file with the redirection code to thebuild
folder before deploying your project, and youโll need to add code handling the redirect parameter toindex.html
. You can find a detailed explanation of this technique in this guide.
Use the Heroku Buildpack for Create React App.
You can find instructions in Deploying React with Zero Configuration.
See this blog post on how to deploy your React app to Amazon Web Services S3 and CloudFront.
It is reported that npm run build
can fail on machines with no swap space, which is common in cloud environments. If the symptoms are matching, consider adding some swap space to the machine youโre building on, or build the project locally.