Home

Awesome

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?

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

Features:

Stack:

Testing:

Tools:

Development process:

When developing a feature,

$ yarn run test:watch
$ yarn test -- <the-file-path>

For example:

$ yarn test -- app/test/actions
$ yarn test:ci

Why

The way suggested by babel's doc compiles the code on the fly, which is too slow, especially when the number of files grows:

//package.json

{
  "scripts": {
    "test": "mocha --compilers js:babel-register"
  }
}

So here we pre-compile the code, watch it, and maintain a cache to avoid repeated build of the files that doesn't change. After the pre-compile, the testing cycle can be much faster than before, especially when doing TDD.

For Winners Vimmers:

For vim users, there's a plugin called vim-test binding tests with the editor. You can trigger a test "nearest the cursor", which is super handy when doing TDD.

To make vim-test work together with the pre-compile process,

  1. copy the editor_configs/vimrc to <project-root>/.vimrc
  2. make sure these two lines exist in your ~/.vimrc to enable directory-based .vimrc:
set exrc
set secure

Then you can enjoy the fast feedback loop powered by the greatest editor on the planet.

Routes Draft:

How it works:

Assets Management

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

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:

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:

yarn test:

The single quotes in yarn 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:

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

Deploy to Heroku

To deploy this app to heroku,

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: