From 98f4a2adcbb19e39e8f698b7f2d2f92ef457597e Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 15 Dec 2016 16:05:20 +1100 Subject: [PATCH] FIX: on 404 from brotli asset path return a correctly encoded doc old implementation would cache the 404 for 1 year with incorrect encoding hilarity would ensue --- app/controllers/static_controller.rb | 20 +++++++++++--------- spec/controllers/static_controller_spec.rb | 9 +++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index 1641c019565..6fa864c2683 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -135,23 +135,25 @@ class StaticController < ApplicationController opts = { disposition: nil } opts[:type] = "application/javascript" if path =~ /\.js.br$/ - response.headers["Expires"] = 1.year.from_now.httpdate - response.headers["Cache-Control"] = 'max-age=31557600, public' - response.headers["Content-Encoding"] = 'br' begin response.headers["Last-Modified"] = File.ctime(path).httpdate response.headers["Content-Length"] = File.size(path).to_s rescue Errno::ENOENT - raise Discourse::NotFound + response.headers["Expires"] = 5.seconds.from_now.httpdate + response.headers["Cache-Control"] = 'max-age=5, public' + expires_in 5.seconds, public: true, must_revalidate: false + + render text: "missing brotli asset", status: 404 + return end + response.headers["Expires"] = 1.year.from_now.httpdate + response.headers["Cache-Control"] = 'max-age=31557600, public' + response.headers["Content-Encoding"] = 'br' + expires_in 1.year, public: true, must_revalidate: false - if File.exists?(path) - send_file(path, opts) - else - raise Discourse::NotFound - end + send_file(path, opts) end diff --git a/spec/controllers/static_controller_spec.rb b/spec/controllers/static_controller_spec.rb index f8ba6629848..732f94255a8 100644 --- a/spec/controllers/static_controller_spec.rb +++ b/spec/controllers/static_controller_spec.rb @@ -3,6 +3,15 @@ require 'rails_helper' describe StaticController do context 'brotli_asset' do + it 'returns a brotli encoded 404 if asset is missing' do + + get :brotli_asset, path: 'missing.js' + + expect(response.status).to eq(404) + expect(response.headers['Content-Encoding']).not_to eq('br') + expect(response.headers["Cache-Control"]).to match(/max-age=5/) + end + it 'has correct headers for brotli assets' do begin assets_path = Rails.root.join("public/assets")