From 6818a060ad9d20217b10428e23f9d6fb9ec9e33a Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 4 Aug 2022 20:31:15 +0100 Subject: [PATCH] Enable brotli compression of matomo script --- cookbooks/matomo/recipes/default.rb | 20 +++++++++++++++++++ cookbooks/matomo/templates/default/apache.erb | 15 +++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cookbooks/matomo/recipes/default.rb b/cookbooks/matomo/recipes/default.rb index 9b2b6302c..b916e83d7 100644 --- a/cookbooks/matomo/recipes/default.rb +++ b/cookbooks/matomo/recipes/default.rb @@ -25,6 +25,8 @@ include_recipe "php::fpm" passwords = data_bag_item("matomo", "passwords") package %w[ + brotli + gzip php-cli php-curl php-mbstring @@ -153,6 +155,15 @@ if File.symlink?("/srv/matomo.openstreetmap.org") subscribes :run, "execute[core:update]" end + execute "/opt/matomo-#{version}/matomo/matomo.br" do + action :nothing + command "brotli -k -9 /opt/matomo-#{version}/matomo/matomo.js" + cwd "/opt/matomo-#{version}" + user "root" + group "root" + subscribes :run, "execute[custom-matomo-js:update]" + end + execute "/opt/matomo-#{version}/matomo/matomo.js" do action :nothing command "gzip -k -9 /opt/matomo-#{version}/matomo/matomo.js" @@ -162,6 +173,15 @@ if File.symlink?("/srv/matomo.openstreetmap.org") subscribes :run, "execute[custom-matomo-js:update]" end + execute "/opt/matomo-#{version}/matomo/piwik.br" do + action :nothing + command "brotli -k -9 /opt/matomo-#{version}/matomo/piwik.js" + cwd "/opt/matomo-#{version}" + user "root" + group "root" + subscribes :run, "execute[custom-matomo-js:update]" + end + execute "/opt/matomo-#{version}/matomo/piwik.js" do action :nothing command "gzip -k -9 /opt/matomo-#{version}/matomo/piwik.js" diff --git a/cookbooks/matomo/templates/default/apache.erb b/cookbooks/matomo/templates/default/apache.erb index 589d563d6..5b503cae6 100644 --- a/cookbooks/matomo/templates/default/apache.erb +++ b/cookbooks/matomo/templates/default/apache.erb @@ -57,18 +57,27 @@ ExpiresActive On RewriteEngine on - RewriteCond "%{HTTP:Accept-encoding}" "gzip" + RewriteCond "%{HTTP:Accept-Encoding}" "br" + RewriteCond "%{REQUEST_FILENAME}\.br" -s + RewriteRule "^(.*)\.js" "$1\.js\.br" [QSA] + + RewriteCond "%{HTTP:Accept-Encoding}" "gzip" RewriteCond "%{REQUEST_FILENAME}\.gz" -s RewriteRule "^(.*)\.js" "$1\.js\.gz" [QSA] - RewriteRule "\.js\.gz$" "-" [T=text/javascript,E=no-gzip:1] + RewriteRule "\.js\.(br|gz)$" "-" [T=text/javascript,E=no-gzip:1,E=no-brotli:1] + + + Header append Content-Encoding br + Header append Vary Accept-Encoding + Header append Content-Encoding gzip Header append Vary Accept-Encoding - + ExpiresDefault "access plus 1 week" Header set Cache-Control "max-age=604800" -- 2.39.5