Giter Club home page Giter Club logo

bust_rails_etags's Introduction

Bust Rails Etags

Helps bust http caching on new deploys of a Rails app using the environment variable:

ENV["ETAG_VERSION_ID"]

You can see more explanation and history of the problem and this solution at my blog:

Rails Caching: A problem with etags and a solution

Or here's a summary:

Rails has awesome http caching abilities. However, when you deploy a new version of your Rails app, there isn't a good way to have that http cache get busted. And so browsers end up carrying around an old version.

One way of busting the cache is using:

ENV["RAILS_CACHE_ID"] = release_version["name"]

But changing that environment variable on deploys also busts things like your action and fragment caches. That's a bit too destructive.

So this gem creates a new environment variable you can use to bust only your http caches.

Installation

Add this line to your application's Gemfile:

gem 'bust_rails_etags'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bust_rails_etags

Usage

The goal of this gem is to provide an environment variable you can change that will bust all of your http caches for you app on deployments.

The recommended way of doing that is to create an initializer file in your Rails app's config/initializers folder. You can call it bust_http_cache.rb

Your goal in that file is to set ENV["ETAG_VERSION_ID"] to something that will change on each Rails deployment.

On Heroku I can use the release numbers. So in my bust_http_cache.rb I can do:

require 'heroku-api'

heroku = Heroku::API.new(:api_key => "2304u34oisefiou34342k3mdsd")
release_version = heroku.get_releases('myappname').body.last

ENV["ETAG_VERSION_ID"] = release_version["name"]

Your process can be different depending on your deployment environment. What's important is that the deploy changes the value of ENV["ETAG_VERSION_ID"] across all nodes of your application, and that every node also has the same value of ENV["ETAG_VERSION_ID"]. So you probably wouldn't want to do something like this:

ENV["ETAG_VERSION_ID"] = Time.now

Because that will cause different servers to have different values of ENV["ETAG_VERSION_ID"] if they were initialized at even slightly different times.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

bust_rails_etags's People

Contributors

n8 avatar

Watchers

Evan Arnold avatar

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.