Skip to content

Commit 727a8d6

Browse files
committed
feat(ServerRendering) add end-to-end test of per-request prerenderer
1 parent 9acd261 commit 727a8d6

17 files changed

+64
-11
lines changed

Appraisals

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ appraise "rails-4.2-sprockets_4" do
4343
gem 'rails', '~> 4.2.1'
4444
gem "sprockets", "~> 4.0.x"
4545
gem "turbolinks", "~> 2.5.0"
46+
# This ExecJS backend provides stateful context
47+
# which the default nodejs backend does not
48+
gem "mini_racer"
4649
end
4750

4851
appraise "rails-5" do
4952
gem 'rails', '~> 5.0.0'
5053
gem "turbolinks", "~> 5.0.0"
54+
# This ExecJS backend provides stateful context
55+
# which the default nodejs backend does not
56+
gem "therubyracer"
5157
end
5258

5359
appraise "rails-5-no_sprockets" do

gemfiles/rails_3.2.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
source "http://rubygems.org"
44

5+
gem "turbolinks", "~> 2.0"
56
gem "rails", "~> 3.2.21"
67
gem "rack-cache", "~> 1.6.1"
7-
gem "turbolinks", "~> 2.0"
88

99
gemspec :path => "../"

gemfiles/rails_4.0.5.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
source "http://rubygems.org"
44

5-
gem "rails", "4.0.5"
65
gem "turbolinks"
6+
gem "rails", "4.0.5"
77

88
gemspec :path => "../"

gemfiles/rails_4.0_with_therubyracer.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
source "http://rubygems.org"
44

5+
gem "turbolinks"
56
gem "rails", "~> 4.0.13"
67
gem "therubyracer", "0.12.0", :platform => :mri
7-
gem "turbolinks"
88

99
gemspec :path => "../"

gemfiles/rails_4.1.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
source "http://rubygems.org"
44

5-
gem "rails", "~> 4.1.10"
65
gem "turbolinks", "~> 2.3.0"
6+
gem "rails", "~> 4.1.10"
77

88
gemspec :path => "../"

gemfiles/rails_4.2_sprockets_2.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
source "http://rubygems.org"
44

5+
gem "turbolinks"
56
gem "rails", "~> 4.2.1"
67
gem "sprockets", "~> 2.12"
7-
gem "turbolinks"
88

99
gemspec :path => "../"

gemfiles/rails_4.2_sprockets_3.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
source "http://rubygems.org"
44

5+
gem "turbolinks", "~> 2.5.0"
56
gem "rails", "~> 4.2.1"
67
gem "sprockets", "~> 3.5"
7-
gem "turbolinks", "~> 2.5.0"
88

99
gemspec :path => "../"

gemfiles/rails_4.2_sprockets_4.gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
source "http://rubygems.org"
44

5+
gem "turbolinks", "~> 2.5.0"
56
gem "rails", "~> 4.2.1"
67
gem "sprockets", "~> 4.0.x"
7-
gem "turbolinks", "~> 2.5.0"
8+
gem "mini_racer"
89

910
gemspec :path => "../"

gemfiles/rails_5.gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
source "http://rubygems.org"
44

5-
gem "rails", "~> 5.0.0"
65
gem "turbolinks", "~> 5.0.0"
6+
gem "rails", "~> 5.0.0"
7+
gem "therubyracer"
78

89
gemspec :path => "../"

gemfiles/rails_5_no_sprockets.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
source "http://rubygems.org"
44

5+
gem "turbolinks"
56
gem "rails", "~> 5.0.0"
67

78
gemspec :path => "../"

gemfiles/rails_5_sprockets_4.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
source "http://rubygems.org"
44

5+
gem "turbolinks", "~> 5.0.0"
56
gem "rails", "~> 5.0.0"
67
gem "sprockets", "~> 4.0.x"
7-
gem "turbolinks", "~> 5.0.0"
88

99
gemspec :path => "../"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
//= require_self
22
//= require_tree ./components
3+
//= require ./pages

test/dummy/app/assets/javascripts/pages.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
var GreetingMessage = React.createClass({
22
getInitialState: function() {
3-
return {greeting: 'Hello'};
3+
var initialGreeting = 'Hello';
4+
if (typeof global !== "undefined" && global.ctx && global.ctx.greeting) {
5+
initialGreeting = global.ctx.greeting
6+
}
7+
8+
return {
9+
greeting: initialGreeting
10+
}
411
},
512
goodbye: function() {
613
this.setState({greeting: 'Goodbye'});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
class PagesController < ApplicationController
2+
around_action :per_request_react_prerenderer
3+
24
def show
5+
@prerender = !!params[:prerender]
6+
if @prerender
7+
js_context = __prerenderer.context
8+
# This isn't safe for production, we're just testing the render context:
9+
greeting_override = params[:greeting] || ""
10+
setup_code = "global.ctx = {}; global.ctx.greeting = '#{greeting_override}';"
11+
js_context.exec(setup_code)
12+
end
313
@name = %w(Alice Bob)[params[:id].to_i % 2]
14+
render :show
15+
if @prerender
16+
js_context.exec("global.ctx = undefined;")
17+
end
418
end
519
end

test/dummy/app/views/pages/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</ul>
55

66
<div id='component-parent'>
7-
<%= react_component 'GreetingMessage', { :name => @name }, { :id => 'component' } %>
7+
<%= react_component 'GreetingMessage', { :name => @name }, { :id => 'component', prerender: @prerender } %>
88
</div>
99

1010
<button onClick="ReactRailsUJS.unmountComponents('#component-parent')">Unmount by parent selector</button>

test/react/rails/pages_controller_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,20 @@ class PagesControllerTest < ActionController::TestCase
55
get :show, id: 1
66
assert_equal(200, response.status)
77
end
8+
9+
when_stateful_js_context_available do
10+
test "it sets up and tears down a react context" do
11+
get :show, id: 1, prerender: true
12+
assert_includes(response.body, "Hello")
13+
14+
get :show, id: 1, prerender: true, greeting: "Howdy"
15+
assert_includes(response.body, "Howdy")
16+
17+
get :show, id: 1, prerender: true, greeting: "👋"
18+
assert_includes(response.body, "👋")
19+
20+
get :show, id: 1, prerender: true
21+
assert_includes(response.body, "Hello")
22+
end
23+
end
824
end

test/test_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,9 @@ def assert_compiled_javascript_matches(javascript, expectation)
129129
def assert_compiled_javascript_includes(javascript, expected_part)
130130
assert_includes javascript.gsub(/\s/, ''), expected_part.gsub(/\s/, '')
131131
end
132+
133+
def when_stateful_js_context_available
134+
if defined?(V8) || defined?(MiniRacer)
135+
yield
136+
end
137+
end

0 commit comments

Comments
 (0)