Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Engine's static assets taking priority over applications

#6388

When serve_static_assets = true, an engine's static assets are taking priority over an application's. This is happening on Edge Rails. I created a barebones rails app with a barebones engine and config.static_asset_paths--when loading the 'ActionDispatch::Static' middleware--is:

1.9.2:

{"/engine_app_engine"=>"/code/sandbox/engine_app/public", "/"=>"/code/sandbox/parent_app/public"}

1.8.7:

#<OrderedHash {"/"=>"/code/sandbox/parent_app/public", "/engine_app_engine"=>"/code/sandbox/engine_app/public"}>

But both result in the same FileHandlers being created in ActionDispatch::Static:

[#<ActionDispatch::FileHandler:0x1021a6960 @file_server=#<Rack::File:0x1021a66e0 @root="/code/sandbox/engine_app/public">, @at="/engine_app_engine", @compiled_root=/^\/code\/sandbox\/engine_app\/public/, @root="/code/sandbox/engine_app/public", @compiled_at=/^\/engine_app_engine/>, #<ActionDispatch::FileHandler:0x1021a66b8 @file_server=#<Rack::File:0x1021a6500 @root="/code/sandbox/parent_app/public">, @at="", @compiled_root=/^\/code\/sandbox\/parent_app\/public/, @root="/code/sandbox/parent_app/public", @compiled_at=nil>]

The engine is:

module EngineApp
  class Engine < Rails::Engine
  end
end

So when ActionDispatch::Static is getting called, it first looks in the engine's public directory, and then if not found, the applications public directory. It seems like this has something to do with initialization order. Here is where those static_asset_paths are getting set:

# in railties/lib/rails/engine.rb
  initializer :append_asset_paths do
    config.asset_path ||= default_asset_path

    public_path = paths["public"].first
    if config.compiled_asset_path && File.exist?(public_path)
      config.static_asset_paths[config.compiled_asset_path] = public_path
    end
  end

So since static_asset_paths is using an OrderedHash, this initializer must be getting called on the engine, then the application (since the application is just also an engine).

Tested on 1.9.2-p136, 1.8.7-p302

Reported by rubymaverick · February 8th, 2011 @ 05:09 AM

State: resolved
Milestone: 3.1
Assigned to: Piotr Sarnacki Piotr Sarnacki
Importance: High

Activity

  1. rubymaverick
    rubymaverick

    I created repo's on GitHub for the Parent App (https://github.com/rubymaverick/Parent-App) and the Engine (https://github.com/rubymaverick/Engine-App) so anyone can replicate this. Just pull down both repos into the same directory, use bundler to resolve the parent apps dependencies, fire up the server, and go to http://localhost:3000/engine-app.

    The file parent_app/public/engine_app_engine/stylesheets/style.css does not get served, instead engine_app/public/stylesheets/style.css does.

    February 8th, 2011 @ 03:31 PM

  2. rubymaverick
    rubymaverick

    Here is a patch with a failing test proving the bug

    February 8th, 2011 @ 04:17 PM

  3. rubymaverick
    rubymaverick

    Another failing test. Proves that engines initializers are loaded before the applications, which I think is the cause of the problem?

    February 8th, 2011 @ 05:11 PM

  4. José Valim
    José Valim
    • State changed from new to open
    • Milestone set to 3.1
    • Assigned user set to Piotr Sarnacki
    • Importance changed from to High

    Piotr, could you please take a look at this?

    February 8th, 2011 @ 09:57 PM

  5. Piotr Sarnacki
    Piotr Sarnacki

    @rubymaverick: the loading of initializers is ok, they should load before application's ones, I just haven't noticed that with such behavior assets from engine will have higher priority.

    February 9th, 2011 @ 11:05 AM

  6. Repository
    Repository
    • State changed from open to resolved

    (from [e6369bc9e97d0f1e5583725cd9f684bbe4fca3e1]) Application's assets should have higher priority than engine's ones [#6388 state:resolved] https://github.com/rails/rails/commit/e6369bc9e97d0f1e5583725cd9f68...

    February 9th, 2011 @ 11:20 AM

  7. Piotr Sarnacki