Often in our applications we need to add pages with fairly static content (FAQ, About Us, etc). We could implement a full blown CMS or we could create a few HTML/ERB files. Let’s explore different approaches.
Template and controller
First we create
home/about template and controller.
Let’s do a quick perf test using wrk.
./wrk -d30s http://localhost:3000/home/about gives us
Now we implement caching to speed up page loading. To enable Rails view caching we update development.rb or production.rb.
cache block to
Data will be cached like this:
Running same perf test as before gives us
Requests/sec: 66.16 which is slower.
High Voltage gem
high_voltage is a Rails engine for creating static pages. We can add
cache block to these files same as before.
We can create folder/subfolder structure and apply caching to it.
Without caching running
./wrk -d30s http://localhost:3000/pages/products gives us
Requests/sec: 73.55. With caching enabled we get
Applying caching to such static files actually slows things down a little bit. If our pages had rich CSS or pulled data from DB then caching would likely speed things up.
Another option is to simply put HTML/CSS/JS into public folder.
./wrk -d30s http://localhost:3000/about.html gives us
Requests/sec: 56277.05. MUCH faster.
Static site generators
For more complex docs (such as technical documentation) we can integrate a static site generator like middleman or jekyll into our Rails app. Middleman gives us tools similar to Rails (helpers, partials, layouts) to speed up our development.
middleman to Gemfile and run
middleman init content from app root. It will create
content subfolder. We want to output static HTML to public/content subfolder to serve via our webserver.
Running perf test
./wrk -d30s http://localhost:3000/content gives us
Requests/sec: 9936.43. Slower than plain about.html page but now our page is much richer with CSS / JS.
After we deploy the application we need to run
cd content && middleman build via a post deploy hook on production server. Read middleman documentation for how to implement various features it supports.