rack-pagespeed: Rack middleware for page speed optimization
Thanks to work by Google and Yahoo, we’re all better informed about how to speed up our web pages. For those on Apache, Google has made it easier to implement these ideas at the Apache Module level. For those running on Ruby web frameworks, Julio Cesar offers up the same goodness as Rack middleware.
Rack Pagespeed offers some HTML output filters to help you implement page optimization best practices with minimal effort. To get started, install the gem:
gem install rack-pagespeed
For Sinatra, Rack, or Padrino apps, configure Rack Pagespeed in your config.ru
Rackup file:
require 'rack/pagespeed'
require 'myapp'
use Rack::PageSpeed, :public => "/app/public/dir" do
store :disk => Dir.tmpdir # require 'tmpdir'
inline_javascript :max_size => 4000
inline_css
combine_javascripts
end
run Sinatra::Application
For those Rails, create a rack_pagespeed.rb
initializer with:
require 'rack/pagespeed' # somewhere
class Application < Rails::Application
config.middleware.use Rack::PageSpeed, :public => Rails.public_path do
store :disk => Dir.tmpdir # require 'tmpdir'
inline_javascript :max_size => 4000
inline_css
combine_javascripts
end
# ...
Filters
Out of the box, Rack Pagespeed supports filters to:
- Inline JavaScript under 2kb
- Inline CSS under 2kb
- Bundle JavaScript files
- Minify JavaScript files
- Bundle CSS files
- Inline images using
data-uri
You can even roll your own filters.
Storage
Rack Pagespeed currently supports two storage options: disk and memcached. See the well designed docs for advanced options.
Discussion
Sign in or Join to comment or subscribe