React SSR in Rails.
This gem is to solve one problem, that's react-rails
will compile all React
components in one file.
This gem will works well with webpackrails
to let you use commonjs feature,
and only evaluate js code as few as possible, and in production environment it
will use precompiled static file instead of compiling the files again and again.
Add this line to your application's Gemfile:
gem 'reactssr-rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install reactssr-rails
The SSR file is a js file whose name ends with ssr.js
under base folder, and
base folder is the folder where you put all your SSR files at, the default base
folder name is components
.
You can config the base folder, put this within your config/application.rb
file,
config.reactssr.assets_base = 'folder_name'
.
react_ssr
is a view helper just like react_component
in react-rails
. Here is
an example how to use it:
<%= react_ssr('IndexView', {props}) %>
So, suppose the current controller_path
is home
, then reactssr-rails will look
up a ssr file in components named home.ssr.js
, just like below:
// home.ssr.js
Components.IndexView = require('./IndexView.jsx');
Put this in your application.js
: //= require react_ssr
that below your Components.
This scripts will remount the component on client side so the event will be working.
According to React document, if remount the component on a markup that has been mounted on the server side, the React will remount it in an efficient way on the client side.
http://github.com/towry/reactssr-rails-example
- In production env, do not run
webpackrails
After checking out the repo, run bin/setup
to install dependencies. Then, run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
to create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
- Fork it ( https://github.com/towry/reactssr-rails/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request