• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    JavaScript
  • Created almost 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A boilerplate for building static sites with Webpack 2, React and React Router

⚠️ UNMAINTAINED ⚠️ This project is no longer maintained because I've found Gatsby does everything I originally wanted and more. So feel free to use/fork this project, but it is no longer under active development.


React Static Boilerplate

React Static Boilerplate

A boilerplate for building static sites with Webpack, React and React Router.

Quick Start:

  • git clone https://github.com/iansinnott/react-static-boilerplate my-static-site
  • cd my-static-site && rm -rf .git
  • npm install
  • npm start to run a dev server
  • Write an awesome client-side app...
  • npm run build to minify, package and generate static HTML files from your routes

Now you're all set to deploy to your favorite hosting solution 🍻

To check your production build use pushstate-server npm install pushstate-server -g pushstate-server build

NOTE: It's important to run npm run build and not npm build. The latter will silently exit because it is a native NPM command.

Changes Jan 2017

Webpack 2 was just released. In celebration, here are some fun new features:

  • Dev tooling is a dependency! It is no longer part of this repo. This means future upgrades will be a breeze.
  • Support for Webpack 2
  • Fully mocked client side environment, so code that requires the DOM/BOM should still compile
  • Support for webpack-dashbaord + improved dev server logging (npm run start:dashboard)
  • Asset fingerprinting to make long-term caching a breeze (Adds hash to output files. Ex app.72266bec.js)
  • Analyze the size of your bundle webpack-bundle-analyzer plugin (npm run build:analyze. See screenshot below of output below)

Usage Commands

npm run start: Start a dev server

npm run start:dashboard: Start a dev server with Webpack Dashboard

npm run build: Compile, minify and fingerprint everything and create static files from routes

npm run build:analyze: Same as above, but will also output webpack-bundle-analyzer-stats.json and webpack-bundle-analyzer-report.html for you to analyze. Open the HTML file in a web browser to analyze the bundle. Or from the CLI:

open build/webpack-bundle-analyzer-report.html

npm run eject: This project uses another project of mine, app-time, to run builds. That means all the nasty webpack config stuff is hidden away. But sometimes you really just need to customize the hell out of the build process so if you need to you can run this command to remove the app-time dependency and bring all the config files into your own project. This cannot be undone once you've made changes to the config, but if you do change your mind before modifying any code you can always revert:

git checkout .
rm -rf apptime

webpack-bundle-analyzer

Project Goals

  • A single source of truth: Your routes
  • Intuitive. Leverage existing front-end knowledge
  • Awesome developer experience
  • Flexible. No file structure or naming conventions required. Use whatever modules you want

Dynamic Routes

Important Note: This boilerplate does not yet support generating static sites for dynamic routes such as post/:id. That's the next major feature addition (see the Roadmap below) but it hasn't been implemented yet.

For more info see this issue on the react-static-webpack-plugin repo.

Opinionated styling

This boilerplate is slightly opinionated, especially when it comes to CSS. Don't worry, you can easily change any of this but by default I use Stylus and CSS Modules for compiling CSS.

Stylus

There is certainly no need to use Stylus for styling your static pages. Use whatever you like best. But Stylus does support standard CSS syntax so if you don't want to set up anything new just start writing CSS in any of the *.styl files and watch it compile as expected 😆

This boilerplate uses Stylus for page styling by default. It's pretty simple to swap out stylus for Sass, Less or even plain CSS. Just update the Webpack config files (both webpack.config.dev.js and webpack.config.prod.js) to use your loader of choice. If you're not sure how to do this check out the Webpack loaders documentation.

CSS Modules

CSS Modules are provided as part of the css-loader itself. For more info check out the CSS Loader docs. If you haven't heard of CSS Modules check out GitHub repo. Basically it lets you write styles local to any component or file. In my experience it makes styling much more pleasant and much less error prone.

Of course if you don't want to use this feature you don't have to.

To disable CSS modules you just need to replace to Webpack config lines for dev and prod:

// webpack.config.dev.js

// Replace this...
'css?modules&importLoaders=2&localIdentName=[name]__[local]__[hash:base64:6]',

// ...with this.
'css',
// webpack.config.prod.js

// Replace this...
loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=2!autoprefixer!stylus'),

// ...with this.
loader: ExtractTextPlugin.extract('style', 'css!autoprefixer!stylus'),

Serving static files

This project will generate all the static files you need to server a full, production-ready website. However your server will likely need some configuration so that it maps the same URLs React Router uses to actual static files. For example, the "about" page might have the following URL according to React Router: /about.

However, a static server without any configuration would see the URL /about and return a 404 because it expects a URL of /about.html to actually server the file. There are many ways to handle this on different systems. Here is how you might handle it with Nginx.

Nginx

This project contains a script to help generate a usable Nginx config. To run it:

npm run -s conf

This will output the config to stdout. Redirect it as you see fit to save it to disk:

npm run -s conf > my-site.conf

Once you've saved the file you can sym link it into place and reload Nginx. This is just an example, your path to Nginx will vary based on system:

ln -s $PWD/my-site.conf /path/to/nginx/conf-files/static-fun.conf
nginx -s reload

Testing the static site with Docker

First, make sure you have Docker installed and running on your system. Then proceed.

This repository includes a Docker Compose config file for quickly setting up an Nginx server to test your static site. To run it, simply run:

npm run -s conf > nginx.conf
docker-compose up

Now run docker ps to get the IP address and port where the container is running. If you're using Docker Machine this will likely be something like 192.168.99.100:8080.

Testing the static site with Nginx on a Mac

If you don't have Nginx yet install it with brew:

brew install nginx

Now generate an Nginx config file using the script in this project:

npm run -s conf > nginx.conf

Then link that file we just created in to the global Nginx config file and reload Nginx:

mkdir -p /usr/local/etc/nginx/servers
ln -s $PWD/nginx.conf /usr/local/etc/nginx/servers/static-fun.conf
nginx -s reload

Technology Used

For further reading on the primary tech used in this boilerplate see the links below:

Roadmap

  • Add Docker Compose to run a production-like server on demand for manual testing
  • Add readme docs for testing a prod site with Docker and Docker compose
  • Support for dynamic content
  • Leverage code splitting for efficient bundling and async module loading
  • Improved SVG tooling

Redux

Redux is supported. Just be sure to tell the plugin where to find your store so that it can be passed through a <Provider> during the static rendering. You can configure this in apptime.config.prod.js using the ReactStaticPlugin configuration function:

// apptime.config.prod.js
module.exports = (config, apptime) => ({
  ...config, // Base config

  ...apptime.ReactStaticPlugin({
    routes: './client/routes.js',
    reduxStore: './client/redux/store.js', // Add redux store
    template: './template.js',
  }),
});
// ./client/store.js
import { createStore } from 'redux';

import someReducer from '../reducers';

const store = createStore(someReducer);

export default store;

👉 Go here for a full working Redux example.

Troubleshooting

Babel Env

Make sure BABEL_ENV is not set to development when building. If it is babel will likely throw a hot module replacement error since HMR transformations are getting run on everything that goes through babel.

Font Awesome Webpack

The font-awesome-webpack module does not seem to work with the approach of generating files as UMD modules then requiring them from the public dir. It throws an error about window not being defined.

More Repositories

1

react-string-replace

A simple way to safely do string replacement with React components
JavaScript
605
star
2

alfred-maestro

An Alfred workflow to execute Keyboard Maestro macros.
Go
409
star
3

jstz

🌐Timezone detection for JavaScript
JavaScript
172
star
4

react-static-webpack-plugin

Build full static sites using React, React Router and Webpack (Webpack 2 supported)
JavaScript
157
star
5

prompta

ChatGPT UI that is keyboard-centric, mobile friendly, can syncs chat history across devices and search past conversations.
Svelte
30
star
6

asciilib

(ノ◕ヮ◕)ノ*:・゚✧ A library of ascii faces and kaomoji
JavaScript
18
star
7

rxjs-dash-docset

RxJS 5 documentation for Dash
JavaScript
15
star
8

browser-gopher

Search, aggregate, backup your browsing history from the command line.
Go
14
star
9

notion-utils

TypeScript
9
star
10

asciilib-workflow

Quickly search through ascii faces and kaomoji (ノ◕ヮ◕)ノ*:・゚✧
JavaScript
9
star
11

zazu-emoji

⚡ Fast, offline emoji search for Zazu
JavaScript
8
star
12

jekyll-post

A tool for managing Jekyll from the command line
JavaScript
8
star
13

react-static-presentation

The Slide deck and examples for my React Static talk
JavaScript
8
star
14

darkly-darker-theme

A dark Chrome theme
JavaScript
7
star
15

one-dark-tab

Like OneTab, but darker.
JavaScript
7
star
16

nightmare-ava-example

JavaScript
6
star
17

webpack-base-project

A minimal Webpack project to teach you how to set up a new project
JavaScript
6
star
18

things-2do-importer

Import 2Do tasks into Things 3
Python
6
star
19

mailstring

Generate mailto strings for fun and profit. Also a React component 📤
JavaScript
5
star
20

zazu-dark-theme

🕶 A simple, dark theme for Zazu
CSS
5
star
21

shirt

👕 Put a shirt on that data! Simple algebraic data types
JavaScript
4
star
22

iansinnott.github.io

The blog of Ian SInnott
HTML
4
star
23

app-time

🌟 Build complete, wonderful, beautiful apps with one dependency. Compile to static HTML files with a single command.
JavaScript
4
star
24

google-sheets-backend

JavaScript
3
star
25

express-middleware-lecture

Source code and writeup for my lecture on Express middleware
JavaScript
3
star
26

stylite

🎨 A super lightweight style editor. Apply any styles you want to any site.
JavaScript
3
star
27

chinese-common-wordlist-pleco-decks

JavaScript
2
star
28

real-time-stack

JavaScript
2
star
29

imessage-backup-helpers

JavaScript
2
star
30

react-boilerplate

React + Webpack + Hot Reloading 🎉
JavaScript
2
star
31

bitbucket-cli

A CLI for BitBucket
JavaScript
2
star
32

asciilib-site

The website for asciilib
TypeScript
2
star
33

slush-express-isinn

Generate Express apps with Slush.
CSS
1
star
34

markdown-to-csv

JavaScript
1
star
35

emoji-annotations

JavaScript
1
star
36

character-frequency-workflow

JavaScript
1
star
37

gatsby-notion

TypeScript
1
star
38

url-spider

TypeScript
1
star
39

jquery-ui-dropdown

A simple dropdown widget for jQuery UI
JavaScript
1
star
40

trimstring

🔪 Neatly trim template strings
JavaScript
1
star
41

mini-redux

A simple imitation of Redux implemented in a single React component
JavaScript
1
star
42

sqlite-syncta

Experimenting with syncing sqlite databases
Go
1
star
43

eslint-config-zen

An ESLint config for use with the latest ESNext features, React and Flow
JavaScript
1
star
44

electron-auto-update

TypeScript
1
star
45

addr.fyi

JavaScript
1
star
46

how-the-web-works

An over simplified explanation of how the web works
JavaScript
1
star
47

history-to-finda

Clojure
1
star
48

notedown

A note-taking app...
JavaScript
1
star
49

record-video

An example of recording on mobile with the front-facing camera.
TypeScript
1
star
50

baby-lisp-interpreter

Clojure
1
star
51

try-instantdb

Trying out InstantDB
TypeScript
1
star
52

next-tailwind-typescript-starter

TypeScript
1
star
53

authguardian-react-starter

JavaScript
1
star
54

lab.iansinnott.com

🔬 Where I run experiments and learn by doing
JavaScript
1
star