Created
July 3, 2013 13:23
-
-
Save ugisozols/5917814 to your computer and use it in GitHub Desktop.
Middleware which speeds up Rails dev for apps with lots of assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# turbo dev by Sam Saffron | |
# http://discuss.emberjs.com/t/what-are-people-doing-about-the-gazzillion-file-problem/1006/8 | |
module Middleware | |
# this class cheats and bypasses rails altogether if the client attempts | |
# to download a static asset | |
class TurboDev | |
def initialize(app, settings={}) | |
@app = app | |
end | |
def call(env) | |
# hack to bypass all middleware if serving assets, a lot faster 4.5 seconds -> 1.5 seconds | |
if (etag = env['HTTP_IF_NONE_MATCH']) && env['REQUEST_PATH'] =~ /^\/assets\// | |
name = $' | |
etag = etag.gsub "\"", "" | |
asset = Rails.application.assets.find_asset(name) | |
if asset && asset.digest == etag | |
return [304,{},[]] | |
end | |
end | |
@app.call(env) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment