Module: ReactOnRails::ProHelper
- Included in:
- Helper
- Defined in:
- lib/react_on_rails/pro_helper.rb
Instance Method Summary collapse
-
#generate_component_script(render_options) ⇒ Object
Generates the complete component specification script tag.
-
#generate_store_script(redux_store_data) ⇒ Object
Generates the complete store hydration script tag.
- #generated_stylesheet_hrefs_json(render_options) ⇒ Object
Instance Method Details
#generate_component_script(render_options) ⇒ Object
Generates the complete component specification script tag. For Pro users, includes an inline script for immediate hydration during streaming.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/react_on_rails/pro_helper.rb', line 7 def generate_component_script() # Setup the page_loaded_js, which is the same regardless of prerendering or not! # The reason is that React is smart about not doing extra work if the server rendering did its job. component_specification_tag = content_tag(:script, json_safe_and_pretty(.client_props).html_safe, type: "application/json", class: "js-react-on-rails-component", id: "js-react-on-rails-component-#{.dom_id}", "data-component-name" => .react_component_name, "data-trace" => (.trace ? true : nil), "data-dom-id" => .dom_id, "data-ssr-identifier-prefix" => (.html_streaming? ? .dom_id : nil), "data-store-dependencies" => .store_dependencies&.to_json, "data-generated-stylesheet-hrefs" => generated_stylesheet_hrefs_json()) # Add immediate invocation script for Pro users to enable hydration during streaming spec_tag = if ReactOnRails::Utils.react_on_rails_pro? # Escape dom_id for JavaScript context escaped_dom_id = escape_javascript(.dom_id) nonce = csp_nonce = nonce.present? ? { nonce: } : {} immediate_script = content_tag(:script, %( typeof ReactOnRails === 'object' && ReactOnRails.reactOnRailsComponentLoaded('#{escaped_dom_id}'); ).html_safe, ) "#{component_specification_tag}\n#{immediate_script}" else component_specification_tag end spec_tag.html_safe end |
#generate_store_script(redux_store_data) ⇒ Object
Generates the complete store hydration script tag. For Pro users, includes an inline script for immediate hydration during streaming.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/react_on_rails/pro_helper.rb', line 53 def generate_store_script(redux_store_data) store_hydration_data = content_tag(:script, json_safe_and_pretty(redux_store_data[:props]).html_safe, type: "application/json", "data-js-react-on-rails-store" => redux_store_data[:store_name]) # Add immediate invocation script for Pro users to enable hydration during streaming store_hydration_scripts = if ReactOnRails::Utils.react_on_rails_pro? # Escape store_name for JavaScript context escaped_store_name = escape_javascript(redux_store_data[:store_name]) nonce = csp_nonce = nonce.present? ? { nonce: } : {} immediate_script = content_tag( :script, <<~JS.html_safe, typeof ReactOnRails === 'object' && ReactOnRails.reactOnRailsStoreLoaded('#{escaped_store_name}'); JS ) "#{store_hydration_data}\n#{immediate_script}" else store_hydration_data end store_hydration_scripts.html_safe end |
#generated_stylesheet_hrefs_json(render_options) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/react_on_rails/pro_helper.rb', line 42 def generated_stylesheet_hrefs_json() return unless .auto_load_bundle pack_name = "generated/#{.react_component_name}" sources = preload_sources_for_stylesheet_pack(pack_name) hrefs = unique_preload_sources_by_href(sources).map { |source| source.fetch(:href) } hrefs.to_json if hrefs.present? end |