diff options
| author | Luc Donnet | 2018-03-12 15:11:41 +0100 | 
|---|---|---|
| committer | GitHub | 2018-03-12 15:11:41 +0100 | 
| commit | faa6c9b8a4262fb2c5212fd3b971337881bb242b (patch) | |
| tree | 466cb4fd4bea3bd9aecb15a017b2ee27dfe32524 | |
| parent | 10765f6378777d74b121faf9e6f2a17a9fb1594e (diff) | |
| parent | ac9495472164136dd4604f89c7bebaee3cecdd0c (diff) | |
| download | chouette-core-faa6c9b8a4262fb2c5212fd3b971337881bb242b.tar.bz2 | |
Merge pull request #344 from af83/5896-add-cache-headers
5896 Add cache-related headers for assets
| -rw-r--r-- | config/environments/development.rb | 9 | ||||
| -rw-r--r-- | config/environments/production.rb | 8 | ||||
| -rw-r--r-- | config/middlewares/cachesettings.rb | 20 | 
3 files changed, 37 insertions, 0 deletions
| diff --git a/config/environments/development.rb b/config/environments/development.rb index 446e72190..7bd049979 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,3 +1,5 @@ +require Rails.root + 'config/middlewares/cachesettings' +  Rails.application.configure do    # Settings specified here will take precedence over those in config/application.rb. @@ -96,6 +98,13 @@ Rails.application.configure do    config.middleware.insert_after(ActionDispatch::Static, Rack::LiveReload) if ENV['LIVERELOAD']    config.middleware.use I18n::JS::Middleware +  config.middleware.use CacheSettings, { +    /\/assets\/.*/ => { +      cache_control: "max-age=86400, public", +      expires: 86400 +    } +  } +    config.development_toolbar = false    if ENV['TOOLBAR'] && File.exists?("config/development_toolbar.rb")      config.development_toolbar = OpenStruct.new diff --git a/config/environments/production.rb b/config/environments/production.rb index 9a699eb44..eb44e1ab1 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -1,3 +1,5 @@ +require Rails.root + 'config/middlewares/cachesettings' +  Rails.application.configure do    # Settings specified here will take precedence over those in config/application.rb. @@ -136,6 +138,12 @@ Rails.application.configure do    config.iev_url = ENV.fetch('IEV_URL',"http://iev:8080")    config.rails_host = ENV.fetch('RAILS_HOST','http://front:3000') +  config.middleware.use CacheSettings, { +    /\/assets\/.*/ => { +      cache_control: "max-age=#{1.year.to_i}, public", +      expires: 1.year.to_i +    } +  }    # Set node env for browserify-rails    # config.browserify_rails.node_env = "production"  end diff --git a/config/middlewares/cachesettings.rb b/config/middlewares/cachesettings.rb new file mode 100644 index 000000000..8a122891f --- /dev/null +++ b/config/middlewares/cachesettings.rb @@ -0,0 +1,20 @@ +class CacheSettings +  def initialize app, pat +    @app = app +    @pat = pat +  end + +  def call env +    res = @app.call(env) +    path = env["REQUEST_PATH"] +    @pat.each do |pattern,data| +      if path =~ pattern +        res[1]["Cache-Control"] = data[:cache_control] if data.has_key?(:cache_control) +        res[1]["Expires"] = (Time.now + data[:expires]).utc.rfc2822 if data.has_key?(:expires) +        res[1]["X-Custom-Cache-Control"] = "yes" if Rails.env.development? +        return res +      end +    end +    res +  end +end | 
