Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Smaller is always better - at RoRo Sydney

Smaller is always better - at RoRo Sydney

Gzipping content has long been a best practice when serving text based files over the web. Turn on gzip compression in your web server, your users download smaller files quicker and everyone is happy, right?

That's what I thought as I went to tune my personal site over the holiday and I ended up writing 3 new gems and learning a whole bunch about the compression options available to us today. So join me on a journey to smaller and smaller files, discover how to tune your applications and find out how I ended up fighting a CDN over 1kB.

--

The gems:
https://github.com/philnash/jekyll-gzip
https://github.com/philnash/jekyll-zopfli
https://github.com/philnash/jekyll-brotli

Other important gems:
https://github.com/miyucy/zopfli
https://github.com/miyucy/brotli
https://github.com/hansottowirtz/sprockets-exporters_pack

Gzip a file in Ruby: https://philna.sh/blog/2018/02/25/gzip-file-ruby/

Phil Nash

May 08, 2018
Tweet

More Decks by Phil Nash

Other Decks in Programming

Transcript

  1. JEKYLL-GZIP Jekyll::Hooks.register :site, :post_write do |site| if ENV["JEKYLL_ENV"] == "production"

    Jekyll::Gzip::Compressor.new(site).compress end end 01. 02. 03. 04. 05. @philnash
  2. JEKYLL-GZIP def compress_file(file_name) zipped = "#{file_name}.gz" Zlib::GzipWriter.open(zipped, Zlib::BEST_COMPRESSION) do |gz|

    gz.mtime = File.mtime(file_name) gz.orig_name = file_name gz.write IO.binread(file_name) end end 01. 02. 03. 04. 05. 06. 07. 08. @philnash
  3. JEKYLL-GZIP def compress_file(file_name) zipped = "#{file_name}.gz" Zlib::GzipWriter.open(zipped, Zlib::BEST_COMPRESSION) do |gz|

    gz.mtime = File.mtime(file_name) gz.orig_name = file_name gz.write IO.binread(file_name) end end 01. 02. 03. 04. 05. 06. 07. 08. @philnash
  4. TABLE OF COMPRESSION Results when compressing unmini ed jQuery style

    bytes % uncompressed 271,751 - gzip default 80,669 -70.32% gzip best 80,268 -70.46% @philnash
  5. JEKYLL-ZOPFLI require "zopfli" def compress_file(file_name) zipped = "#{file_name}.gz" contents =

    Zopfli.deflate(File.read(file_name), format: :gzip) File.open(zipped, "w+") do |file| file << contents end end 01. 02. 03. 04. 05. 06. 07. 08. @philnash
  6. TABLE OF COMPRESSION Results when compressing unmini ed jQuery style

    bytes % uncompressed 271,751 - gzip default 80,669 -70.32% gzip best 80,268 -70.46% zop i 76,352 -71.90% @philnash
  7. JEKYLL-BROTLI require 'brotli' def compress_file(file_name) compressed = "#{file_name}.br" contents =

    Brotli.deflate(File.read(file_name), quality: 11) File.open(compressed, "w+") do |file| file << contents end end 01. 02. 03. 04. 05. 06. 07. 08. @philnash
  8. TABLE OF COMPRESSION Results when compressing unmini ed jQuery style

    bytes % uncompressed 271,751 - gzip default 80,669 -70.32% gzip best 80,268 -70.46% zop i 76,352 -71.90% brotli quality 6 75,302 -72.29% @philnash
  9. JEKYLL-BROTLI require 'brotli' def compress_file(file_name) compressed = "#{file_name}.br" contents =

    Brotli.deflate(File.read(file_name), quality: 11) File.open(compressed, "w+") do |file| file << contents end end 01. 02. 03. 04. 05. 06. 07. 08. @philnash
  10. TABLE OF COMPRESSION Results when compressing unmini ed jQuery style

    bytes % uncompressed 271,751 - gzip default 80,669 -70.32% gzip best 80,268 -70.46% zop i 76,352 -71.90% brotli quality 6 75,302 -72.29% brotli quality 11 66,920 -75.37% @philnash