A boilerplate to create Universal React Application.
Client-side source code contains app. An app has an index.js
file which is the root file for the web application, you shouldn’t edit this file unless you are sure what you are doing.
The components
folder holds most of the client-side source code that all React components reside. The root component of the React VDOM tree is App, it is import directly by index.js
. This again should not be changed, you should write your components below this component.
Additionally, the boilerplate has styled-component and react-helmet built-in features to handle CSS styling and head tags respectively. The GlobalStyle and Head components are the two built-in components. The first one is for handling with global CSS styles, e.g. setting fonts, heading styles, html and body styles. The second is for declaring tags to render inside head tag, such as title, meta, link, script, etc.
These CLI commands will help you to do various tasks for you and you don’t have to do anything else.
npm start
-s
option or by using the SSR
option from the configuration file.-h, --help
: output usage information.-l, --long
: Verbose stats.-s, --ssr
: Turn on server-side rendering.npm run lint
dist
named lint-report.html
. You can open it in a browser to view the result.npm run test:client
coverage/client
inside this repository.npm run test:server
coverage/server
inside this repository.npm test
npm run test:client
and npm run test:server
sequentially.npm run test:open:client
npm run test:open:server
npm run test:open
npm run test:open:client
!npm run test:clean:client
coverage/client
directory from this repository.npm run test:clean:server
coverage/server
directory from this repository.npm run test:clean
coverage
directory from this repository.npm run build
dist
folder. This command enables SSR by default but you can choose to turn it off.npm run node
build
.npm run new-component
npm run storybook
npm run setup
npm install
for the first time.Configurations are needed for customizing your project. The file config.js
provide various of configurations to make your project more flexible and manageable.
APP
[string]
Added in: v0.1.0. Removed in: v1.5.0
Specify which app to run if multiple apps are presented.
PORT
[number]
Added in: v1.0.0
The port number on which the Node.js server listens upon startup. This port is used on both development and production mode. Although this config variable gives the server a static port number, we can still change it by setting the PORT
environment variable.
WEBPACK_PORT
[number]
Added in: v1.0.0
The port used by Webpack Dev Server, used only on development mode.
SSR
[boolean]
Added in: v1.3.0
Indicate server-side rendering mode, used only on development mode. The build
command does not care about this option to prevent accidentally turning SSR off. On development mode, SSR would be enabled if either this option is true
or by using the -s
option.
globals
[Object]
Added in: v1.1.0
Contains all global constants which you can use inside the source code. Before v1.1.0, it was replaced by API_URL
which is the global URL for making AJAX requests. Since v1.1.0, API_URL
was moved into globals
to enable declaring more global constants.
babelrc
[Object]
Added in: v1.2.0
Babel configuration for transpiling JS. It is used to replace .babelrc
file.
The dist
directory contains all files for deployment. It includes both client-side and server-side code. It may also contains reports for ESlint and Bundle Analyzer Plugin.
There are some important files and directories in a ready-for-production dist
directory:
index.js
: Node server-side script to run the server.public
: root directory for serving static files when running index.js
.
build
: contains all client-side files built from the App by Webpack.js
: contains common static JavaScript files.templates
: contains EJS template files to render the HTML.Docker is used for containerizing all of the production assets into a single image and then deploying to the server.
To build a Docker image, you first have Docker installed on your machine or your build environment. The installation and documentation can be found here and here. We recommend you to first look at these resources unless you have already used Docker.
The Docker image for our app will include all assets from the dist
directory and package.json
. The package.json
is included to install all non-dev dependencies from NPM, these dependencies are necessary to run our Node.js server.
Here is a list of example CLI command to build and run our app using Docker:
# Build the app
docker build -t <tag-name> .
# Ex: docker build -t URA .
# Run the app
docker run -p <public-port>:<node-port> -d <tag-name>
# Ex: docker run -p 80:3000 -d URA
# Using environment variable
docker run -p <public-port>:<node-port> -d -e PORT=<node-port> <tag-name>
# Ex: docker run -p 443:4000 -d -e PORT=4000 URA
public-port
: port exposed to the host machine.node-port
: port used by our app.