Class: TurboDevAssets

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_dev_assets.rb,
lib/turbo_dev_assets/version.rb

Overview

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

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(app, settings = {}) ⇒ TurboDevAssets

Returns a new instance of TurboDevAssets.



15
16
17
# File 'lib/turbo_dev_assets.rb', line 15

def initialize(app, settings={})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/turbo_dev_assets.rb', line 19

def call(env)
  is_asset = (env['REQUEST_PATH'] =~ /^#{Rails.application.config.assets.prefix}\//)

  # 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