From fb8655769e31d2d2e30a1f62641b9436279346bd Mon Sep 17 00:00:00 2001 From: Martin Charles Date: Thu, 15 May 2014 09:50:54 -0400 Subject: [PATCH] Fixed Caching and Cleaned up Old Caching Caching was broken for many reasons: * Puma calls `Travis::Web::App.initilize` multiple times. This caused the server start time to be incorrect, breaking caching. * The `Date` HTTP header was missing. This caused some browsers to fail to cache assets. * The `ETag` was incorrectly formatted. Some other things which were changed: * Removed `Rack::Cache` --- Gemfile | 1 - Gemfile.lock | 3 --- config.ru | 3 ++- lib/travis/web/app.rb | 27 +++++++++++++-------------- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/Gemfile b/Gemfile index 4110a159..97e08ac6 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,6 @@ ruby '2.1.2' gem 'puma' gem 'rack-ssl', '~> 1.3' gem 'rack-protection', '~> 1.3' -gem 'rack-cache' gem 'rack-mobile-detect' gem 'sinatra' diff --git a/Gemfile.lock b/Gemfile.lock index 0ee3859c..ca61857b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,8 +60,6 @@ GEM puma (2.6.0) rack (>= 1.1, < 2.0) rack (1.5.2) - rack-cache (1.2) - rack (>= 0.4) rack-mobile-detect (0.4.0) rack rack-protection (1.3.2) @@ -114,7 +112,6 @@ DEPENDENCIES guard libv8 (~> 3.16.0) puma - rack-cache rack-mobile-detect rack-protection (~> 1.3) rack-ssl (~> 1.3) diff --git a/config.ru b/config.ru index b9a55f43..fff7ffb2 100644 --- a/config.ru +++ b/config.ru @@ -30,5 +30,6 @@ run Travis::Web::App.build( api_endpoint: ENV['API_ENDPOINT'], pusher_key: ENV['PUSHER_KEY'], ga_code: ENV['GA_CODE'], - root: File.expand_path('../public', __FILE__) + root: File.expand_path('../public', __FILE__), + server_start: Time.now ) diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index d459eee3..b6cc3a7d 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -1,6 +1,5 @@ require 'rack' require 'rack/ssl' -require 'rack/cache' require 'rack/protection' require 'delegate' require 'time' @@ -35,7 +34,6 @@ class Travis::Web::App builder = Rack::Builder.new if options[:environment] == 'production' builder.use Rack::SSL - # builder.use Rack::Cache end builder.use Rack::Deflater builder.use Rack::Head @@ -49,21 +47,23 @@ class Travis::Web::App end end - attr_reader :routers, :version, :last_modified, :age, :options, :root + attr_reader :routers, :version, :age, :options, :root, :server_start def initialize(options = {}) - @options = options - @root = options.fetch(:root) - @version = File.read File.expand_path('version', root) - @last_modified = Time.now - @age = 60 * 60 * 24 * 365 - @routers = { default: create_router } + @options = options + @server_start = options.fetch(:server_start) + @root = options.fetch(:root) + @version = File.read File.expand_path('version', root) + @age = 60 * 60 * 24 * 365 + @routers = { default: create_router } end def call(env) name = env['travis.alt'] || :default routers[name] ||= create_router(alt: name) - routers[name].call(env) + route = routers[name].call(env) + route[1]["Date"] = Time.now.httpdate + route end private @@ -84,13 +84,12 @@ class Travis::Web::App set_config(content, options) if config_needed?(file) headers = { 'Content-Length' => content.bytesize.to_s, - 'Content-Location' => path_for(file), 'Cache-Control' => cache_control(file), 'Content-Location' => path_for(file), 'Content-Type' => mime_type(file), - 'ETag' => version, - 'Last-Modified' => last_modified.httpdate, - 'Expires' => (last_modified + age).httpdate, + 'ETag' => %Q{"#{version}"}, + 'Last-Modified' => server_start.httpdate, + 'Expires' => (server_start + age).httpdate, 'Vary' => vary_for(file) } [ 200, headers, [content] ]