Skip to content

Instantly share code, notes, and snippets.

Avatar

Tom Aranda taranda

View GitHub Profile
@taranda
taranda / dynamic-critical-path-css.md
Last active Jul 7, 2021
Dynamically Add Critical CSS to Your Rails App
View dynamic-critical-path-css.md

Dynamically Add Critical CSS to Your Rails App

Optimizing the delivery of CSS is one way to improve user experience, load speed and SEO of your web app. This involves determining the "critical path CSS" and embeding it into the html of your page. The rest of the CSS for the site is loaded asynchronously so it does not block the rendering of your "above the fold" content. This Gist will show you how to dynamically generate critical path CSS for your Rails app.

In this example we will use the mudbugmedia/critical-path-css gem.

Prerequisites

You will need to set up caching and Active Job in your Rails app. I recommend using a thread-safe background job manager like resque.