Are there good practices to run middleman on Heroku ?
For now the best approach I have is this:
Add a Rakefile that contains this:
namespace :assets do
desc 'heroku runs this command on push'
task :precompile do
system "bundle exec middleman build"
end
end
^ Heroku will think it’s a Rails app and run that step when building the slug.
Next we need a web server to send the statically compiled files. For now I’m using a Sinata app because it’s relatively fast and convenient.
require "sinatra"
set server: "thin"
# Ugly code that serves all assets directly from memory.
# TODO: .gz support
# TODO: handle redirects
static_dir = File.join settings.root, 'build'
Dir[File.join static_dir, '**', '*'].each do |file_path|
next unless File.file?(file_path)
file_data = IO.read(file_path)
path = file_path.sub(static_dir, '')
mime = Rack::Mime.mime_type File.extname(path)
if File.basename(path) == 'index.html'
path = (File.dirname(path) + '/').gsub(/\/+/, '/')
get path do
protected!
content_type :html
cache_control :private, :must_revalidate
body file_data
end
get path[0..-2] do
redirect to(path)
end
else
get path do
content_type mime
cache_control :public
body file_data
end
end
end
I’ve noticed that Heroku dynos are quite weak so it would make sense to use a pre-compiled binary like a Go program to squeeze out even more performance.