Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No such middleware to insert before: ActionDispatch::Static (RuntimeError) on production #1101

Closed
aeberlin opened this issue Jan 4, 2016 · 13 comments

Comments

@aeberlin
Copy link

aeberlin commented Jan 4, 2016

In production, with config.serve_static_assets = false:

$ be rescue rails s -e production
=> Booting Thin
=> Rails 4.2.5 application starting in production on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/foo/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/stack.rb:125:in `assert_index': No such middleware to insert before: ActionDispatch::Static (RuntimeError)
    from /Users/foo/.gem/ruby/2.2.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/stack.rb:88:in `insert'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/configuration.rb:68:in `block in merge_into'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/configuration.rb:67:in `each'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/configuration.rb:67:in `merge_into'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/engine.rb:501:in `app'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/application/finisher.rb:34:in `block in <module:Finisher>'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/foo/.rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
    from /Users/foo/.rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /Users/foo/.rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
    from /Users/foo/.rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /Users/foo/.rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:345:in `each'
    from /Users/foo/.rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:345:in `call'
    from /Users/foo/.rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
    from /Users/foo/.rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
    from /Users/foo/.rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
    from /Users/foo/Projects/my_app/config/environment.rb:5:in `<top (required)>'
    from /Users/foo/.gem/ruby/2.2.4/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /Users/foo/.gem/ruby/2.2.4/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/foo/.gem/ruby/2.2.4/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/foo/.gem/ruby/2.2.4/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /Users/foo/Projects/my_app/config.ru:3:in `block in <main>'
    from /Users/foo/.gem/ruby/2.2.4/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/foo/.gem/ruby/2.2.4/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
    from /Users/foo/Projects/my_app/config.ru:in `new'
    from /Users/foo/Projects/my_app/config.ru:in `<main>'
    from /Users/foo/.gem/ruby/2.2.4/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
    from /Users/foo/.gem/ruby/2.2.4/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/foo/.gem/ruby/2.2.4/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
    from /Users/foo/.gem/ruby/2.2.4/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /Users/foo/.gem/ruby/2.2.4/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/server.rb:61:in `app'
    from /Users/foo/.gem/ruby/2.2.4/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
    from /Users/foo/.gem/ruby/2.2.4/gems/rack-1.6.4/lib/rack/server.rb:272:in `start'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/server.rb:80:in `start'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/foo/.gem/ruby/2.2.4/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:10:in `require'
    from bin/rails:10:in `<main>'

It seems, based on the documentation here, that ActionDispatch::Static is only inserted when serve_static_assets is true.

Is this expected behavior from locomotive's perspective, that this should be enabled even in production? If so, how does one handle assets being served from alternative nodes? Either way, probably should be mentioned in documentation somewhere, since it took me a bit to track down and I'm rather proficient.

@adam-pl
Copy link

adam-pl commented Jan 4, 2016

I had the same problem.

Just change:

config.serve_static_assets = false

into:

config.serve_static_files = false

If it wont work show me content of the config/environments/production.rb file

@aeberlin
Copy link
Author

aeberlin commented Jan 4, 2016

Yeah, that was the same conclusion I drew. I think there should be a note somewhere in the documentation on this point.

@adam-pl
Copy link

adam-pl commented Jan 4, 2016

Well, the Engine v3 is still beta version.

I think it could be great to add info about executing bundle exec rails generate locomotive:install if something is wrong in the production mode - Install Locomotive Engine · Locomotive V3.

I'm not sure if it generates correct production.rb file but see heroku-instant-deploy/config/environments/production.rb
file. It has defined config.serve_static_files option we are talking about.

@did
Copy link
Member

did commented Jan 5, 2016

@adam-pl and @aeberlin I'm confused. Where does config.serve_static_assets = false come from?

@aeberlin
Copy link
Author

aeberlin commented Jan 5, 2016

Hey @did,

In current RAILS, config/production.rb defaults to:

config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

The middleware Locomotive::Middlewares::ImageThumbnail is inserted before ActionDispatch::Static. When config.serve_static_files is false, ActionDispatch::Static will not be present in the middleware chain, and will raise an exception on this statement:

lib/locomotive/engine.rb#L68

65    initializer 'locomotive.middlewares' do |app|
66      require 'locomotive/middlewares'
67
68->    app.middleware.insert_before ActionDispatch::Static, '::Locomotive::Middlewares::ImageThumbnail'
69      app.middleware.use '::Locomotive::Middlewares::Site'
70    end

Hope this helps.

@did
Copy link
Member

did commented Jan 5, 2016

@aeberlin I see, it makes totally sense now.

@did
Copy link
Member

did commented Jan 5, 2016

thanks guys

@did did closed this as completed Jan 5, 2016
@aeberlin
Copy link
Author

aeberlin commented Jan 5, 2016

^_^

@aeberlin
Copy link
Author

aeberlin commented Jan 5, 2016

Also, @adam-pl, serve_static_assets should be an alias for serve_static_files, until RAILS 5.0. Not sure why that helped you.

Anyway, cheers.

@adam-pl
Copy link

adam-pl commented Jan 5, 2016

@aeberlin, I'm using config.serve_static_files = true on the production and it works fine.
Actually you're write about this alias.

Anyway I prefere using config.serve_static_files to config.serve_static_assets because of warning:

DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly.

I do not like any warning that occurs while deployment is in progress (got it with using the Capistrano & ruby 2.2.1).

@aeberlin
Copy link
Author

aeberlin commented Jan 5, 2016

Ah, I see. You said false in both lines in your previous comment, now I see that was a typo.

Cheers

@adam-pl
Copy link

adam-pl commented Jan 5, 2016

Yeah :)

Cheers

@akostadinov
Copy link

FYI there is one approach font_assets gem used ericallam/font_assets#53
I'm not sure how good this approach is though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants