Class: React::ServerRendering::SprocketsRenderer
- Inherits:
-
ExecJSRenderer
- Object
- ExecJSRenderer
- React::ServerRendering::SprocketsRenderer
- Defined in:
- lib/react/server_rendering/sprockets_renderer.rb
Constant Summary collapse
- CONSOLE_POLYFILL =
Reimplement console methods for replaying on the client
" var console = { history: [] };\n ['error', 'log', 'info', 'warn'].forEach(function (fn) {\n console[fn] = function () {\n console.history.push({level: fn, arguments: Array.prototype.slice.call(arguments)});\n };\n });\n"- CONSOLE_REPLAY =
Replay message from console history
" (function (history) {\n if (history && history.length > 0) {\n result += '\\\\n<scr'+'ipt>';\n history.forEach(function (msg) {\n result += '\\\\nconsole.' + msg.level + '.apply(console, ' + JSON.stringify(msg.arguments) + ');';\n });\n result += '\\\\n</scr'+'ipt>';\n }\n })(console.history);\n"
Constants inherited from ExecJSRenderer
ExecJSRenderer::GLOBAL_WRAPPER
Instance Method Summary collapse
- #after_render(component_name, props, prerender_options) ⇒ Object
-
#initialize(options = {}) ⇒ SprocketsRenderer
constructor
A new instance of SprocketsRenderer.
- #render(component_name, props, prerender_options) ⇒ Object
Methods inherited from ExecJSRenderer
Constructor Details
#initialize(options = {}) ⇒ SprocketsRenderer
Returns a new instance of SprocketsRenderer.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/react/server_rendering/sprockets_renderer.rb', line 8 def initialize(={}) @replay_console = .fetch(:replay_console, true) filenames = .fetch(:files, ["react-server.js", "components.js"]) js_code = CONSOLE_POLYFILL.dup filenames.each do |filename| js_code << ::Rails.application.assets[filename].to_s end super(.merge(code: js_code)) end |
Instance Method Details
#after_render(component_name, props, prerender_options) ⇒ Object
35 36 37 |
# File 'lib/react/server_rendering/sprockets_renderer.rb', line 35 def after_render(component_name, props, ) @replay_console ? CONSOLE_REPLAY : "" end |
#render(component_name, props, prerender_options) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/react/server_rendering/sprockets_renderer.rb', line 20 def render(component_name, props, ) # pass prerender: :static to use renderToStaticMarkup react_render_method = if == :static "renderToStaticMarkup" else "renderToString" end if !props.is_a?(String) props = props.to_json end super(component_name, props, {render_function: react_render_method}) end |