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
This commit is contained in:
Sam 2016-12-15 16:05:20 +11:00
parent f867af6bf9
commit 98f4a2adcb
2 changed files with 20 additions and 9 deletions

View File

@ -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
end

View File

@ -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")