ActionDispatch::Static Error
Alright folks,
I don't know what I'm doing wrong. Its got to be something stupid, but I can't figure it out. Here's what I've got:
Rails 3.2.13 Font Assets 0.1.10
In my production.rb, I have config.serve_static_assets = false. When I deploy my app to the production server with capistrano it gets to the precompile assets stage and it fails because it cannot insert Middleware before ActionDispatch::Static. Now I saw the issues that have been logged and the fixes that have been made. I looked at the code and I see this:
insert_target = if defined?(ActionDispatch::Static)
'ActionDispatch::Static'
else
'Rack::Runtime'
end
in the lib/font_assets/railtie.rb file so it should be doing the insert before 'Rack::Runtime'. So I figured maybe that was somehow busted. I commented out that whole if/else section and I hardcoded insert_target = 'Rack::Runtime'. This time the deploy worked without throwing an error. Perfect! But no.
I went onto my production server to make sure everything was good, and I ran: rake middleware. The FontAssets::Middleware module was not in the list and when I try to start the server I get an error that complains about my config.font_assets.origin parameter in production.rb. It says it doesn't know what font_assets is.
So fine, I concede defeat. I went back to production.rb and turned my config.serve_static_assets = true. I reverted the font_assets gem back to the original. And I ran the deployment again. This time no errors, perfect. But again, when I run the rake middleware command on the production server, its not there.
I'm out of ideas. This shouldn't be that difficult. All my other custom middleware components are installed just fine. If I set config.serve_static_assets = false and remove font_assets from my Gemfile and re-deploy everything works just fine. So I'm certain that its this gem that is the issue, but I have no idea why.
Any advice??
So I finally got this thing working, but unfortunately, I had to rewrite a good portion of it. The primary changes were the switch from using Railties to invoke the middleware to using a Rails Engine.
lib/font_assets/engine.rb
module FontAssets
class Engine < ::Rails::Engine
config.app_middleware.insert_before 'Rack::Runtime', FontAssets::Middleware
end
end
The second major change that I had to make was to stop using production.rb config options. I created a new class and started using that instead.
lib/font_assets/config.rb
module FontAssets
module Config
class << self
attr_accessor :origin
attr_accessor :options
end
self.origin = "*"
self.options = { allow_ssl: true }
end
end
This change required a minor adjustment to the initialize method inside of
lib/font_assets/middleware.rb
def initialize(app)
@app = app
@origin = FontAssets::Config.origin
@options = FontAssets::Config.options
@mime_types = FontAssets::MimeTypes.new(Rack::Mime::MIME_TYPES)
end
So now to set the config options, I created a new file
config/initializers/font_assets.rb
FontAssets::Config.origin = "https://www.mywebsite.com"
FontAssets::Config.options = # whatever you want here
Once I made these adjustments, the middleware started loading properly and executing as expected. Still not sure what the root problem was, but this seems to have done the trick. I'll leave it to you to decide what you want to do with this bit of information.
Are you submitting a pull request for this? Is this still working for you? I ran into the same problem.
I didn't submit a pull request. Ultimately, I ended up scrapping this gem all together and going to public cdn's. I'm pulling my gem files from font awesome's cdn and google font's cdn. Even with the fix I made, it was behaving very flaky and would work sometimes on some OS's and Browsers and fail on others and it seemed to change with every new deployment I did to the production server. So i finally just gave up. The public cdn's have the headers set properly and they serve up my fonts without issue.
+1 for the issue.
+1 for the issue. I ended up scrapping this gem in favor of a public CDN as well as this issue is over a year old.
:+1:
If you have config.serve_static_assets = false then ActionDispatch::Static is never loaded by rails, therefore you can't insert before it - it doesn't exist. Which makes sense right? You said you don't need to serve static assets so it's cutting out the middleware responsible for it.