From 667d3a3fd65bb1fb8f0e14f11d27e0625cf60588 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 22 Feb 2019 11:21:07 +1100 Subject: [PATCH] PERF: include content-length header for CDN Attempt to force NGINX to include content length when doing X-SendFile This does not seem to be required when bypassing NGINX. Without this header some CDNs may have issues caching --- app/controllers/theme_javascripts_controller.rb | 2 ++ spec/requests/theme_javascripts_controller_spec.rb | 1 + 2 files changed, 3 insertions(+) diff --git a/app/controllers/theme_javascripts_controller.rb b/app/controllers/theme_javascripts_controller.rb index bbee8f480aa..cfa48dca0c9 100644 --- a/app/controllers/theme_javascripts_controller.rb +++ b/app/controllers/theme_javascripts_controller.rb @@ -28,6 +28,8 @@ class ThemeJavascriptsController < ApplicationController File.write(cache_file, content) end + # this is only required for NGINX X-SendFile it seems + response.headers["Content-Length"] = File.size(cache_file).to_s set_cache_control_headers send_file(cache_file, disposition: :inline) end diff --git a/spec/requests/theme_javascripts_controller_spec.rb b/spec/requests/theme_javascripts_controller_spec.rb index fd186ffc176..ac9475c1664 100644 --- a/spec/requests/theme_javascripts_controller_spec.rb +++ b/spec/requests/theme_javascripts_controller_spec.rb @@ -40,6 +40,7 @@ describe ThemeJavascriptsController do get "/theme-javascripts/#{javascript_cache.digest}.js" expect(response.status).to eq(200) expect(response.body).to eq(javascript_cache.content) + expect(response.headers['Content-Length']).to eq(javascript_cache.content.bytesize.to_s) javascript_cache.destroy!