Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

conservative_etags

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

conservative_etags

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

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:

# Creates an ETag based on whatever objects you pass it
# Automatically expired based on application start time, session start time, etc.
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.

FAQs

Package last updated on 01 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc