2013-04-18 02:32:48 -04:00
|
|
|
module Middleware
|
2013-04-18 02:46:04 -04:00
|
|
|
# this class cheats and bypasses rails altogether if the client attempts
|
|
|
|
# to download a static asset
|
2013-04-18 02:32:48 -04:00
|
|
|
class TurboDev
|
|
|
|
def initialize(app, settings={})
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
def call(env)
|
2013-09-09 15:35:13 -04:00
|
|
|
|
|
|
|
is_asset = (env['REQUEST_PATH'] =~ /^\/assets\//)
|
|
|
|
|
2013-04-18 02:32:48 -04:00
|
|
|
# hack to bypass all middleware if serving assets, a lot faster 4.5 seconds -> 1.5 seconds
|
2013-09-09 15:35:13 -04:00
|
|
|
if (etag = env['HTTP_IF_NONE_MATCH']) && is_asset
|
2013-04-18 02:32:48 -04:00
|
|
|
name = $'
|
|
|
|
etag = etag.gsub "\"", ""
|
|
|
|
asset = Rails.application.assets.find_asset(name)
|
|
|
|
if asset && asset.digest == etag
|
|
|
|
return [304,{},[]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-09 15:35:13 -04:00
|
|
|
status, headers, response = @app.call(env)
|
|
|
|
headers['Cache-Control'] = 'no-cache' if is_asset
|
|
|
|
[status, headers, response]
|
2013-04-18 02:32:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|