class React::ServerRendering::BundleRenderer
Extends ExecJSRenderer
for the Rails
environment
-
fetches JS code from the
Rails
app (webpacker or sprockets) -
stringifies props
-
implements console replay
Constants
- CONSOLE_POLYFILL
Reimplement console methods for replaying on the client
- CONSOLE_REPLAY
- CONSOLE_RESET
- TIMEOUT_POLYFILL
Attributes
Public Class Methods
# File lib/react/server_rendering/bundle_renderer.rb, line 19 def initialize(options={}) @replay_console = options.fetch(:replay_console, true) filenames = options.fetch(:files, ['server_rendering.js']) js_code = CONSOLE_POLYFILL.dup js_code << TIMEOUT_POLYFILL.dup js_code << options.fetch(:code, '') filenames.each do |filename| js_code << asset_container.find_asset(filename) end super(options.merge(code: js_code)) end
Public Instance Methods
# File lib/react/server_rendering/bundle_renderer.rb, line 47 def after_render(component_name, props, prerender_options) @replay_console ? CONSOLE_REPLAY : '' end
Get an object which exposes assets by their logical path.
Out of the box, it supports a Sprockets::Environment (application.assets) and a Sprockets::Manifest (application.assets_manifest), which covers the default Rails
setups.
You can provide a custom asset container with ‘React::ServerRendering::BundleRenderer.asset_container_class = MyAssetContainer`.
@return [#find_asset(logical_path)] An object that returns asset contents by logical path
# File lib/react/server_rendering/bundle_renderer.rb, line 65 def asset_container @asset_container ||= asset_container_class.new end
# File lib/react/server_rendering/bundle_renderer.rb, line 43 def before_render(component_name, props, prerender_options) @replay_console ? CONSOLE_RESET : '' end
Prerender options are expected to be a Hash however might also be a symbol. pass prerender: :static to use renderToStaticMarkup pass prerender: true to enable default prerender pass prerender: {} to proxy some custom options
# File lib/react/server_rendering/bundle_renderer.rb, line 37 def render(component_name, props, prerender_options) t_options = prepare_options(prerender_options) t_props = prepare_props(props) super(component_name, t_props, t_options) end
Private Instance Methods
Detect what kind of asset system is in use and choose that container. Or, if the user has provided {.asset_container_class}, use that. @return [Class] suitable for {#asset_container}
# File lib/react/server_rendering/bundle_renderer.rb, line 102 def asset_container_class if self.class.asset_container_class.present? self.class.asset_container_class elsif WebpackerManifestContainer.compatible? WebpackerManifestContainer elsif assets_precompiled? if ManifestContainer.compatible? ManifestContainer elsif YamlManifestContainer.compatible? YamlManifestContainer else # Even though they are precompiled, we can't find them :S EnvironmentContainer end else EnvironmentContainer end end
# File lib/react/server_rendering/bundle_renderer.rb, line 95 def assets_precompiled? !::Rails.application.config.assets.compile end
# File lib/react/server_rendering/bundle_renderer.rb, line 71 def prepare_options(options) r_func = render_function(options) opts = case options when Hash then options when TrueClass then {} else {} end # This seems redundant to pass opts.merge(render_function: r_func) end
# File lib/react/server_rendering/bundle_renderer.rb, line 91 def prepare_props(props) props.is_a?(String) ? props : props.to_json end
# File lib/react/server_rendering/bundle_renderer.rb, line 83 def render_function(opts) if opts == :static 'renderToStaticMarkup'.freeze else 'renderToString'.freeze end end