Skip to content

Commit 968d766

Browse files
authored
Merge pull request #692 from reactjs/fix-console-replay-state
fix(console_replay) clear state between renders
2 parents 292f917 + c8b7756 commit 968d766

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

lib/react/server_rendering/bundle_renderer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class BundleRenderer < ExecJSRenderer
1313
# Reimplement console methods for replaying on the client
1414
CONSOLE_POLYFILL = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/console_polyfill.js"))
1515
CONSOLE_REPLAY = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/console_replay.js"))
16+
CONSOLE_RESET = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/console_reset.js"))
1617
TIMEOUT_POLYFILL = File.read(File.join(File.dirname(__FILE__), "bundle_renderer/timeout_polyfill.js"))
1718

1819
def initialize(options={})
@@ -39,6 +40,10 @@ def render(component_name, props, prerender_options)
3940
super(component_name, t_props, t_options)
4041
end
4142

43+
def before_render(component_name, props, prerender_options)
44+
@replay_console ? CONSOLE_RESET : ""
45+
end
46+
4247
def after_render(component_name, props, prerender_options)
4348
@replay_console ? CONSOLE_REPLAY : ""
4449
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if (typeof console !== "undefined" && console.history) {
2+
console.history = [];
3+
}

test/dummy/app/assets/javascripts/components/TodoList.js.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ TodoList = React.createClass({
66
this.setState({mounted: 'yep'});
77
},
88
render: function() {
9+
console.log("Test Console Replay")
910
return (
1011
<ul>
1112
<li id='status'>{this.state.mounted}</li>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "test_helper"
2+
3+
if WebpackerHelpers.available? || SprocketsHelpers.available?
4+
class ConsoleReplayTest < ActionDispatch::IntegrationTest
5+
setup do
6+
WebpackerHelpers.compile
7+
React::ServerRendering.renderer_options = {replay_console: true}
8+
React::ServerRendering.reset_pool
9+
end
10+
11+
EXPECTED_REPLAY = <<-HTML
12+
<script class="react-rails-console-replay">
13+
console.log.apply(console, ["Test Console Replay"]);
14+
</script>
15+
HTML
16+
17+
test "it clears the state between each request" do
18+
# Each request should only contain one log:
19+
get '/server/1'
20+
assert_includes(response.body, EXPECTED_REPLAY)
21+
get '/server/1'
22+
assert_includes(response.body, EXPECTED_REPLAY)
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)