-
Notifications
You must be signed in to change notification settings - Fork 754
Decouple renderer #253
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
Merged
Merged
Decouple renderer #253
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
799ffce
feat(ServerRendering) add ServerRendering and SprocketsRenderer
rmosolgo 381d8c8
feat(SprocketsRenderer) add error handling and console replay
rmosolgo beb61af
feat(ServerRendering) remove previous Renderer
rmosolgo e1e319d
feat(ServerRendering) update readme, fix configs
rmosolgo 61170a2
feat(ViewHelper) pass the prerender option to the renderer
rmosolgo 298f23b
feat(SprocketsRenderer) use prerender: :static
rmosolgo df6ceef
refactor(SprocketsRenderer) don't use SetupJavascript
rmosolgo 486a8ea
Fix tests
rmosolgo 981e3d9
feat(renderer) fix asset test
rmosolgo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
guard :minitest do | ||
# with Minitest::Unit | ||
watch(%r{^test/(.*)\/?(.*)_test\.rb$}) | ||
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" } | ||
watch(%r{^test/test_helper\.rb$}) { 'test' } | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'react/jsx' | ||
require 'react/renderer' | ||
require 'react/rails' | ||
require 'react/console' | ||
require 'react/server_rendering' | ||
|
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,29 @@ | ||
module React | ||
module Rails | ||
module ViewHelper | ||
|
||
# Render a UJS-type HTML tag annotated with data attributes, which | ||
# are used by react_ujs to actually instantiate the React component | ||
# on the client. | ||
# | ||
def react_component(name, args = {}, options = {}, &block) | ||
def react_component(name, props = {}, options = {}, &block) | ||
options = {:tag => options} if options.is_a?(Symbol) | ||
block = Proc.new{concat React::Renderer.render(name, args)} if options[:prerender] | ||
|
||
prerender_options = options[:prerender] | ||
if prerender_options | ||
block = Proc.new{ concat React::ServerRendering.render(name, props.merge(prerender: prerender_options)) } | ||
end | ||
|
||
html_options = options.reverse_merge(:data => {}) | ||
html_options[:data].tap do |data| | ||
data[:react_class] = name | ||
data[:react_props] = React::Renderer.react_props(args) unless args.empty? | ||
data[:react_props] = (props.is_a?(String) ? props : props.to_json) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should always be JSON because |
||
end | ||
html_tag = html_options[:tag] || :div | ||
|
||
# remove internally used properties so they aren't rendered to DOM | ||
html_options.except!(:tag, :prerender) | ||
|
||
content_tag(html_tag, '', html_options, &block) | ||
end | ||
|
||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'connection_pool' | ||
require 'react/server_rendering/sprockets_renderer' | ||
|
||
module React | ||
module ServerRendering | ||
mattr_accessor :renderer, :renderer_options, | ||
:pool_size, :pool_timeout | ||
|
||
def self.reset_pool | ||
options = {size: pool_size, timeout: pool_timeout} | ||
@@pool = ConnectionPool.new(options) { create_renderer } | ||
end | ||
|
||
def self.render(component_name, props) | ||
@@pool.with do |renderer| | ||
renderer.render(component_name, props) | ||
end | ||
end | ||
|
||
def self.create_renderer | ||
renderer.new(renderer_options) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
module React | ||
module ServerRendering | ||
class SprocketsRenderer | ||
def initialize(options={}) | ||
@replay_console = options.fetch(:replay_console, true) | ||
|
||
filenames = options.fetch(:files, ["react.js", "components.js"]) | ||
js_code = GLOBAL_WRAPPER + CONSOLE_POLYFILL | ||
|
||
filenames.each do |filename| | ||
js_code << ::Rails.application.assets[filename].to_s | ||
end | ||
|
||
@context = ExecJS.compile(js_code) | ||
end | ||
|
||
def render(component_name, props) | ||
# pass prerender: :static to use renderToStaticMarkup | ||
if props.is_a?(Hash) && props[:prerender] == :static | ||
react_render_method = "renderToStaticMarkup" | ||
else | ||
react_render_method = "renderToString" | ||
end | ||
|
||
if !props.is_a?(String) | ||
props = props.to_json | ||
end | ||
|
||
js_code = <<-JS | ||
(function () { | ||
var result = React.#{react_render_method}(React.createElement(#{component_name}, #{props})); | ||
#{@replay_console ? CONSOLE_REPLAY : ""} | ||
return result; | ||
})() | ||
JS | ||
|
||
@context.eval(js_code).html_safe | ||
rescue ExecJS::ProgramError => err | ||
raise PrerenderError.new(component_name, props, err) | ||
end | ||
|
||
# Handle node.js & other RubyRacer contexts | ||
GLOBAL_WRAPPER = <<-JS | ||
var global = global || this; | ||
var self = self || this; | ||
var window = window || this; | ||
JS | ||
|
||
# Reimplement console methods for replaying on the client | ||
CONSOLE_POLYFILL = <<-JS | ||
var console = { history: [] }; | ||
['error', 'log', 'info', 'warn'].forEach(function (fn) { | ||
console[fn] = function () { | ||
console.history.push({level: fn, arguments: Array.prototype.slice.call(arguments)}); | ||
}; | ||
}); | ||
JS | ||
|
||
# Replay message from console history | ||
CONSOLE_REPLAY = <<-JS | ||
(function (history) { | ||
if (history && history.length > 0) { | ||
result += '\\n<scr'+'ipt>'; | ||
history.forEach(function (msg) { | ||
result += '\\nconsole.' + msg.level + '.apply(console, ' + JSON.stringify(msg.arguments) + ');'; | ||
}); | ||
result += '\\n</scr'+'ipt>'; | ||
} | ||
})(console.history); | ||
JS | ||
|
||
class PrerenderError < RuntimeError | ||
def initialize(component_name, props, js_message) | ||
message = ["Encountered error \"#{js_message}\" when prerendering #{component_name} with #{props}", | ||
js_message.backtrace.join("\n")].join("\n") | ||
super(message) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guard throws a circular loading in progress warning