Skip to content

Commit 36134a5

Browse files
committed
Ensure proper encoding when reading file from URL
1 parent ee1720b commit 36134a5

5 files changed

Lines changed: 20 additions & 4 deletions

File tree

docs/basics/configuration.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ ReactOnRails.configure do |config|
124124
# While you may configure this to be the same as your client bundle file, this file is typically
125125
# different. Note, be sure to include the exact file name with the ".js" if you are not hashing this file.
126126
# If you are hashing this file (supposing you are using the same file for client rendering), then
127-
#
128127
# you should include a name that matches your bundle name in your webpack config.
129128
config.server_bundle_js_file = "server-bundle.js"
130129

lib/react_on_rails/server_rendering_pool/ruby_embedded_java_script.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def eval_js(js_code, _render_options)
104104
def read_bundle_js_code
105105
server_js_file = ReactOnRails::Utils.server_bundle_js_file_path
106106
if ReactOnRails::Utils.server_bundle_path_is_http?
107-
Net::HTTP.get(URI.parse(server_js_file)).force_encoding("UTF-8")
107+
file_url_to_string(server_js_file)
108108
else
109109
File.read(server_js_file)
110110
end
@@ -204,6 +204,19 @@ def console_polyfill
204204
JS
205205
# rubocop:enable Layout/IndentHeredoc
206206
end
207+
208+
private
209+
210+
def file_url_to_string(url)
211+
response = Net::HTTP.get_response(URI.parse(url))
212+
content_type_header = response["content-type"]
213+
match = content_type_header.match(/\A.*; charset=(?<encoding>.*)\z/)
214+
encoding_type = match[:encoding]
215+
response.body.force_encoding(encoding_type)
216+
rescue StandardError => e
217+
msg = "file_url_to_string #{url} failed\nError is: #{e}"
218+
raise ReactOnRails::Error, msg
219+
end
207220
end
208221
end
209222
end

lib/react_on_rails/utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def self.server_bundle_js_file_path
9696

9797
def self.bundle_js_file_path(bundle_name)
9898
if ReactOnRails::WebpackerUtils.using_webpacker? && bundle_name != "manifest.json"
99-
ReactOnRails::WebpackerUtils.bundle_js_file_path_from_webpacker(bundle_name)
99+
ReactOnRails::WebpackerUtils.bundle_js_uri_from_webpacker(bundle_name)
100100
else
101101
# Default to the non-hashed name in the specified output directory, which, for legacy
102102
# React on Rails, this is the output directory picked up by the asset pipeline.

lib/react_on_rails/webpacker_utils.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def self.dev_server_running?
99
Webpacker.dev_server.running?
1010
end
1111

12-
def self.bundle_js_file_path_from_webpacker(bundle_name)
12+
# This returns either a URL for the webpack-dev-server or a file path
13+
def self.bundle_js_uri_from_webpacker(bundle_name)
1314
# Note Webpacker 3.4.3 manifest lookup is inside of the public_output_path
1415
# [2] (pry) ReactOnRails::WebpackerUtils: 0> Webpacker.manifest.lookup("app-bundle.js")
1516
# "/webpack/development/app-bundle-c1d2b6ab73dffa7d9c0e.js"

spec/dummy/Procfile.static

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Run Rails without hot reloading (static assets).
2+
# For debugging with Pry:
3+
# Tab 1: `foreman start -f Procfile.dev -m puma=0,all=1`
4+
# Tab 2: `rails s webrick`
25
rails: rails s -b 0.0.0.0
36

47
# Build client assets, watching for changes.

0 commit comments

Comments
 (0)