Hendrik Mans

Reduced the size of the bundle served by the public-facing blog from a ridiculously broken 560 KB to a mere 22 KB with a few tricks.

First of all, I've replaced highlight.js with prism.js, which gave me better control over which languages to load (loading all of them really blew up the bundle size, so now I'm picking which ones to support.)

But also, I've finally bothered to serve gzipped assets, which is a bit more work than usual when you statically compile assets into your executable like a madman. I'm using baked_file_system to bake the contents of my Webpack-controlled public/ directory into Crankypants, and I've written this handy macro to serve assets gzipped (or not) depending on the request's headers:

macro serve_static_asset(name)
  if env.request.headers["Accept-Encoding"] =~ /gzip/
    env.response.headers.add "Content-Encoding", "gzip"
    Assets.get("{{ name.id }}.gz").gets_to_end
    Assets.get("{{ name.id }}").gets_to_end

I can use it from a Kemal action like this:

get "/blog-bundle.js" do |env|
  env.response.headers.add "Cache-Control", "max-age=600, public"
  env.response.content_type = "text/javascript"
  serve_static_asset "blog-bundle.js"

Good times! Especially response times. Haha!