Skip to content

Commit

Permalink
Enable brotli compression of matomo script
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Aug 4, 2022
1 parent 67e2c2a commit 748b115
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
20 changes: 20 additions & 0 deletions cookbooks/matomo/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
passwords = data_bag_item("matomo", "passwords")

package %w[
brotli
gzip
php-cli
php-curl
php-mbstring
Expand Down Expand Up @@ -153,6 +155,15 @@
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"
Expand All @@ -162,6 +173,15 @@
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"
Expand Down
13 changes: 11 additions & 2 deletions cookbooks/matomo/templates/default/apache.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,27 @@
ExpiresActive On
RewriteEngine on

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]

<FilesMatch "\.br\.gz$">
Header append Content-Encoding br
Header append Vary Accept-Encoding
</FilesMatch>

<FilesMatch "\.js\.gz$">
Header append Content-Encoding gzip
Header append Vary Accept-Encoding
</FilesMatch>

<FilesMatch "(\.js|\.js\.gz)$">
<FilesMatch "(\.js|\.js\.gz|\.js\.br)$">
ExpiresDefault "access plus 1 week"
Header set Cache-Control "max-age=604800"
</FilesMatch>
Expand Down

0 comments on commit 748b115

Please sign in to comment.