How to enable compression in Ruby on Rails?

后端 未结 3 596
眼角桃花
眼角桃花 2021-02-05 06:29

I posted a similar question here

Serving Compressed Assets in Heroku with Rack-Zippy

but decided to give up on that service, since I couldn\'t get it to work.

相关标签:
3条回答
  • 2021-02-05 06:48

    As per the author of Rack::Deflater it should be placed after ActionDispatch::Static in a Rails app. The reasoning is that if your app is also serving static assets (like on Heroku, for example), when assets are served from disk they are already compressed. Inserting it before would only end up in Rack::Deflater attempting to re-compress those assets. Therefore as a performance optimisation:

    # application.rb
    
    config.middleware.insert_after ActionDispatch::Static, Rack::Deflater
    
    
    0 讨论(0)
  • 2021-02-05 06:54

    Rack::Deflater should work if you use insert_before (instead of "use"), to place it near the top of the middleware stack, prior to any other middleware that might send a response. .use places it at the bottom of the stack. On my machine the topmost middleware is Rack::Sendfile. So I would use:

    config.middleware.insert_before(Rack::Sendfile, Rack::Deflater)
    

    You can get the list of middleware in order of loading by doing rake middleware from the command line.

    Note: A good link for insert_before vs Use in middleware rack

    0 讨论(0)
  • 2021-02-05 07:03

    Enable compression

    Add it to config/application.rb:

    module YourApp
      class Application < Rails::Application
        config.middleware.use Rack::Deflater
      end
    end
    

    Source: http://robots.thoughtbot.com/content-compression-with-rack-deflater

    0 讨论(0)
提交回复
热议问题