You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+38-1Lines changed: 38 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -270,7 +270,7 @@ You can render React components inside your Rails server with `prerender: true`:
270
270
271
271
_(It will also be mounted by the [UJS](#ujs) on page load.)_
272
272
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:
274
274
275
275
-`react-rails` must load your code. By convention, it uses `server_rendering.js`, which was created
276
276
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 :(
279
279
280
280
`ExecJS` supports many backends. CRuby users will get the best performance from [`mini_racer`](https://github.com/discourse/mini_racer#performance).
281
281
282
+
#### Configuration
283
+
282
284
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`.
283
285
284
286
These are the default configurations:
@@ -301,6 +303,41 @@ MyApp::Application.configure do
301
303
end
302
304
```
303
305
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
+
classPagesController < 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`:
`react_rails_prerenderer` may also be accessed in before- or after-actions.
340
+
304
341
#### Custom Server Renderer
305
342
306
343
`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