Class: RubyReactor::Web::Application

Inherits:
Roda
  • Object
show all
Defined in:
lib/ruby_reactor/web/application.rb

Instance Method Summary collapse

Instance Method Details

#serve_index(r) ⇒ Object

rubocop:disable Naming/MethodParameterName



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_reactor/web/application.rb', line 13

def serve_index(r)
  # rubocop:enable Naming/MethodParameterName
  content = File.read(File.expand_path("public/index.html", __dir__))
  # Inject the base path for React Router
  # request.script_name contains the mount path (e.g. "/ruby_reactor")
  base_path = r.script_name.empty? ? "/" : r.script_name

  # Ensure trailing slash for base href to support relative assets (base: './')
  href = base_path.end_with?("/") ? base_path : "#{base_path}/"

  # Inject config and base tag
  content.sub!("<head>", "<head><base href='#{href}'><script>window.RUBY_REACTOR_BASE = '#{base_path}';</script>")
  content
rescue Errno::ENOENT
  "UI not built. Please run `rake build:ui`"
end