aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--config/environments/development.rb9
-rw-r--r--config/environments/production.rb8
-rw-r--r--config/middlewares/cachesettings.rb20
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