npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-static-boilerplate

v0.3.0

Published

A boilerplate for building static sites with React and React Router

Downloads

5

Readme

React Static Boilerplate

React Static Boilerplate

A boilerplate for building static sites with 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 :beers:

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.

Project Goals

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

Dynamic Routes

Iportant 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 :satisfied:

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

  • [x] Add Docker Compose to run a production-like server on demand for manual testing
  • [x] 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

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.