Created
August 27, 2014 15:53
-
-
Save erkattak/0a16ddbb3506c2b4574d to your computer and use it in GitHub Desktop.
TurboDev - Speed up Rails development environment's asset serving
This file contains hidden or 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
# from https://github.com/discourse/discourse/blob/21e08a423e8839c6bacb4ee688b924a4fb71113f/lib/middleware/turbo_dev.rb | |
# lib/middleware/turbo_dev.rb | |
module Middleware | |
# Cheat and bypass Rails in development mode if the client attempts to download a static asset | |
# that's already been downloaded. | |
# | |
# Also ensures that assets are not cached in development mode. Around Chrome 29, the behavior | |
# of `must-revalidate` changed and would often not request assets that had changed. | |
# | |
# To use, include in your project and add the following to development.rb: | |
# | |
# require 'middleware/turbo_dev' | |
# config.middleware.insert 0, Middleware::TurboDev | |
# | |
class TurboDev | |
def initialize(app, settings={}) | |
@app = app | |
end | |
def call(env) | |
is_asset = (env['REQUEST_PATH'] =~ /^\/assets\//) | |
# hack to bypass all middleware if serving assets, a lot faster 4.5 seconds -> 1.5 seconds | |
if (etag = env['HTTP_IF_NONE_MATCH']) && is_asset | |
name = $' | |
etag = etag.gsub "\"", "" | |
asset = Rails.application.assets.find_asset(name) | |
if asset && asset.digest == etag | |
return [304,{},[]] | |
end | |
end | |
status, headers, response = @app.call(env) | |
headers['Cache-Control'] = 'no-cache' if is_asset | |
[status, headers, response] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment