• Stars
    star
    160
  • Rank 234,703 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rack middleware for serving gzip files

Hey fellow Rails developers, please read!

Want to use rack-zippy with a Rails v4.2 or greater app?
Its recommended you don't! Rails 4.2+ now supports serving gzipped files directly so there's no need for rack-zippy in Rails 4.2+ apps.

Want to use rack-zippy with a Rails v4.1 or less app?
You'll need to use v3.0 of rack-zippy, see the README here: https://github.com/eliotsykes/rack-zippy/tree/v3.0.1

rack-zippy

rack-zippy v4+ is a Rack middleware for serving .gz files in Rack apps that are not Rails 4.2+ apps. (If you need to use rack-zippy in a Rails <= 4.1 app, then use v3.0 of rack-zippy, see README here: https://github.com/eliotsykes/rack-zippy/tree/v3.0.1)

rack-zippy has convenient directory request handling:

  • Requests for / and /index respond with public/index.html if present
  • Requests for /foo/ and /foo respond with first file present out of public/foo.html, public/foo/index.html (Same behaviour for subdirectories)

rack-zippy decorates actionpack's ActionDispatch::Static middleware for non-Rails Rack apps to provide rack-zippy's own choice of caching headers and whitelisting of permitted static file extensions. (As an alternative to rack-zippy, you can use actionpack's ActionDispatch::Static directly without rack-zippy.)

Installation in Rack app (that isnโ€™t a Rails app)

Add this line to your application's Gemfile:

gem 'rack-zippy'

And then execute:

$ bundle

In config.ru:

require 'rack-zippy'

# Set asset_root to an absolute or relative path to the directory holding your asset files
# e.g. '/path/to/my/apps/static-assets' or 'public'
asset_root = '/path/to/my/apps/public'
use Rack::Zippy::AssetServer, asset_root

Usage

Follow the installation instructions above and rack-zippy will serve any static assets, including gzipped assets, from your application's public/ directory and will respond with sensible caching headers.

Options

max_age_fallback

max_age_fallback, is an integer value in seconds that should be used as the max_age fallback for files served by rack-zippy that live outside the /assets subdirectory and aren't /favicon.ico.

A typical use for max_age_fallback is to define how long the cache lifetime for static HTML files served by rack-zippy should be. For one of my sites I have this set to 15 minutes:

max_age_in_secs = 15*60 # 15 mins = 900 secs
use Rack::Zippy::AssetServer, asset_root, max_age_fallback: max_age_in_secs

Any files given the max_age_fallback would have the following Cache-Control header:

Cache-Control: public, max-age=900

Configuration

Supported Extensions Whitelist

rack-zippy handles only files with whitelisted extensions. Default extensions are stored in the static_extensions array with an entry for each of these: css js html htm txt ico png jpg jpeg gif pdf svg zip gz eps psd ai woff woff2 ttf eot otf swf

You can modify this list to support other extensions by appending the lowercased file extension to the static_extensions array:

Rack::Zippy.configure do |config|
  # Add support for the given extensions:
  config.static_extensions.push('map', 'csv', 'xls', 'rtf', ...EXTENSIONS TO ADD...)
end

It is not recommended, however if you use rack-zippy 4.0+ with a Rails 4.2+ app, you can skip the rack-zippy rails version check and log output. Put the following in an initializer:

# config/initializers/zippy.rb
Rack::Zippy::Railtie.skip_version_check = true

Troubleshooting

NameError: uninitialized constant Rack::Zippy
  • Check Gemfile doesn't limit rack-zippy to a subset of environment groups
  • Run bundle install
  • Check Gemfile.lock contains an entry for rack-zippy
  • Ensure require 'rack-zippy' is present near the top of config.ru

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Run tests (rake test)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

Optional for contributors

To try a local branch of rack-zippy out as the gem dependency in a local app, configure bundler with a local gem override as follows:

In your-app/Gemfile: edit the rack-zippy dependency to the following:

# The branch your-local-branch-name **must** exist otherwise bundler will shout obscenities at you
gem 'rack-zippy', :github => 'eliotsykes/rack-zippy', :branch => 'your-local-branch-name'

At the command line, inside your-app, configure bundler to set a local git repo to override the one we specified in the previous step for rack-zippy:

$> bundle config --local local.rack-zippy /path/to/your/local/rack-zippy

Now when you run your-app with bundle exec, the rack-zippy gem dependency will resolve to /path/to/your/local/rack-zippy.

Cleanup time! When youโ€™re finished testing, delete the local override and set your Gemfile dependency back to the original:

# At the command line:
$> bundle config --delete local.rack-zippy

# In your-app/Gemfile change rack-zippy dependency to this (or similar):
gem 'rack-zippy', '~> 9.8.7' # Replace 9.8.7 with the rack-zippy release version you want to use.

How to Run a Single Test

# Single test file
ruby -Ilib:test test/assert_server_test.rb

# Single test method
ruby -Ilib:test test/assert_server_test.rb --name test_serves_static_file_as_directory

# Test methods matching a regex
ruby -Ilib:test test/assert_server_test.rb --name /serves_static/

Contributors

Releasing a new gem

  1. Update pre-release version to the release version in lib/rack-zippy/version.rb, e.g. 1.0.1.pre becomes 1.0.1
  2. Update CHANGELOG.md version and date. Update Contributors in README.md.
  3. Tests pass? (rake test)
  4. Commit and push changes to origin.
  5. Build the gem (rake build)
  6. Release on rubygems.org (rake release)
  7. Update version to the next pre-release version in lib/rack-zippy/version.rb, e.g. 1.0.1 becomes 1.0.2.pre.
  8. Add new heading to CHANGELOG for the next pre-release
  9. Commit and push the updated lib/rack-zippy/version.rb and CHANGELOG files.

More Repositories

1

rspec-rails-examples

RSpec cheatsheet & Rails app: Learn how to expertly test Rails apps from a model codebase
Ruby
2,150
star
2

real-world-rails

Real World Rails applications and their open source codebases for developers to learn from
Ruby
1,643
star
3

rails-security-checklist

๐Ÿ”‘ Community-driven Rails Security Checklist (see our GitHub Issues for the newest checks that aren't yet in the README)
Ruby
1,332
star
4

rails-testing-toolbox

๐Ÿ”ง Tools to help Rails developers test
Ruby
127
star
5

asset_fingerprint

Asset Fingerprint Plugin for Ruby on Rails - allows you to use md5 or timestamps in query string or in asset filenames as suggested by Google Page Speed
Ruby
99
star
6

awesome-challenges

Sharpen skills & prepare for interview. Includes puzzles, kata, algorithm & data structure exercises
37
star
7

monitorstxt

monitors.txt - lazy web app monitoring, see http://monitorstxt.org and http://monitorstxt.org/monitors.txt
31
star
8

spree-zoom-photos

Lightbox+Zoom product photos for your Spree store
JavaScript
27
star
9

page_cache

Holeless page caching plugin for Rails. Users never have to wait for cached pages to be generated (unlike the page caching that ships with Rails).
Ruby
26
star
10

rails-code-review

An evolving set of guidelines & supporting reasons to consider when code reviewing Ruby on Rails apps
21
star
11

rails-static-router

Enjoy static routes in your Rails config/routes.rb
Ruby
18
star
12

real-world-ember

Use the source: Read & learn from Real World Ember apps
17
star
13

rspec-katas

A friendly place for RSpec-driven Kata to gather and teach those wanting to learn the ways of RSpec.
Ruby
16
star
14

spree-single-product-urls

Extension for Spree to give each product a single URL to minimize duplicate content issues. Behaves like Amazon's "Look for similar items in these categories" product page section.
Ruby
13
star
15

spree-redirects

An extension for managing redirects within Spree
Ruby
10
star
16

spree-titles

Replaces the general Spree title with accurate titles.
Ruby
10
star
17

spree-db-tools

An extension to add a few useful Spree-related database rake tasks.
Ruby
9
star
18

less-css-jawr

Less CSS for JAWR
Java
7
star
19

spring-site-scope

Sample code to show how a site scope in Spring might work.
Java
6
star
20

6-minute-backups

Simple ruby script for securely backing up directories and mysql databases
Ruby
6
star
21

spree-i18n-db

Spree extension to override YAML translations with translations stored in the database.
Ruby
5
star
22

spree-products-page-remover

Extension for removing the /products page in Spree. Helps avoid duplicate content issues between home page and /products.
Ruby
5
star
23

acts_as_geocodable_extra

Some personal changes I needed to acts_as_geocodable - see the original project at the homepage url below
Ruby
4
star
24

rails-testing-cheatsheet

๐ŸšŒ Community-driven Rails Testing Cheatsheet. Contributions welcome!
4
star
25

rails-activerecord-aerobics

[UNMAINTAINED] Boost your Rails ActiveRecord Query core fitness with this workout plan. A+ abs or your money back.
Ruby
3
star
26

rails-console-cheatsheet

Rails console cheatsheet ๐Ÿ“ โ˜บ๏ธ
2
star
27

debugging-rails

๐Ÿ› Tips & links for debugging Ruby and Ruby on Rails
2
star
28

ruby-data-structures

Ruby Data Structures
2
star
29

gordonflash

Gordon Flash Grails plugin protects flash scope from being cleared out unnecessarily
Groovy
1
star
30

tools

Handy Script Collection (for Rubyists)
Shell
1
star
31

gz-compare

Compare compression file sizes for gzip with Zopfli
Ruby
1
star
32

asset_fingerprint_tester

Rails app exists purely for testing the Asset Fingerprint plugin - you probably don't want this
Ruby
1
star
33

gitbook-plugin-markrundown

Write runnable markdown documents, with superpowers for technical authors
JavaScript
1
star
34

rails-troubleshooter

Troubleshoot common Ruby on Rails issues
1
star