ConservativeEtags
Use of browser cache for pages that have already been loaded has great performance benefits,
but it doesn't take into account things like Flash messages, which should only be
shown the first time that a page is loaded.
Also, I found it annoyingly easy to wind up with pages that never refresh...
What I needed was something that was a little more quick to expire ETags.
This gem will only send a 304 NOT_MODIFIED
if:
- The page has already been loaded during this user session
- The requested resources has not been modified
- AND the application has not been restarted since the last time the resource was loaded.
This eases development while still getting some pretty decent use of browser cache.
How does it work?
Within CoffeeScript:
A comparison of the current URL and csrf token tells the script whether this version of the page has already been loaded.
This means that it works with Turbolinks, which loads pages in the background. It's not possible to just check for a 304 NOT_MODIFIED
because of how Ajax works in browsers. It replaces a 304 response with the cached 200 SUCCESS
, which means you get no real info about the cache behavior from the XHR.
Inside the controller
A wrapper around the stale? method adds to the items being serialized into an ETag,
- Whatever is later:
- The application's start time (which will invalidate any page loaded before the last app restart)
- The session start time (invalidate any page loaded before the session was created)
- Or in development mode, the current time (always invalidate)
Usage
In your controller:
if conservative_stale? Batch.all, params[:page], @active_link
render :index
end
Inside your CoffeScript:
if ConservativeEtags.pageIsOld()
# set up ajax to get any new notifications that should be shown to the user
else
# Do stuff that should only happen the first time a page is loaded,
# Such as displaying the flash messages.
$('.alert.hidden-xs-up').removeClass('hidden-xs-up')
Installation
Add this line to your application's Gemfile:
gem 'conservative_etags'
And then execute:
$ bundle
Create an initializer file, config/initializers/conservative_etags.rb
, like this:
::ActionController::Base.send :include, ConservativeEtags::ApplicationControllerExtension
Edit production.rb
to contain the following:
Rails.application.config.x.app_start_time = DateTime.now
Contributing
Make a pull request.
License
The gem is available as open source under the terms of the MIT License.