Class: ReactOnRailsPro::Request
- Inherits:
-
Object
- Object
- ReactOnRailsPro::Request
- Defined in:
- lib/react_on_rails_pro/request.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- CONNECTION_MUTEX =
Mutex for thread-safe connection management. Using a constant eliminates the race condition that would exist with @mutex ||= Mutex.new
Mutex.new
Class Method Summary collapse
- .asset_exists_on_vm_renderer?(filename) ⇒ Boolean
- .render_code(path, js_code, send_bundle) ⇒ Object
- .render_code_as_stream(path, js_code, is_rsc_payload:) ⇒ Object
- .reset_connection ⇒ Object
- .upload_assets ⇒ Object
Class Method Details
.asset_exists_on_vm_renderer?(filename) ⇒ Boolean
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/react_on_rails_pro/request.rb', line 81 def asset_exists_on_vm_renderer?(filename) Rails.logger.info { "[ReactOnRailsPro] Sending request to check if file exist on node-renderer: #{filename}" } form_data = common_form_data # Add targetBundles from the current bundle hash and RSC bundle hash pool = ReactOnRailsPro::ServerRenderingPool::NodeRenderingPool target_bundles = [pool.server_bundle_hash] target_bundles << pool.rsc_bundle_hash if ReactOnRailsPro.configuration.enable_rsc_support form_data["targetBundles"] = target_bundles response = perform_request("/asset-exists?filename=#{filename}", json: form_data) JSON.parse(response.body)["exists"] == true end |
.render_code(path, js_code, send_bundle) ⇒ Object
23 24 25 26 27 |
# File 'lib/react_on_rails_pro/request.rb', line 23 def render_code(path, js_code, send_bundle) Rails.logger.info { "[ReactOnRailsPro] Perform rendering request #{path}" } form = form_with_code(js_code, send_bundle) perform_request(path, form: form) end |
.render_code_as_stream(path, js_code, is_rsc_payload:) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/react_on_rails_pro/request.rb', line 29 def render_code_as_stream(path, js_code, is_rsc_payload:) Rails.logger.info { "[ReactOnRailsPro] Perform rendering request as a stream #{path}" } if is_rsc_payload && !ReactOnRailsPro.configuration.enable_rsc_support raise ReactOnRailsPro::Error, "RSC support is not enabled. Please set enable_rsc_support to true in your " \ "config/initializers/react_on_rails_pro.rb file before " \ "rendering any RSC payload." end ReactOnRailsPro::StreamRequest.create do |send_bundle| form = form_with_code(js_code, send_bundle) perform_request(path, form: form, stream: true) end end |
.reset_connection ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/react_on_rails_pro/request.rb', line 14 def reset_connection CONNECTION_MUTEX.synchronize do new_conn = create_connection old_conn = @connection @connection = new_conn old_conn&.close end end |
.upload_assets ⇒ Object
44 45 46 47 48 49 50 51 52 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 79 |
# File 'lib/react_on_rails_pro/request.rb', line 44 def upload_assets Rails.logger.info { "[ReactOnRailsPro] Uploading assets" } # Early checks with descriptive messages. add_bundle_to_form(check_bundle: true) also # validates existence, but these provide clearer context for the rake task user. server_bundle_path = ReactOnRails::Utils.server_bundle_js_file_path unless File.exist?(server_bundle_path) raise ReactOnRailsPro::Error, "Server bundle not found at #{server_bundle_path}. " \ "Please build your bundles before uploading assets." end # Create a list of bundle timestamps to send to the node renderer pool = ReactOnRailsPro::ServerRenderingPool::NodeRenderingPool target_bundles = [pool.server_bundle_hash] # Add RSC bundle if enabled if ReactOnRailsPro.configuration.enable_rsc_support rsc_bundle_path = ReactOnRailsPro::Utils.rsc_bundle_js_file_path unless File.exist?(rsc_bundle_path) raise ReactOnRailsPro::Error, "RSC bundle not found at #{rsc_bundle_path}. " \ "Please build your bundles before uploading assets." end target_bundles << pool.rsc_bundle_hash end form = form_with_assets_and_bundle # TODO: targetBundles is only kept for backward compatibility with older node renderers # (protocol 2.0.0) that require it. The new node renderer derives target directories from # the bundle_<hash> form keys and ignores this field. Remove at the next breaking version. # Note: it's not mandatory to keep this until then — users are expected to upgrade the # node renderer and react_on_rails gem to the same version together — but it's an easy # backward compatibility safeguard. form["targetBundles"] = target_bundles perform_request("/upload-assets", form: form) end |