Skip to content

Commit 0c6ce10

Browse files
authored
Merge pull request #680 from reactjs/controller-preprender-false
Controller preprender: false
2 parents 96cedde + 0b9c0ec commit 0c6ce10

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ end
224224

225225
This custom renderer behaves the same as a normal view renderer and accepts the usual arguments - `content_type`, `layout`, `location` and `status`.
226226
By default, your current layout will be used and the component, rather than a view, will be rendered in place of `yield`. Custom data-* attributes
227-
can be passed like `data: {remote: true}`.
227+
can be passed like `data: {remote: true}`. Prerendering is set to `true` by default, but can be turned off like any other option: `prerender: false`.
228228

229229
### Component generator
230230

lib/react/rails/controller_renderer.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module Rails
33
# A renderer class suitable for `ActionController::Renderers`.
44
# It is associated to `:component` in the Railtie.
55
#
6-
# It is prerendered with {React::ServerRendering}.
6+
# It is prerendered by default with {React::ServerRendering}.
7+
# Set options[:prerender] to `false` to disable prerendering.
78
#
89
# @example Rendering a component from a controller
910
# class TodosController < ApplicationController
@@ -24,12 +25,18 @@ def initialize(options={})
2425
@__react_component_helper = controller.__react_component_helper
2526
end
2627

27-
# @return [String] Prerendered HTML for `component_name` with `options[:props]`
28+
# @return [String] HTML for `component_name` with `options[:props]`
2829
def call(component_name, options, &block)
2930
props = options.fetch(:props, {})
30-
options = options.slice(:data, :aria, :tag, :class, :id).merge(prerender: true)
31+
options = default_options.merge(options.slice(:data, :aria, :tag, :class, :id, :prerender))
3132
react_component(component_name, props, options, &block)
3233
end
34+
35+
private
36+
37+
def default_options
38+
{ prerender: true }
39+
end
3340
end
3441
end
3542
end

test/dummy/app/controllers/server_controller.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,24 @@ def console_example_suppressed
1515
@todos = %w{todo1 todo2 todo3}
1616
end
1717

18-
def inline_component
19-
render component: 'TodoList',
20-
props: { todos: ['Render this inline'] },
21-
tag: 'span',
22-
class: 'custom-class',
23-
id: 'custom-id',
24-
data: { remote: true }
18+
def inline_component_prerender_true
19+
render(component_options)
20+
end
21+
22+
def inline_component_prerender_false
23+
render(component_options.merge(prerender: false))
24+
end
25+
26+
private
27+
28+
def component_options
29+
{
30+
component: 'TodoList',
31+
props: { todos: ['Render this inline'] },
32+
tag: 'span',
33+
class: 'custom-class',
34+
id: 'custom-id',
35+
data: { remote: true }
36+
}
2537
end
2638
end

test/dummy/config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
collection do
55
get :console_example
66
get :console_example_suppressed
7-
get :inline_component
7+
get :inline_component_prerender_true
8+
get :inline_component_prerender_false
89
end
910
end
1011
end

test/server_rendered_html_test.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def wait_to_ensure_asset_pipeline_detects_changes
5353
assert_no_match(/console.error/, response.body)
5454
end
5555

56-
test 'react inline component rendering' do
57-
get '/server/inline_component'
56+
test 'react inline component rendering (pre-rendered)' do
57+
get '/server/inline_component_prerender_true'
5858
rendered_html = response.body
5959
assert_match(/<span.*data-react-class=\"TodoList\"/, rendered_html)
6060
# make sure that the items are prerendered
@@ -67,5 +67,12 @@ def wait_to_ensure_asset_pipeline_detects_changes
6767
assert_match(/id=\"custom-id\"/, rendered_html)
6868
assert_match(/data-remote=\"true\"/, rendered_html)
6969
end
70+
71+
test 'react inline component rendering (not pre-rendered)' do
72+
get '/server/inline_component_prerender_false'
73+
rendered_html = response.body
74+
# make sure the tag closes immediately:
75+
assert_match(/<span.*data-react-class=\"TodoList\"[^<]*><\/span>/, rendered_html)
76+
end
7077
end
7178
end

0 commit comments

Comments
 (0)