Skip to content

Commit 1f1b6c4

Browse files
committed
Fallback to EnvironmentContainer if assets_manifest is not supported
Support for `Rails.application.assets_manifest` which is used by `ServerRendering::ManifestContainer` has only been added in sprockets-rails 2.2.2. Rails 4.0.5 depends on sprockets ~> 2.0.0. * Feature detect if `assets_manifest` is present * Add appraisal to test with rails-sprockets < 2.2.2
1 parent 7fa90ea commit 1f1b6c4

File tree

8 files changed

+37
-13
lines changed

8 files changed

+37
-13
lines changed

Appraisals

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ appraise "rails-4.0" do
77
gem 'rails', '~> 4.0.13'
88
end
99

10+
appraise "rails-4.0.5" do
11+
# Depends on sprockets-rails ~> 2.0.0. Support for
12+
# `Rails.application.assets_manifest` which is used by
13+
# `ServerRendering::ManifestContainer` has only been added in
14+
# sprockets-rails 2.2.2. Ensure that server rendering falls back to
15+
# `ServerRendering::EnvironmentContainer`.`
16+
gem 'rails', '4.0.5'
17+
end
18+
1019
appraise "rails-4.0-with-therubyracer" do
1120
gem 'rails', '~> 4.0.13'
1221
gem 'therubyracer', '0.12.0', :platform => :mri

gemfiles/rails_4.0.5.gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file was generated by Appraisal
2+
3+
source "http://rubygems.org"
4+
5+
gem "rails", "4.0.5"
6+
7+
gemspec :path => "../"

lib/react/server_rendering/manifest_container.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def find_asset(logical_path)
1414
asset_full_path = ::Rails.root.join("public", @manifest.dir, asset_path)
1515
File.read(asset_full_path)
1616
end
17+
18+
# sprockets-rails < 2.2.2 does not have `application.assets_manifest=`
19+
def self.compatible?
20+
::Rails.application.respond_to?(:assets_manifest)
21+
end
1722
end
1823
end
1924
end

lib/react/server_rendering/sprockets_renderer.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,18 @@ class << self
6161
def asset_container
6262
@asset_container ||= if self.class.asset_container_class.present?
6363
self.class.asset_container_class.new
64-
elsif ::Rails.application.config.assets.compile
65-
EnvironmentContainer.new
64+
elsif assets_precompiled? && ManifestContainer.compatible?
65+
ManifestContainer.new
66+
elsif assets_precompiled? && YamlManifestContainer.compatible?
67+
YamlManifestContainer.new
6668
else
67-
if ::Rails::VERSION::MAJOR == 3
68-
YamlManifestContainer.new
69-
else
70-
ManifestContainer.new
71-
end
69+
EnvironmentContainer.new
7270
end
7371
end
72+
73+
def assets_precompiled?
74+
!::Rails.application.config.assets.compile
75+
end
7476
end
7577
end
7678
end

lib/react/server_rendering/yaml_manifest_container.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def find_asset(logical_path)
1414
asset_full_path = ::Rails.root.join("public", "assets", asset_path)
1515
File.read(asset_full_path)
1616
end
17+
18+
def self.compatible?
19+
::Rails::VERSION::MAJOR == 3
20+
end
1721
end
1822
end
1923
end

test/react/server_rendering/manifest_container_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
require "test_helper"
22

3-
# Rails 3.x doesn't have `application.assets_manifest=`
4-
# I don't think we have to support Rails 3 + Sprockets 3 anyways!
5-
if Rails::VERSION::MAJOR > 3
3+
if React::ServerRendering::ManifestContainer.compatible?
64
class ManifestContainerTest < ActiveSupport::TestCase
75
def setup
86
precompile_assets

test/react/server_rendering/yaml_manifest_container_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "test_helper"
22

3-
if Rails::VERSION::MAJOR == 3
3+
if React::ServerRendering::YamlManifestContainer.compatible?
44
class YamlManifestContainerTest < ActiveSupport::TestCase
55
def setup
66
precompile_assets
@@ -18,4 +18,3 @@ def test_find_asset_gets_asset_contents
1818
end
1919
end
2020
end
21-

test/test_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def precompile_assets
5454
Rake::Task['assets:precompile'].invoke
5555
end
5656

57-
if Rails::VERSION::MAJOR > 3
57+
if Rails.application.respond_to?(:assets_manifest)
5858
# Make a new manifest since assets weren't compiled before
5959
config = Rails.application.config
6060
path = File.join(config.paths['public'].first, config.assets.prefix)

0 commit comments

Comments
 (0)