Sha256: 71ee9ed38dbd337da9ce1f044f9bf9a701905d7cbc0d12828733a56cd077ffff

Contents?: true

Size: 1.32 KB

Versions: 15

Compression:

Stored size: 1.32 KB

Contents

# https://github.com/discourse/discourse/blob/master/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)
      root = "/assets/"
      is_asset = env['REQUEST_PATH'] && env['REQUEST_PATH'].starts_with?(root)

      # 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 = env['REQUEST_PATH'][(root.length)..-1]
        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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
o2webappizer-0.2.6 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.2.5 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.2.4 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.2.3 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.2.2 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.2.1 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.2.0 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.1.15 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.1.14 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.1.13 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.1.12 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.1.11 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.1.10 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.1.9 templates/lib/middleware/turbo_dev.rb
o2webappizer-0.1.8 templates/lib/middleware/turbo_dev.rb