Middleman is a great tool for developing static websites with a bunch of tools like template engine, SASS or CoffeeScript. Jordan Elver has a great post about how to deploy a Middleman to Heroku as a Rack app serving your static files.
If you want to follow the suggestions from Google's PageSpeed Insights to increase your website performance, you'll need some additional steps.
There are some more things you can do:
The Middleman documentation suggest using middleman-imageoptim to optimize your images on build, but my previous experience was pretty painful–especially when you have many images in your website. It can make your every deploy really long, since it'll try to optimize every image after build.
If you're using a Mac, I'll suggest to do manual image optimization with the ImageOptim app. You just have to do it once, and drap your new images to the app before adding to your repository. All the images in your site are now optimized forever, so you don't have to wait for your images being re-optimized every time you deploy.
Enable gzip and leverage caching, the easy way
The easiest way to turn both things on is using
Rack::StaticCache. The later comes from the
rack-contrib project, which also hosts the
Just add these lines to your
And it'll work perfectly for you.
Serve pre-compiled gzip assets
Rack::Deflater middleware works great on providing gzipped files, but it's a real-time compression for every single request. It can cost extra CPU resource on your server.
Middleman has built-in support to generate pre-gzipped assets on build, so it'll be great if you can use them directly.
First, you'll need to turn this on in your
Then I use the rack-zippy Rack to replace the
Rack::Deflater middleware. This middleware simply check if there's a gzipped version for the file you're requesting, and serve it if found. Add the gem to your Gemfile and run bundle:
then add this to your
Modify Rack::StaticCache to work with rack-zippy
Then a problem comes:
rack-zippy does not work well with
Rack::StaticCache. The two middleware both try to serve a qualified file directly, rather than pass that file to another middleware.
If you put
use Rack::Zippy::AssetServer before
use Rack::StaticCache, you'll lose the extended expire header; and if you put it after, you'll lose the gzip file serving.
To make the two middlewares work properly, I need the
StaticCache to call
Zippy::AssetServer for file to serve, but still modify the header for extended expire dates. I've made a modified version of that, and pushed it to GitHub.
You can try that out by adding
gem 'zippy_static_cache' to your
Gemfile, than replace the
Rack::StaticCache part in your
config.ru with this:
And it should work with
rack-zippy now. But use at your own risk!
This is my final