universal-redux-template 0,1,1,1 gulp npm webpack

A clean, extensible react + redux boilerplate with universal/isomorphic rendering, testing and more

3 years after

Build Status

Universal Redux Template

A boilerplate doing universal/isomorphic rendering with Redux + React-router + Express, based on Redux-Realword-Example

Philosophy

To bootstrap a React app development environment is not an easy task, there are so many libraries to setup, including webpack, babel, testing stuff and others. I'd like this boilerplate to be a ready-to-use one with the essential tools and the simplest logic that just work to build a universal rendering React + Redux app. That's why there is no fancy stuff in this app, since it's a basis of your killer app rather than a showcase one.

How to use this template?

  • clone this app and name it as whatever your want: $ git clone https://github.com/mz026/universal-redux-template.git my-killer-app

  • remove the .git folder since you won't need the history of this boilerplate: $ cd my-killer-app; rm -rf .git

  • start out a new git history: $ git init

  • Install dependencies: $ npm install

  • Host dev environment and start to build something chaning the world! $ npm start

  • To run the test with Mocha, Enzyme, Sinon and Chai: $ npm test

  • To generate a container/component/action and its tests: $ ./bin/generate <type> <path>

eg: $ ./bin/generate component myNamespace/MyComponent

Features:

  • Universal rendering, with async data support
  • Server side redirect
  • Separate vendor and app js files
  • Use Immutable as store data

Stack:

  • React 15.3.2
  • React-Router 2.8.1
  • Immutable
  • Express as isomorphic server
  • Babel
  • Webpack

Testing:

Routes Draft:

  • Intro page: {base_url}
  • Question Page: {base_url}/questions

How it works:

Assets Management

When handling static assets, we want to achieve the following goals:

  • Make assigning assets url a no-brainer
  • Apply revision when deploying to production to integrate with CDN services easily

The usage example can be found in [Intro Container](https://github.com/mz026/universal-redux-template/blob/master/app/containers/Intro.js)

We achieve this by:

First, creating an assets container folder: app/assets

In development mode:

Assign static folder linking /assets to the folder above

In production mode:

Use a gulp task (gulp build) to handle it:

  • A set of [rev](https://github.com/smysnk/gulp-rev-all)-ed assets with hash code appended would be built into dist/public/assets
  • A static middleware mapping root url to the folder mentioned above is configured in server.js
  • A set of [revReplace](https://github.com/jamesknelson/gulp-rev-replace)-ed server code would be built into dist/server-build, so that the rev-ed assets can be used when doing server rendering

Redirect after API Calls

Under some cases, we'd like to do 302 redirect after fetching API. For example:

When users try to access a question page via an unexisting Id, redirect her to Index route.

In the code layer, we want the implementation to be shared on both client and server side. This is achieved by passing a history instance to action creators, and use history.push whenever needed.

On the client side, react-router would then take care the rest of redirecting logic, while on server side, we subscribe the url-chaning events on each request, and redirect requests to proper pages if needed.

Such implementation can be found in QuestionContainer, questions action

Vendor Scripts:

Vendor related scripts are bundled into a vendor.js, associated settings can be found in the entry field of webpack.config.js.

(thanks @dlombardi for pointing it out!)

For Windows Users:

npm test:

The single quotes in npm test script surrounding path do not work on Windows, while they're necessary on unix-like machines. Please remove them in scripts.test section in package.json like this:

"test": "NODE_ENV=test NODE_PATH=./app mocha --compilers js:babel-register -r app/spec/support/setup.mocha.js --recursive app/spec/**/*.test.js -w"

(thanks @jbuffin for pointing it out!)

Deployment:

To deploy this app to production environment:

  • Prepare a server with NodeJS environment

  • Use whatever tool to upload this app to server. (Capistrano is a good choice.)

  • Run $ NODE_ENV=production npm install on server

    • After the installation above, postinstall script will run automatically, building front-end related assets and rev-ed server code under dist/ folder.
  • Kick off the server with:

NODE_ENV=production NODE_PATH=./dist/server-build node dist/server-build/server

Deploy to Heroku

To deploy this app to heroku,

  • Set up heroku git remote url
  • Set API_BASE_URL to heroku config var. (without trailing slash)

Here's a sample deployed to heroku: https://redux-template-test.herokuapp.com/ For this case, the API_BASE_URL mention above would be https://redux-template-test.herokuapp.com

Resources:

Related Repositories

awesome-redux

awesome-redux

Awesome list of Redux examples and middlewares ...

electrode-boilerplate-universal-react-node

electrode-boilerplate-universal-react-node

Electrode boilerplate application which has Universal React + Node ...

awesome-redux

awesome-redux

Awesome list of Redux examples and middlewares ...

awesome-redux

awesome-redux

Awesome list of Redux examples and middlewares ...

awesome-redux

awesome-redux

Awesome list of Redux examples and middlewares ...


Top Contributors

mz026 jbuffin marccoll TylorShin joewestcott

Dependencies

package version
assets-webpack-plugin ^3.2.0
babel-core ^6.3.15
babel-loader ^6.2.0
babel-polyfill ^6.3.14
babel-preset-es2015 ^6.3.13
babel-preset-react ^6.3.13
babel-register ^6.5.1
bluebird ^2.10.0
compression ^1.6.0
concurrently ^2.0.0
cross-env ^1.0.7
ejs ^2.3.4
express ^4.13.3
gulp ^3.9.0
gulp-autoprefixer ^3.1.0
gulp-load-plugins ^1.2.0
gulp-rev ^6.0.1
gulp-rev-all ^0.8.24
gulp-rev-replace ^0.4.3
gulp-sass ^2.1.1
gulp-sourcemaps ^1.6.0
history ^1.17.0
humps ^0.6.0
immutable ^3.8.1
lodash ^3.10.1
node-sass ^3.4.2
normalizr ^1.0.0
prop-types ^15.5.10
react ^15.4.2
react-dom ^15.4.2
react-helmet ^5.1.3
react-redux ^5.0.5
react-router ^3.0.5
redux ^3.0.0
redux-logger ^1.0.9
redux-thunk ^0.1.0
rewire ^2.3.4
rimraf ^2.5.2
run-sequence ^1.2.2
superagent ^1.4.0
webpack ^2.2.0
dev babel-eslint ^7.2.1
babel-plugin-react-transform ^2.0.0
babel-plugin-rewire ^1.0.0-beta-5
chai ^3.3.0
chai-as-promised ^5.1.0
chokidar ^1.6.1
dotenv ^4.0.0
enzyme ^2.3.0
eslint ^3.18.0
eslint-plugin-babel ^3.2.0
eslint-plugin-react ^6.0.0
jsdom ^7.0.1
md5-file ^3.1.1
mocha ^2.4.5
nock ^2.17.0
react-addons-test-utils ^15.4.2
react-hot-loader ^1.3.1
rewire-webpack ^1.0.0
sinon ^1.17.1
sinon-chai ^2.8.0
webpack-dev-middleware ^1.10.2
webpack-hot-middleware ^2.18.0

Releases

-   v0.3.0 zip tar
-   v0.2.0 zip tar
-   v0.1.0 zip tar