Class: Panda::Core::ModuleRegistry::JavaScriptMiddleware
- Inherits:
-
Object
- Object
- Panda::Core::ModuleRegistry::JavaScriptMiddleware
- Defined in:
- lib/panda/core/module_registry.rb
Overview
Custom Rack middleware to serve JavaScript modules from all registered Panda modules
This middleware checks all registered modules' app/javascript/panda directories and serves the first matching file. This solves the problem of multiple Rack::Static instances blocking each other.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ JavaScriptMiddleware
constructor
A new instance of JavaScriptMiddleware.
Constructor Details
#initialize(app) ⇒ JavaScriptMiddleware
Returns a new instance of JavaScriptMiddleware.
288 289 290 |
# File 'lib/panda/core/module_registry.rb', line 288 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/panda/core/module_registry.rb', line 292 def call(env) request = Rack::Request.new(env) path = request.path_info # Only handle /panda/core/* and /panda/cms/* style JavaScript module requests # Skip paths like /panda-core-assets/* (public assets handled by Rack::Static) return @app.call(env) unless path.start_with?("/panda/") # Strip /panda/ prefix to get relative path # e.g., "/panda/cms/application.js" -> "cms/application.js" relative_path = path.sub(%r{^/panda/}, "") return @app.call(env) if relative_path.empty? # Try to find the file in registered modules if ENV["RSPEC_DEBUG"] puts "[JavaScriptMiddleware] Looking for: #{path} (relative: #{relative_path})" end file_path = find_javascript_file(relative_path) if file_path && File.file?(file_path) puts "[JavaScriptMiddleware] ✅ Serving: #{path} from #{file_path}" if ENV["RSPEC_DEBUG"] serve_file(file_path, env) else if ENV["RSPEC_DEBUG"] puts "[JavaScriptMiddleware] ❌ Not found: #{path}" puts "[JavaScriptMiddleware] Searched relative path: #{relative_path}" puts "[JavaScriptMiddleware] Checked locations:" ModuleRegistry.modules.each do |gem_name, info| if ModuleRegistry.send(:engine_available?, info[:engine]) root = ModuleRegistry.send(:engine_root, info[:engine]) if root puts "[JavaScriptMiddleware] - #{root.join("app/javascript/panda", relative_path)}" puts "[JavaScriptMiddleware] - #{root.join("public/panda", relative_path)}" end end end if defined?(Rails.root) puts "[JavaScriptMiddleware] - #{Rails.root.join("app/javascript/panda", relative_path)}" puts "[JavaScriptMiddleware] - #{Rails.root.join("public/panda", relative_path)}" end end @app.call(env) end rescue => e # On error, log and pass to next middleware puts "[JavaScriptMiddleware] Error: #{e.}" if ENV["RSPEC_DEBUG"] Rails.logger.error("[ModuleRegistry::JavaScriptMiddleware] Error: #{e.}\n#{e.backtrace.join("\n")}") if defined?(Rails.logger) @app.call(env) end |