Optimise loading of piwik javascript
authorTom Hughes <tom@compton.nu>
Tue, 18 Jun 2019 08:01:08 +0000 (09:01 +0100)
committerTom Hughes <tom@compton.nu>
Tue, 18 Jun 2019 08:12:17 +0000 (09:12 +0100)
Serve it precompressed, and improve the cacheability.

cookbooks/piwik/recipes/default.rb
cookbooks/piwik/templates/default/apache.erb

index 9064522..c8b9b10 100644 (file)
@@ -33,7 +33,9 @@ package "php-apcu"
 
 package "geoipupdate"
 
+apache_module "expires"
 apache_module "php7.2"
+apache_module "rewrite"
 
 version = node[:piwik][:version]
 
@@ -56,6 +58,14 @@ execute "unzip-piwik-#{version}" do
   not_if { File.exist?("/opt/piwik-#{version}/piwik") }
 end
 
+execute "/opt/piwik-#{version}/piwik/piwik.js" do
+  command "gzip -k -9 /opt/piwik-#{version}/piwik/piwik.js"
+  cwd "/opt/piwik-#{version}"
+  user "root"
+  group "root"
+  not_if { File.exist?("/opt/piwik-#{version}/piwik/piwik.js.gz") }
+end
+
 directory "/opt/piwik-#{version}/piwik/config" do
   owner "www-data"
   group "www-data"
index 8f27414..58703f4 100644 (file)
 
 <Directory /srv/piwik.openstreetmap.org>
        Require all granted
+
+       ExpiresActive On
+       RewriteEngine on
+
+       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]
+
+       <FilesMatch "\.js\.gz$">
+               Header append Content-Encoding gzip
+               Header append Vary Accept-Encoding
+       </FilesMatch>
+
+       <FilesMatch "(\.js|\.js\.gz)$">
+               ExpiresDefault "access plus 1 week"
+               Header set Cache-Control "max-age=604800"
+       </FilesMatch>
 </Directory>