Sha256: 727cbab8e5b012e2a4b74d9e47904538f4d4e97958b97dfb96c54729d47a91e8

Contents?: true

Size: 1.49 KB

Versions: 10

Compression:

Stored size: 1.49 KB

Contents

module React
  module ServerRendering
    # A bare-bones renderer for React.js + Exec.js
    # - Depends on global ReactDOMServer in the ExecJS context
    # - No Rails dependency
    # - No browser concerns
    class ExecJSRenderer
      def initialize(options={})
        js_code = options[:code] || raise("Pass `code:` option to instantiate a JS context!")
        @context = ExecJS.compile(GLOBAL_WRAPPER + js_code)
      end

      def render(component_name, props, prerender_options)
        render_function = prerender_options.fetch(:render_function, "renderToString")
        js_code = <<-JS
          (function () {
            #{before_render(component_name, props, prerender_options)}
            var result = ReactDOMServer.#{render_function}(React.createElement(#{component_name}, #{props}));
            #{after_render(component_name, props, prerender_options)}
            return result;
          })()
        JS
        @context.eval(js_code).html_safe
      rescue ExecJS::ProgramError => err
        raise React::ServerRendering::PrerenderError.new(component_name, props, err)
      end

      # Hooks for inserting JS before/after rendering
      def before_render(component_name, props, prerender_options); ""; end
      def after_render(component_name, props, prerender_options); ""; end

      # Handle Node.js & other ExecJS contexts
      GLOBAL_WRAPPER = <<-JS
        var global = global || this;
        var self = self || this;
        var window = window || this;
      JS

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
react-rails-1.9.0 lib/react/server_rendering/exec_js_renderer.rb
react-rails-1.8.2 lib/react/server_rendering/exec_js_renderer.rb
react-rails-1.8.1 lib/react/server_rendering/exec_js_renderer.rb
react-rails-1.8.0 lib/react/server_rendering/exec_js_renderer.rb
react-rails-1.7.2 lib/react/server_rendering/exec_js_renderer.rb
react-rails-1.7.1 lib/react/server_rendering/exec_js_renderer.rb
react-rails-1.7.0 lib/react/server_rendering/exec_js_renderer.rb
react-rails-1.6.2 lib/react/server_rendering/exec_js_renderer.rb
react-rails-1.6.1 lib/react/server_rendering/exec_js_renderer.rb
react-rails-1.6.0 lib/react/server_rendering/exec_js_renderer.rb