Skip to content

Commit fa1d062

Browse files
committed
raise any JS error when prerendering
1 parent 45ca4ad commit fa1d062

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/react/renderer.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
module React
44
class Renderer
55

6+
class PrerenderError < RuntimeError
7+
def initialize(component_name, props, js_message)
8+
message = "Encountered error \"#{js_message}\" when prerendering #{component_name} with #{props.to_json}"
9+
super(message)
10+
end
11+
end
12+
613
cattr_accessor :pool
714

815
def self.setup!(react_js, components_js, args={})
@@ -56,13 +63,8 @@ def render(component, args={})
5663
}()
5764
JS
5865
context.eval(jscode).html_safe
59-
# What should be done here? If we are server rendering, and encounter an error in the JS code,
60-
# then log it and continue, which will just render the react ujs tag, and when the browser tries
61-
# to render the component it will most likely encounter the same error and throw to the browser
62-
# console for a better debugging experience.
6366
rescue ExecJS::ProgramError => e
64-
::Rails.logger.error "[React::Renderer] #{e.message}"
67+
raise PrerenderError.new(component, args, e)
6568
end
66-
6769
end
6870
end

test/react_renderer_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ class ReactRendererTest < ActiveSupport::TestCase
1515
end
1616
end
1717
end
18+
19+
test 'prerender errors are thrown' do
20+
err = assert_raises React::Renderer::PrerenderError do
21+
React::Renderer.render("NonexistentComponent", {error: true, exists: false})
22+
end
23+
expected_message = 'Encountered error "ReferenceError: NonexistentComponent is not defined" when prerendering NonexistentComponent with {"error":true,"exists":false}'
24+
assert_equal expected_message, err.message
25+
end
1826
end

0 commit comments

Comments
 (0)