Skip to content

Commit

Permalink
Optimise loading of piwik javascript
Browse files Browse the repository at this point in the history
Serve it precompressed, and improve the cacheability.
  • Loading branch information
tomhughes committed Jun 18, 2019
1 parent b73a91d commit 8adb918
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cookbooks/piwik/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@

package "geoipupdate"

apache_module "expires"
apache_module "php7.2"
apache_module "rewrite"

version = node[:piwik][:version]

Expand All @@ -56,6 +58,14 @@
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"
Expand Down
19 changes: 19 additions & 0 deletions cookbooks/piwik/templates/default/apache.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,23 @@

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

0 comments on commit 8adb918

Please sign in to comment.