Search code examples
ruby-on-railsrails-enginesrack-middleware

Assets not working in isolated engine endpoint


I'm getting Not Found errors when trying to access assets inside an isolated engine mounted as an endpoint in a middleware. I'm trying to access the assets from within the engine, so they should be able to be found.

My suspicion is that asset routing isn't working because of the way I'm routing requests on a certain domain to the endpoint:

require 'addressable/uri'

class AdminRouter

  def initialize(app)
    @app = app
  end

  def call(env)
    request = ActionDispatch::Request.new(env)

    # Allow requests to the admin system through without going any further
    if request.host == Rails.application.config.admin_address
      Admin::Engine.call(env)
    else
      @app.call(env)
    end
  end

end

I'm doing it this way because I don't want the admin application routes accessible from the main app and vice versa. It's working well, just not the assets.


Solution

  • Looks like there is no assets middleware as such. action_pack loops over each asset and prepends a route in the main router pointing to a rack endpoint:

    /gems/actionpack-3.2.22.2/lib/sprockets/bootstrap.rb 
    
    app.routes.prepend do
      mount app.assets => config.assets.prefix
    end
    
    /gems/sprockets-2.2.3/lib/sprockets/server.rb
    

    that in turn serves the asset among other things.

    So the reason my Admin::Engine doesn't have access to the routes is because they are never added when the app boots up.

    My workaround was to use asset_host to specify a special hostname for the assets server. I then route requests to this host to the main app instead of the admin engine.