Skip to content

Latest commit

 

History

History
303 lines (216 loc) · 15.7 KB

File metadata and controls

303 lines (216 loc) · 15.7 KB

Code Climate Coverage Status

React, Redux, Tailwind CSS, ES7, Webpack, Ruby on Rails Demo

Control Plane Deployment Example

Control Plane offers a viable, cost-saving alternative to Heroku, especially when using the cpflow gem to deploy to Control Plane.

ShakaCode recently migrated HiChee.com to Control Plane, resulting in a two-thirds reduction in server hosting costs!

See doc in ./.controlplane/readme.md for how to easily deploy this app to Control Plane.

The instructions leverage the cpflow CLI, with source code and many more tips on how to migrate from Heroku to Control Plane in https://github.com/shakacode/heroku-to-control-plane.


React on Rails Pro and ShakaCode Pro Support

React on Rails Pro provides Node server rendering and other performance enhancements for React on Rails.

2018-09-11_10-31-11

For more information, see the React on Rails Pro Docs.

  • Optimizing your front end setup with Webpack v5+ and Shakapacker for React on Rails including code splitting with loadable-components.
  • Upgrading your app to use the current Webpack setup that skips the Sprockets asset pipeline.
  • Better performance client and server side.

ShakaCode can also help you with your custom software development needs. We specialize in marketplace and e-commerce applications that utilize both Rails and React. We can even leverage our code for HiChee.com for your app!

See the ShakaCode Client Engagement Model article to learn how we can work together.


Community


Testimonials

From Joel Hooks, Co-Founder, Chief Nerd at egghead.io, January 30, 2017:

2017-01-30_11-33-59

For more testimonials, see Live Projects and Kudos.


Videos

  1. History and Motivation
  2. Basic Tutorial Walkthrough
  3. Code Walkthrough

NEWS

You can see this tutorial live here: http://reactrails.com/

Table of Contents

Demoed Functionality

  • Example of using the react_on_rails gem for easy react/webpack integration with Rails.
  • Example of React with CSS Modules inside of Rails using Webpack as described in Smarter CSS builds with Webpack.
  • Example of enabling hot reloading of both JS and CSS (modules) from your Rails app in development mode. Change your code. Save. Browser updates without a refresh!
  • Example of React/Redux with Rails Action Cable.
  • Example of Rails 7 with ReactJs/Redux/React-Router with Webpack and ES7.
  • Enabling development of a JS client independently from Rails using the Webpack Dev Server. You can see this by starting the app and visiting http://localhost:4000
  • Enabling the use of npm modules and Babel with a Rails application using Webpack.
  • Easily enable retrofitting such a JS framework into an existing Rails app. You don't need a brand new single page app!
  • Example setting up Ruby and JavaScript linting in a real project, with corresponding CI rake tasks.
  • Enabling the i18n functionality with react-intl.

Technologies involved

See package.json and Gemfile for versions

  1. react_on_rails gem
  2. React
  3. Redux
  4. react-router
  5. react-router-redux
  6. Webpack with hot-reload (for local dev)
  7. Babel transpiler
  8. Ruby on Rails 7 for backend app and comparison with plain HTML
  9. Thruster - Zero-config HTTP/2 proxy for optimized asset delivery
  10. Heroku for Rails 7 deployment
  11. Deployment to the ControlPlane
  12. Turbolinks 5
  13. Tailwind CSS

Basic Demo Setup

Prerequisites

  • Node v22.3.0 or above. Be sure that you have Node installed! We suggest using nvm and running nvm list to check the active Node version. See this article Updating and using nvm.
  • Ruby 3.3.3 or above
  • Postgres v9.2 or above
  • Redis. Check that you have Redis installed by running which redis-server. If missing and on MacOS, install with Homebrew (brew install redis)
  • Yarn.

Setup

  1. git clone git@github.com:shakacode/react-webpack-rails-tutorial.git
  2. cd react-webpack-rails-tutorial
  3. bundle install
  4. yarn
  5. rake db:setup
  6. rails start

Basic Command Line

  • Run all linters and tests: rake
  • See all npm commands: yarn run
  • To start all development processes: foreman start -f Procfile.dev
  • To start only all Rails development processes: foreman start -f Procfile.hot

Rails Integration

We're now using Webpack for all Sass and JavaScript assets so we can do CSS Modules within Rails!

  • Production Deployment: heroku-deployment.md.
    • Configure Buildpacks
      heroku buildpacks:set heroku/ruby --app your-app
      heroku buildpacks:add --index 1 heroku/nodejs --app your-app
      heroku buildpacks:set --index 3 https://github.com/sreid/heroku-buildpack-sourceversion.git --app your-app
      

Testing

  • See Yak Shaving Failing Integration Tests with React and Rails

  • Be sure to see Integration Test Notes for advice on running your integration tests.

  • Testing Mode: When running tests, it is useful to run foreman start -f Procfile.spec in order to have webpack automatically recompile the static bundles. Rspec is configured to automatically check whether or not this process is running. If it is not, it will automatically rebuild the webpack bundle to ensure you are not running tests on stale client code. This is achieved via the ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config) line in the rails_helper.rb file. If you are using this project as an example and are not using RSpec, you may want to implement similar logic in your own project.

Webpack and Rspack

Converted to use Shakapacker with support for both Webpack and Rspack bundlers.

This project supports both Webpack and Rspack as JavaScript bundlers via Shakapacker. Switch between them by changing the assets_bundler setting in config/shakapacker.yml:

# Use Rspack (default - faster builds)
assets_bundler: rspack

# Or use Webpack (classic, stable)
assets_bundler: webpack

Performance Comparison

Measured bundler compile times for this project (client + server bundles):

Build Type Webpack Rspack Improvement
Development ~3.1s ~1.0s ~3x faster
Production (cold) ~22s ~10.7s ~2x faster

Benefits of Rspack:

  • 67% faster development builds (saves ~2.1s per incremental build)
  • 51% faster production builds (saves ~11s on cold builds)
  • Faster incremental rebuilds during development
  • Reduced CI build times
  • Drop-in replacement - same configuration files work for both bundlers

Note: These are actual bundler compile times. Total build times including package manager overhead may vary.

Configuration Files

All bundler configuration is in config/webpack/:

  • webpack.config.js - Main entry point (auto-detects Webpack or Rspack)
  • commonWebpackConfig.js - Shared configuration
  • clientWebpackConfig.js - Client bundle settings
  • serverWebpackConfig.js - Server-side rendering bundle
  • development.js, production.js, test.js - Environment-specific settings

Additional Resources

Thruster HTTP/2 Proxy

This project uses Thruster, a zero-config HTTP/2 proxy from Basecamp, for optimized asset delivery and improved performance.

What Thruster Provides

  • HTTP/2 Support: Automatic HTTP/2 with multiplexing for faster parallel asset loading
  • Asset Caching: Intelligent caching of static assets from the public/ directory
  • Compression: Automatic gzip/Brotli compression for reduced bandwidth usage
  • Simplified Configuration: No need for manual early hints configuration
  • Production Ready: Built-in TLS termination with Let's Encrypt support

Benefits

Compared to running Puma directly with --early-hints:

  • 20-30% faster initial page loads due to HTTP/2 multiplexing
  • 40-60% reduction in transfer size with Brotli compression
  • Simpler setup - zero configuration required
  • Better caching - automatic static asset optimization

Usage

Thruster is already integrated into all Procfiles:

# Development with HMR
foreman start -f Procfile.dev

# Production
web: bundle exec thrust bin/rails server

The server automatically benefits from HTTP/2, caching, and compression without any additional configuration.

For detailed information, troubleshooting, and advanced configuration options, see docs/thruster.md.

Sass, CSS Modules, and Tailwind CSS Integration

This example project uses mainly Tailwind CSS for styling. Besides this, it also demonstrates Sass and CSS modules, particularly for some CSS transitions.

We're using Webpack to handle Sass assets so that we can use CSS modules. The best way to understand how we're handling assets is to close follow this example. We'll be working on more docs soon. If you'd like to give us a hand, that's a great way to learn about this!

For example in client/app/bundles/comments/components/CommentBox/CommentBox.jsx, see how we use standard JavaScript import syntax to refer to class names that come from CSS modules:

import css from './CommentBox.module.scss';

export default class CommentBox extends React.Component {
  render() {
    const { actions, data } = this.props;
    const cssTransitionGroupClassNames = {
      enter: css.elementEnter,
      enterActive: css.elementEnterActive,
      exit: css.elementLeave,
      exitActive: css.elementLeaveActive,
    };
  }
}

Fonts with SASS

The tutorial makes use of a custom font OpenSans-Light. We're doing this to show how to add assets for the CSS processing. The font files are located under client/app/assets/fonts and are loaded by both the Rails asset pipeline and the Webpack HMR server.

Process management during development

bundle exec foreman start -f <Procfile>
  1. Procfile.dev: Starts the Webpack Dev Server and Rails with Hot Reloading.
  2. Procfile.static: Starts the Rails server and generates static assets that are used for tests.

Contributors

The Shaka Code team!, led by Justin Gordon, along with with many others. See contributors.md

RubyMine and WebStorm

Special thanks to JetBrains for their great tools: RubyMine and WebStorm. Some developers of this project use RubyMine at the top level, mostly for Ruby work, and we use WebStorm opened up to the client directory to focus on JSX and Sass files.

Hiring

We're looking for great developers that want to work with Rails + React (and react-native!) with a remote-first, distributed, worldwide team, for our own products, client work, and open source. More info here.


Thank you from Justin Gordon and ShakaCode

Thank you for considering using React on Rails.

Aloha and best wishes from the ShakaCode team!