Git Product home page Git Product logo

rails_critical_css_server's Introduction

RailsCriticalCssServer

Eliminate render-blocking CSS in above-the-fold content in your Rails app

If you've run Google Pagespeed Insights on your web app, you might have seen this message:

Eliminate render-blocking JavaScript and CSS in above-the-fold content

Your page has blocking CSS resources. This causes a delay in rendering your page. None of the above-the-fold content on your page could be rendered without waiting for the following resources to load. Try to defer or asynchronously load blocking resources, or inline the critical portions of those resources directly in the HTML.

This is a Ruby on Rails client for Critical CSS Server, designed to solve this problem for you outside of your build process.

Installation

Add this line to your application's Gemfile:

gem 'rails_critical_css_server'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rails_critical_css_server

Usage

In your application layout, just wrap your CSS in a call to Rewrite:

<%= RailsCriticalCssServer::Rewrite.call self do %>
  <%= stylesheet_link_tag "application" %>
<% end %>

Every time a new controller action is rendered, the server will begin to compile the bare-minimum CSS required to render the above the fold content on that page. Eventually, once the CSS is available, the critical portion will be injected, and the remainder of your CSS will be lazy-loaded with JavaScript.

Before CSS is ready:

The header will be rendered normally:

<link rel="stylesheet" href="/assets/application.css" />

After CSS is compiled:

<style>/* inline css rules injected here... */</style>
<script>// loadCSS injected here...</script>
<script>loadCSS("/assets/application.css");</script>
<noscript>
  <link rel="stylesheet" href="/assets/application.css" />
</noscript>

Busting the Cache

Each time CSS is compile, it is cached using the cache key: [controller, action, version].

By default, we used Heroku's environment variable HEROKU_RELEASE_VERSION to bust the version cache. This is available through the Heroku Labs: Dyno Metadata.

If this doesn't work for you, set the version flag in config to some other deploy-unique id, so that new CSS can be generated for you each time you change your pages.

Configuration Options

In config/initializers/critical_css_server.rb:

RailsCriticalCssServer.config do |c|
  c.host = ENV['CRITICAL_CSS_SERVER_URL'] # URL of Critical CSS Server
  c.auth_token = ENV['CRITICAL_CSS_SERVER_AUTH_TOKEN'] # Credentials for Critical CSS Server
  c.timeout = 0.05 # Timeout when requested critical CSS in layout.
  c.version = ENV['HEROKU_RELEASE_VERSION'] # Unique id for the current deploy. Used to bust the cache after changes are made
  c.width = 900 # width of viewport to optimize CSS for
  c.height = 1200 # height of viewport to optimize CSS for
  c.force_selectors = [] # force individual selectors to be included, even if they are not rendered on the first pass. Useful for JavaScript-enabled styles.
end

Under the hood:

License

The gem is available as open source under the terms of the MIT License.

rails_critical_css_server's People

Contributors

dependabot[bot] avatar hasghari avatar wheeyls avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rails_critical_css_server's Issues

Use the assets manifest instead of heroku env var

Rails.application.assets_manifest.assets["application.css"] => "application-46ae33e78e504ff295219f41d63c79719d062e48dc0c07bd9b6f7bcad72c6636.css"

That's a a unique version of the manifest if something changes, the whole md5 hash will change so you can use that to generate your cache key

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.