Skip to content

Commit 292f917

Browse files
committed
doc(Readme) document per_request_react_rails_prerenderer
1 parent bbb1ff1 commit 292f917

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ You can render React components inside your Rails server with `prerender: true`:
270270

271271
_(It will also be mounted by the [UJS](#ujs) on page load.)_
272272

273-
Server rendering is powered by [`ExecJS`](https://github.com/rails/execjs) and subject to some requiments:
273+
Server rendering is powered by [`ExecJS`](https://github.com/rails/execjs) and subject to some requirements:
274274

275275
- `react-rails` must load your code. By convention, it uses `server_rendering.js`, which was created
276276
by the install task. This file must include your components _and_ their dependencies (eg, Underscore.js).
@@ -279,6 +279,8 @@ so jQuery and some other libs won't work in this environment :(
279279

280280
`ExecJS` supports many backends. CRuby users will get the best performance from [`mini_racer`](https://github.com/discourse/mini_racer#performance).
281281

282+
#### Configuration
283+
282284
Server renderers are stored in a pool and reused between requests. Threaded Rubies (eg jRuby) may see a benefit to increasing the pool size beyond the default `0`.
283285

284286
These are the default configurations:
@@ -301,6 +303,41 @@ MyApp::Application.configure do
301303
end
302304
```
303305

306+
#### JavaScript State
307+
308+
Some of ExecJS's backends are stateful (eg, mini_racer, therubyracer). This means that any side-effects of a prerender will affect later renders with that renderer.
309+
310+
To manage state, you have a couple options:
311+
312+
- Make a custom renderer with `#before_render` / `#after_render` hooks as [described below](#custom-server-renderer)
313+
- Use `per_request_react_rails_prerenderer` to manage state for a whole controller action.
314+
315+
To check out a renderer for the duration of a controller action, call the `per_request_react_rails_prerenderer` helper in the controller class:
316+
317+
```ruby
318+
class PagesController < ApplicationController
319+
# Use the same React server renderer for the entire request:
320+
per_request_react_rails_prerenderer
321+
end
322+
```
323+
324+
Then, you can access the ExecJS context directly with `react_rails_prerenderer.context`:
325+
326+
```ruby
327+
def show
328+
react_rails_prerenderer # => #<React::ServerRendering::BundleRenderer>
329+
react_rails_prerenderer.context # => #<ExecJS::Context>
330+
331+
# Execute arbitrary JavaScript code
332+
# `self` is the global context
333+
react_rails_prerenderer.context.exec("self.Store.setup()")
334+
render :show
335+
react_rails_prerenderer.context.exec("self.Store.teardown()")
336+
end
337+
```
338+
339+
`react_rails_prerenderer` may also be accessed in before- or after-actions.
340+
304341
#### Custom Server Renderer
305342

306343
`react-rails` depends on a renderer class for rendering components on the server. You can provide a custom renderer class to `config.react.server_renderer`. The class must implement:

0 commit comments

Comments
 (0)