Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Compress responses when the client accepts it (#5378)

jezmor7df75675484c
diff --git a/config/application.rb b/config/application.rb
index 95811ffb38..89420355f2 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -31,6 +31,23 @@ module Sure
# Explicit source of truth for the session cookie name (also used by Codespaces).
config.session_store :cookie_store, key: SESSION_COOKIE_KEY
+ # Pages are server-rendered HTML that compresses ~15x, and the Docker image
+ # serves Puma directly, so without this every page crossed the network
+ # uncompressed unless a reverse proxy happened to compress it. A proxy that
+ # does compress leaves an already-encoded response alone. Not in
+ # development, where rack-mini-profiler has to inject into the HTML.
+ #
+ # After Rack::Sendfile, which every stack has: ActionDispatch::Static is
+ # absent when public_file_server is disabled. Text only (PDFs and images are
+ # already compressed), and never a 206, whose Content-Range counts bytes of
+ # the uncompressed file.
+ unless Rails.env.development?
+ config.middleware.insert_after Rack::Sendfile, Rack::Deflater,
+ include: %w[text/html text/vnd.turbo-stream.html text/css text/plain text/csv text/javascript
+ application/javascript application/json application/xml image/svg+xml],
+ if: ->(_env, status, _headers, _body) { status != 206 }
+ end
+
# TODO: This is here for incremental adoption of localization. This can be removed when all translations are implemented.
config.i18n.fallbacks = true
diff --git a/test/integration/response_compression_test.rb b/test/integration/response_compression_test.rb
new file mode 100644
index 0000000000..00390dc780
--- /dev/null
+++ b/test/integration/response_compression_test.rb
@@ -0,0 +1,54 @@
+require "test_helper"
+
+class ResponseCompressionTest < ActionDispatch::IntegrationTest
+ setup do
+ sign_in users(:family_admin)
+ end
+
+ test "pages are compressed for a client that accepts it" do
+ get root_path, headers: { "Accept-Encoding" => "gzip" }
+
+ assert_response :success
+ assert_equal "gzip", response.headers["Content-Encoding"]
+ assert_includes Zlib.gunzip(response.body), "</html>"
+ end
+
+ test "pages are sent as-is to a client that does not" do
+ get root_path
+
+ assert_response :success
+ assert_nil response.headers["Content-Encoding"]
+ assert_includes response.body, "</html>"
+ end
+
+ test "compression sits after Rack::Sendfile, which is in every stack" do
+ stack = Rails.application.middleware.map(&:klass)
+
+ assert_equal stack.index(Rack::Sendfile) + 1, stack.index(Rack::Deflater)
+ end
+
+ test "binary content and byte ranges are sent as-is" do
+ deflater = build_deflater
+
+ pdf = deflater.call(env_accepting_gzip.merge("TEST_STATUS" => 200, "TEST_TYPE" => "application/pdf"))
+ assert_nil pdf[1]["content-encoding"]
+
+ range = deflater.call(env_accepting_gzip.merge("TEST_STATUS" => 206, "TEST_TYPE" => "text/css"))
+ assert_nil range[1]["content-encoding"]
+
+ css = deflater.call(env_accepting_gzip.merge("TEST_STATUS" => 200, "TEST_TYPE" => "text/css"))
+ assert_equal "gzip", css[1]["content-encoding"]
+ end
+
+ private
+ # The app's own Rack::Deflater configuration around a stub app.
+ def build_deflater
+ middleware = Rails.application.middleware.find { |m| m.klass == Rack::Deflater }
+ app = ->(env) { [ env["TEST_STATUS"], { "content-type" => env["TEST_TYPE"] }, [ "x" * 2_000 ] ] }
+ middleware.build(app)
+ end
+
+ def env_accepting_gzip
+ Rack::MockRequest.env_for("/", "HTTP_ACCEPT_ENCODING" => "gzip")
+ end
+end