Skip to content

Add suffix code for context during server rendering #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ react_component('HelloMessage', {name: 'John'}, {prerender: true})
```
This will return the fully rendered component markup, and as long as you have included the `react_ujs` script in your page, then the component will also be instantiated and mounted on the client.

#### Preload globals

If you want to preload some global variables, generated by Gon for example,
use the same view helper `react_component`, and pass in `context_suffix: 'some loading javascript'` in the `options` hash.

```erb
react_component('HelloMessage', {name: 'John'}, {prerender: true, context_suffix: 'window.myVar = 123;'})
```

This will return the fully rendered component markup, that can use preloaded variables

## Configuring

### Variants
Expand Down
3 changes: 2 additions & 1 deletion lib/react/rails/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module ViewHelper
#
def react_component(name, args = {}, options = {}, &block)
options = {:tag => options} if options.is_a?(Symbol)
block = Proc.new{concat React::Renderer.render(name, args)} if options[:prerender] == true
context_suffix = options.delete(:context_suffix) || ''
block = Proc.new{concat React::Renderer.render(name, args, context_suffix)} if options[:prerender] == true

html_options = options.reverse_merge(:data => {})
html_options[:data].tap do |data|
Expand Down
20 changes: 14 additions & 6 deletions lib/react/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def self.setup!(react_js, components_js, args={})
@@pool = ConnectionPool.new(:size => args[:size]||10, :timeout => args[:timeout]||20) { self.new }
end

def self.render(component, args={})
def self.render(component, args={}, context_suffix='')
@@pool.with do |renderer|
renderer.render(component, args)
renderer.render(component, args, context_suffix)
end
end

Expand Down Expand Up @@ -62,18 +62,26 @@ def self.react_props(args={})
end
end

def context
@context ||= ExecJS.compile(self.class.combined_js)
def context(suffix_code='')
if suffix_code.present?
reset_context(suffix_code)
else
@context ||= ExecJS.compile(self.class.combined_js)
end
end

def reset_context(suffix_code='')
@context = ExecJS.compile(self.class.combined_js + suffix_code)
end

def render(component, args={})
def render(component, args={}, context_suffix='')
react_props = React::Renderer.react_props(args)
jscode = <<-JS
function() {
return React.renderToString(React.createElement(#{component}, #{react_props}));
}()
JS
context.eval(jscode).html_safe
context(context_suffix).eval(jscode).html_safe
rescue ExecJS::ProgramError => e
raise PrerenderError.new(component, react_props, e)
end
Expand Down
18 changes: 18 additions & 0 deletions test/dummy/app/assets/javascripts/components/TodoGlobalList.js.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
TodoGlobalList = React.createClass({
getInitialState: function() {
return({mounted: "nope"});
},
componentWillMount: function() {
this.setState({mounted: 'yep'});
},
render: function() {
return (
<ul>
<li id='status'>{this.state.mounted}</li>
{todos.map(function(todo, i) {
return (<Todo key={i} todo={todo} />)
})}
</ul>
)
}
})
6 changes: 6 additions & 0 deletions test/react_renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class ReactRendererTest < ActiveSupport::TestCase
assert_match /data-react-checksum/, result
end

test 'Server rendering with context suffix ' do
result = React::Renderer.render "TodoGlobalList", {}, 'window.todos=["todo1","todo2","todo3"];'
assert_match /todo1.*todo2.*todo3/, result
assert_match /data-react-checksum/, result
end

test 'Rendering does not throw an exception when console log api is used' do
%W(error info log warn).each do |fn|
assert_nothing_raised(ExecJS::ProgramError) do
Expand Down