Add a postprocess hook to gzip output text items.
authorNick Bowler <nbowler@draconx.ca>
Thu, 10 Jan 2019 03:00:58 +0000 (22:00 -0500)
committerNick Bowler <nbowler@draconx.ca>
Thu, 10 Jan 2019 03:00:58 +0000 (22:00 -0500)
This enables the use of nginx gzip_static functionality, reducing
per-request processing.  This is only done if GZIP_SITE is set to
a nonempty value in the environment so it can be run only when
publishing to avoid noise when authoring.

Rules

diff --git a/Rules b/Rules
index e81b5e2dc72afb004fbfe88459feddcaebc4f0a7..79dc5f46fa21212229e021c82f8204ff7b65f530 100644 (file)
--- a/Rules
+++ b/Rules
@@ -31,6 +31,23 @@ preprocess do
     end
 end
 
+postprocess do
+    # Gzip all text items for nginx http_static
+    if !ENV['GZIP_SITE'].to_s.empty?
+        reps = @items.flat_map(&:reps).each do |rep|
+            file = rep.raw_path
+            next if !file or rep.binary?
+
+            if system("gzip", "-ck9", file, [:out]=>[file + ".gz.tmp", "wb"])
+                system("touch", "-r", file, file + ".gz.tmp")
+                File.rename(file + ".gz.tmp", file + ".gz")
+            else
+                File.unlink(file + ".gz.tmp")
+            end
+        end
+    end
+end
+
 compile '/**/*.md' do
     filter :kramdown, header_offset: 1
     layout '/default.xml'