11# universal-react-redux
22
3- A universal starter kit built with ES2015, react, react-router and redux . Server
3+ A simple, clean, React and Redux boilerplate with sensible defaults . Server
44rendering with react and express. Bundled with Webpack, with HMR transforms and
55support for ` css-modules ` .
66
7- ** NOTE: This repository is meant to be continually updated to use the latest in
8- the react ecosystem. It is meant to be more of a guiding template for you to
9- customize and build your own universal apps with React and Redux. I do not
10- promise that future updates will not break your existing application.**
11-
127## Get started
138
149Copy environment variables and edit them if necessary:
10+
1511```
1612cp .env.example .env
1713```
1814
1915Then:
16+
2017```
2118npm install
2219npm start
@@ -30,66 +27,84 @@ For production builds:
3027npm run prod:build
3128npm run serve
3229```
30+
3331Or simply
3432
3533```
3634npm run prod
3735```
3836
3937For Heroku, simply add a ` Procfile ` :
40- ```
41- web: npm run serve
42- ```
4338
44- ## Directory Structure
4539```
46- ├── client # Client-side code
47- ├── common # Shared code between client and server
48- │ ├── css # CSS/Sass Resources
49- │ ├── fonts
50- │ ├── images
51- │ ├── js
52- │ │ ├── actions
53- │ │ ├── components # "Dumb" components
54- │ │ ├── containers # Smart containers
55- │ │ ├── lib # Misc. libraries like helpers, etc.
56- │ │ ├── middleware # Middleware for redux
57- │ │ ├── reducers # Redux reducers
58- │ │ ├── routes # Routes each have an index.js which exports a react-router Route.
59- │ │ └── store # Store configuration for production and dev.
60- │ └── layouts # Layout files to be rendered by the server.
61- ├── server # Server-side code
62- ├── webpack # Webpack configuration files
40+ web: npm run serve
6341```
6442
6543## CSS Modules
44+
6645This project uses [ CSS Modules] ( https://github.com/css-modules/css-modules ) .
6746Class names should be in ` camelCase ` . Place the css file as a sibling to the
6847component with the same name, for example:
48+
6949```
7050├── components
7151│ ├── Header.js
7252│ ├── Header.scss
7353```
7454
55+ ## Environment Variables
56+
57+ In development mode, environment variables are loaded by ` dotenv ` off the ` .env `
58+ file in your root directory. In production, you'll have to manage these
59+ yourself. In Heroku, this is simple as running:
60+
61+ ```
62+ heroku config:set FOO=bar
63+ ```
64+
7565## Redux Devtools
66+
7667This project supports the awesome [ Redux Devtools Extension] ( https://github.com/zalmoxisus/redux-devtools-extension ) . Install the
7768Chrome or Firefox extension and it should just work.
7869
79- ## Server Side Rendering (SSR) and Asynchronous Data Fetching
70+ ## Pre-fetching Data for Server Side Rendering (SSR)
71+
8072When rendering components on the server, you'll find that you may need to fetch
8173some data before it can be rendered. The [ server code] ( server/server.js ) looks
8274for a ` fetchData ` method on the container component and its child components,
8375then executes all of them and only renders after the promises have all been
8476resolved.
8577
86- See the [ TodosContainer] ( common/js/containers/Todos/index.js ) for an example.
78+ ```
79+ // As an ES6 class
80+
81+ class TodosContainer extends React.Component {
82+ static fetchData = ({ store }) => {
83+ return store.dispatch(fetchTodos());
84+ };
85+ }
86+
87+ // As a functional stateless component
88+
89+ const TodosContainer = (props) => {
90+ const { todos } = props;
91+ return (
92+ // ...component code
93+ );
94+ }
95+
96+ TodosContainer.fetchData = ({ store }) => {
97+ return store.dispatch(fetchTodos());
98+ }
99+ ```
87100
88101## Async / Await
102+
89103This project uses ` async/await ` , available by default in Node.js v8.x.x or
90104higher. If you experience errors, please upgrade your version of Node.js.
91105
92- ## Writing Tests
106+ ## Testing
107+
93108The default testing framework is Mocha, though you can use whatever you want.
94109Make sure you have it installed:
95110
@@ -109,7 +124,9 @@ Tests should reside in `test/spec` in their appropriate folders:
109124Tests can be written with ES2015, since it passes through ` babel-register ` .
110125
111126## Running Tests
127+
112128To run a single test:
129+
113130```
114131npm test /path/to/single.test.js
115132```
@@ -126,7 +143,7 @@ To run all tests:
126143npm run test:all
127144```
128145
129- This will run all files that are suffixed with a ` . test.js ` .
146+ This will run all tests in the ` test/spec ` directory .
130147
131148## Running ESLint
132149
@@ -136,16 +153,15 @@ npm run lint
136153
137154Check the ` .eslintignore ` file for directories excluded from linting.
138155
139- ## Changing the Asset Host
156+ ## Changing the public asset path
140157
141- In production scenarios, you may want your assets to be hosted elsewhere besides
142- on your server. Just set an environment variable to point the asset host to
143- where ever you want, as defaults to ` localhost:3001 ` . Just set it to the CDN of
144- your choice .
158+ By default, assets are built into ` /dist/public ` . This path is then served by
159+ express under the path ` /assets ` . This is the public asset path. In a production
160+ scenario, you may want your assets to be hosted on a CDN. To do so, just change
161+ the ` PUBLIC_ASSET_PATH ` environment variant .
145162
146163If you're using Heroku:
164+
147165```
148- heroku config:set ASSET_HOST=/dist/
149- # OR
150- heroku config:set ASSET_HOST=https://s3.amazonaws.com/mybucket/myasssets/
166+ heroku config:set PUBLIC_ASSET_PATH=https://my.cdn.com
151167```
0 commit comments