Class: Panda::Core::ModuleRegistry::JavaScriptMiddleware

Inherits:
Object
  • Object
show all
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

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
# 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
  file_path = find_javascript_file(relative_path)

  if file_path && File.file?(file_path)
    puts "[JavaScriptMiddleware] Serving: #{path} from #{file_path}"
    serve_file(file_path, env)
  else
    puts "[JavaScriptMiddleware] Not found: #{path} (tried #{relative_path})"
    @app.call(env)
  end
rescue => e
  # On error, log and pass to next middleware
  puts "[JavaScriptMiddleware] Error: #{e.message}"
  Rails.logger.error("[ModuleRegistry::JavaScriptMiddleware] Error: #{e.message}\n#{e.backtrace.join("\n")}") if defined?(Rails.logger)
  @app.call(env)
end