Skip to content

Fix console replay #691

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 1 commit into from
Apr 12, 2017
Merged
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
10 changes: 9 additions & 1 deletion lib/react/rails/component_mount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ def react_component(name, props = {}, options = {}, &block)
# remove internally used properties so they aren't rendered to DOM
html_options.except!(:tag, :prerender, :camelize_props)

content_tag(html_tag, '', html_options, &block)
rendered_tag = content_tag(html_tag, '', html_options, &block)
if React::ServerRendering.renderer_options[:replay_console]
# Grab the server-rendered console replay script
# and move it _outside_ the container div
rendered_tag.sub!(/\n(<script class="react-rails-console-replay">.*<\/script>)<\/(\w+)>$/m,'</\2>\1')
rendered_tag.html_safe
else
rendered_tag
end
end

private
Expand Down
2 changes: 2 additions & 0 deletions lib/react/server_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module ServerRendering
mattr_accessor :renderer, :renderer_options,
:pool_size, :pool_timeout

self.renderer_options = {}

# Discard the old ConnectionPool & create a new one
def self.reset_pool
options = {size: pool_size, timeout: pool_timeout}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function (history) {
if (history && history.length > 0) {
result += '\n<scr'+'ipt>';
result += '\n<scr'+'ipt class="react-rails-console-replay">';
history.forEach(function (msg) {
result += '\nconsole.' + msg.level + '.apply(console, ' + JSON.stringify(msg.arguments) + ');';
});
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/javascript/components/TodoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = React.createClass({
this.setState({mounted: 'yep'});
},
render: function() {
console.log("Test Console Replay")
return (
<ul>
<li id='status'>{this.state.mounted}</li>
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class Application < Rails::Application
# config.i18n.default_locale = :de
config.react.variant = :production
config.react.addons = false
config.react.server_renderer_options = {
replay_console: true,
}

if SprocketsHelpers.available?
config.assets.enabled = true
end
Expand Down
2 changes: 1 addition & 1 deletion test/react/server_rendering/bundle_renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BundleRendererTest < ActiveSupport::TestCase

test '#render replays console messages' do
result = @renderer.render("TodoListWithConsoleLog", {todos: ["log some messages"]}, nil)
assert_match(/<script>$/, result)
assert_match(/<script class="react-rails-console-replay">$/, result)
assert_match(/console.log.apply\(console, \["got initial state"\]\);$/, result)
assert_match(/console.warn.apply\(console, \["mounted component"\]\);$/, result)
assert_match(/console.error.apply\(console, \["rendered!","foo"\]\);$/, result)
Expand Down