React.js Boilerplate
Quick setup for new performance orientated, offline–first React.js applications featuring Redux, hot–reloading, PostCSS, react-router, ServiceWorker, AppCache, FontFaceObserver and Mocha.
Features
-
Using react-hot-loader, your changes in the CSS and JS get reflected in the app instantly without refreshing the page. That means that the current application state persists even when you change something in the underlying code! For a very good explanation and demo watch Dan Abramov himself talking about it at react-europe.
-
Redux is a much better implementation of a flux–like, unidirectional data flow. Redux makes actions composable, reduces the boilerplate code and makes hot–reloading possible in the first place. For a good overview of redux check out the talk linked above or the official documentation!
-
PostCSS is like Sass, but modular and capable of much more. PostCSS is, in essence, just a wrapper for plugins which exposes an easy to use, but very powerful API. While it is possible to replicate Sass features with PostCSS, PostCSS has an ecosystem of amazing plugins with funcionalities Sass cannot even dream about having.
-
Unit tests should be an important part of every web application developers toolchain. Mocha checks your application is working exactly how it should without you lifting a single finger. Congratulations, you just won a First Class ticket to world domaination, fasten your seat belt please!
-
react-router is used for routing in this boilerplate. Using the new, and currently unreleased,
1.0version, react-router makes routing really easy to do and takes care of a lot of the work. Since the version is not officially out yet, the documentation is not fully finished, but by far finished enough to work for most needs. -
ServiceWorker and AppCache make it possible to use your application offline. As soon as the website has been opened once, it is cached and available without a network connection.
manifest.jsonis specifically for Chrome on Android. Users can add the website to the homescreen and use it like a native app!
Getting started
-
Clone this repo using
git clone git@github.com:mxstbr/react-boilerplate. -
Delete the existing git repository by running
rm -rf .git. -
Initialize a new git repository with
git init,git add .andgit commit -m "Initial commit". -
Run
npm installto install the dependencies. -
Run
npm startto start the local web server. -
Go to
http://localhost:3000and you should see the app running!
Building & Deploying
-
Run
npm run build, which will compile all the necessary files in abuildfolder. -
Upload the contents of the
buildfolder to your web server. -
?????? (You figure this part out)
-
Profit!
Server Configuration
Apache
This boilerplate includes a .htaccess file that does two things:
-
Redirect all traffic to HTTPS because ServiceWorker only works for encrypted traffic
-
Rewrite all pages (e.g. yourdomain.com/subpage) to the
index.htmlto letreact-routertake care of presenting the correct page
CSS
The CSS modules found in the css subfolders all get imported into the main.css file, which get inlined and minified into the compiled.css file. To add/change the styling, either write the CSS into the appropriate module or make a new one and @import it in the main.css file at the appropriate place.
PostCSS Plugins
The boilerplate uses PostCSS, and includes a few plugins by default:
-
postcss-import: Inlines@imported stylesheets to create one big stylesheet. -
postcss-simple-vars: Makes it possible to use `$variables in your CSS. -
postcss-focus: Adds a:focusselector to every:hover. -
autoprefixer-core: Prefixes your CSS automatically, supporting the last two versions of all major browsers and IE 8 and up. -
cssnano: Optimizes your CSS file. For a full list of optimizations check the offical website. -
postcss-reporter: Makes warnings by the above plugins visible in the console.
For a full, searchable catalog of plugins go to postcss.parts.
Folder Structure
The boilerplate comes with a basic folder structure to keep the CSS files organised. This is what the folders are for:
-
base: Global styling, e.g. setting the box–model for all elements -
components: Component specific styling, e.g. buttons, modals,... -
layout: Global layouts, e.g. article, homepage,... -
utils: Utility files, e.g. variables, mixins, functions,... -
vendor: External files, e.g. a CSS reset
JS
All files that are imported/required somewhere get compiled into one big file at build time. (build/bundle.js) Webpack automatically optimizes your JavaScript with UglifyJS, so you do not have to worry about that.
Folder Structure
The folder structure of the JS files reflects how Redux works, so if you are not familiar with Redux check out the official documentation.
-
actions: Actions get dispatched with this/these utility module(s) -
components: The main JS folder. All your React components should be in this folder, for big projects they might be grouped into seperate subfolders. E.g. a navigation componentNav.react.js -
components/pages: Actual pages (routes) users can visit. -
constants: Action constants need to be defined in this/these utility module(s) -
reducers: Reducers manage the state of an app, basically a simplified implementation of Stores in Flux. For an introduction to reducers, watch this talk by @gaearon.
Testing
Unit tests live in the test directory, and are run with npm test. There are two files in there already, testing that the current AppActions and Reducers are working correctly. Should you be stuck and have no idea what is going on check out the official Mocha documentation!
Opinionated features
Web Fonts
If you simply use web fonts in your project, the page will stay blank until these fonts are downloaded. That means a lot of waiting time in which users could already read the content.
FontFaceObserver adds a js-<font-name>-loaded class to the body when the fonts have loaded. You should specify an initial font-family with save fonts, and a .js-<font-name>-loaded font-family which includes your web font.
Adding a new font
-
Add the
@font-facedeclaration tobase/_fonts.css. -
In
base/_base.css, specify your initialfont-familyin thebodytag with only save fonts. In thebody.js-<font-name>-loadedtag, specify yourfont-familystack with your web font. -
In
js/app.jsadd a<font-name>Observerfor your font.
Offline access
Using a ServiceWorker and the App Cache, your application is cached for offline usage. TO cache a file, add it to cache variable of the AppCachePlugin in webpack.build.config.js and to the urlsToCache variable in the serviceworker.js file.
Add To Homescreen
On Chrome for Android (soon hopefully more browsers), users can add a webpage to the homescreen. Combined with offline caching, this means your web app can be used exactly like a native application.
The name and icon to be displayed are set in the manifest.json file. Change them to your project name and icon, and try it!
Gotchas
These are some things to be aware of when using this boilerplate.
Images in the HTML file(s)
Adding images to the HTML is a bit of a pain right now as webpack only goes through the JavaScript file. Add the image to your HTML file how you always would:
<!-- Normal Image -->
<img src="img/yourimg.png" />
<!-- Meta tags -->
<meta property="og:image" content="img/yourimg.png" />
<!-- ... -->If you simply do this, webpack will not transfer the images to the build folder. To get webpack to transfer them, you have to import them with the file loader in your JavaScript somewhere, e.g.:
import 'file?name=[name].[ext]!../img/yourimg.png';Then webpack will correctly transfer the image to the build folder.
License
This project is licensed under the MIT license, Copyright (c) 2015 Maximilian Stoiber. For more information see LICENSE.md.