Skip to content

Commit c2d6827

Browse files
authored
Merge pull request #691 from reactjs/fix-console-replay
Fix console replay
2 parents a89d84f + e199d3f commit c2d6827

File tree

6 files changed

+18
-3
lines changed

6 files changed

+18
-3
lines changed

lib/react/rails/component_mount.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ def react_component(name, props = {}, options = {}, &block)
4646
# remove internally used properties so they aren't rendered to DOM
4747
html_options.except!(:tag, :prerender, :camelize_props)
4848

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

5260
private

lib/react/server_rendering.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module ServerRendering
77
mattr_accessor :renderer, :renderer_options,
88
:pool_size, :pool_timeout
99

10+
self.renderer_options = {}
11+
1012
# Discard the old ConnectionPool & create a new one
1113
def self.reset_pool
1214
options = {size: pool_size, timeout: pool_timeout}

lib/react/server_rendering/bundle_renderer/console_replay.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function (history) {
22
if (history && history.length > 0) {
3-
result += '\n<scr'+'ipt>';
3+
result += '\n<scr'+'ipt class="react-rails-console-replay">';
44
history.forEach(function (msg) {
55
result += '\nconsole.' + msg.level + '.apply(console, ' + JSON.stringify(msg.arguments) + ');';
66
});

test/dummy/app/javascript/components/TodoList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = React.createClass({
88
this.setState({mounted: 'yep'});
99
},
1010
render: function() {
11+
console.log("Test Console Replay")
1112
return (
1213
<ul>
1314
<li id='status'>{this.state.mounted}</li>

test/dummy/config/application.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Application < Rails::Application
3232
# config.i18n.default_locale = :de
3333
config.react.variant = :production
3434
config.react.addons = false
35+
config.react.server_renderer_options = {
36+
replay_console: true,
37+
}
38+
3539
if SprocketsHelpers.available?
3640
config.assets.enabled = true
3741
end

test/react/server_rendering/bundle_renderer_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class BundleRendererTest < ActiveSupport::TestCase
4949

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

0 commit comments

Comments
 (0)