conservative_etags 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +88 -0
- data/Rakefile +37 -0
- data/app/assets/javascripts/conservative_etags.coffee +27 -0
- data/lib/conservative_etags.rb +39 -0
- data/lib/conservative_etags/engine.rb +5 -0
- data/lib/conservative_etags/version.rb +3 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6273480c5ca392d1bd41c5928604bb39ee356ab9
|
4
|
+
data.tar.gz: d91ae5b54c56744f31b1094c34016a4dad4a0b6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fbb190c423dca56d389bdf2caa1b5f9e054578325788680c06a4c2f553382d26f5b1e32cc4e108c1a293d1539b6441150d8e4e9d14784994d487d263f0357e81
|
7
|
+
data.tar.gz: 6d830227629de2ba6e659e76bc20c9066956897ca75595f743d2bdb80b21cce79c3a8ee69bdc3a52090eef7da95298b2bfbac90d3c5b7f347264315fd3b9276a
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Tyler Gannon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# ConservativeEtags
|
2
|
+
|
3
|
+
Use of browser cache for pages that have already been loaded has great performance benefits,
|
4
|
+
but it doesn't take into account things like Flash messages, which should only be
|
5
|
+
shown the first time that a page is loaded.
|
6
|
+
|
7
|
+
Also, I found it annoyingly easy to wind up with pages that never refresh...
|
8
|
+
|
9
|
+
What I needed was something that was a little more quick to expire ETags.
|
10
|
+
|
11
|
+
This gem will only send a `304 NOT_MODIFIED` if:
|
12
|
+
|
13
|
+
* The page has already been loaded during this user session
|
14
|
+
* The requested resources has not been modified
|
15
|
+
* AND the application has not been restarted since the last time the resource was loaded.
|
16
|
+
|
17
|
+
This eases development while still getting some pretty decent use of browser cache.
|
18
|
+
|
19
|
+
### How does it work?
|
20
|
+
|
21
|
+
#### Within CoffeeScript:
|
22
|
+
A comparison of the current URL and csrf token tells the script whether this version of the page has already been loaded.
|
23
|
+
|
24
|
+
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.
|
25
|
+
|
26
|
+
#### Inside the controller
|
27
|
+
|
28
|
+
A wrapper around the stale? method adds to the items being serialized into an ETag,
|
29
|
+
|
30
|
+
* Whatever is later:
|
31
|
+
* The application's start time (which will invalidate any page loaded before the last app restart)
|
32
|
+
* The session start time (invalidate any page loaded before the session was created)
|
33
|
+
* Or in development mode, the current time (always invalidate)
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
### In your controller:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
# Creates an ETag based on whatever objects you pass it
|
41
|
+
# Automatically expired based on application start time, session start time, etc.
|
42
|
+
if conservative_stale? Batch.all, params[:page], @active_link
|
43
|
+
render :index
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
### Inside your CoffeScript:
|
48
|
+
```coffeescript
|
49
|
+
|
50
|
+
if ConservativeEtags.pageIsOld()
|
51
|
+
# set up ajax to get any new notifications that should be shown to the user
|
52
|
+
else
|
53
|
+
# Do stuff that should only happen the first time a page is loaded,
|
54
|
+
# Such as displaying the flash messages.
|
55
|
+
$('.alert.hidden-xs-up').removeClass('hidden-xs-up')
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
## Installation
|
60
|
+
Add this line to your application's Gemfile:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
gem 'conservative_etags'
|
64
|
+
```
|
65
|
+
|
66
|
+
And then execute:
|
67
|
+
```bash
|
68
|
+
$ bundle
|
69
|
+
```
|
70
|
+
|
71
|
+
### Create an initializer file, `config/initializers/conservative_etags.rb`, like this:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
::ActionController::Base.send :include, ConservativeEtags::ApplicationControllerExtension
|
75
|
+
```
|
76
|
+
|
77
|
+
### Edit `production.rb` to contain the following:
|
78
|
+
|
79
|
+
```
|
80
|
+
Rails.application.config.x.app_start_time = DateTime.now
|
81
|
+
```
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
Make a pull request.
|
86
|
+
|
87
|
+
## License
|
88
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ConservativeEtags'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
require 'bundler/gem_tasks'
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task default: :test
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class ConservativeEtags
|
2
|
+
@_checked: () ->
|
3
|
+
@_lastPageChecked == location.href
|
4
|
+
|
5
|
+
@_lastPageChecked = null
|
6
|
+
@_tokenKey: () ->
|
7
|
+
'csrf_token_' + location.href
|
8
|
+
|
9
|
+
@_old = false
|
10
|
+
|
11
|
+
@_checkPage: ->
|
12
|
+
unless @_checked()
|
13
|
+
window.new_csrf_token = $('meta[name=csrf-token]').attr('content')
|
14
|
+
window.old_csrf_token = sessionStorage.getItem(@_tokenKey())
|
15
|
+
if new_csrf_token == old_csrf_token
|
16
|
+
@_old = true
|
17
|
+
else
|
18
|
+
@_old = false
|
19
|
+
|
20
|
+
sessionStorage.setItem(@_tokenKey(), new_csrf_token)
|
21
|
+
|
22
|
+
|
23
|
+
@pageIsOld: ->
|
24
|
+
@_checkPage()
|
25
|
+
@_old
|
26
|
+
|
27
|
+
window.ConservativeEtags = ConservativeEtags
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "conservative_etags/engine"
|
2
|
+
|
3
|
+
module ConservativeEtags
|
4
|
+
module ApplicationControllerExtension
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
before_action :set_session_start, :set_last_modified
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_session_start
|
12
|
+
unless session.key? :start_time
|
13
|
+
session[:start_time] = Time.zone.now.iso8601
|
14
|
+
end
|
15
|
+
@session_start_time = Time.zone.parse(session[:start_time])
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_last_modified
|
19
|
+
app_start_time = Rails.application.config.x.app_start_time
|
20
|
+
if app_start_time.present?
|
21
|
+
@app_modified = [app_start_time, @session_start_time].max
|
22
|
+
else
|
23
|
+
@app_modified = Time.zone.now
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def conservative_stale?(*args)
|
28
|
+
args << @app_modified
|
29
|
+
if stale? etag: args
|
30
|
+
true
|
31
|
+
else
|
32
|
+
false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: conservative_etags
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler Gannon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A Gem for helping to manage page staleness, compatible with Turbolinks
|
42
|
+
5.
|
43
|
+
email:
|
44
|
+
- tyler@aprilseven.co
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- MIT-LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- app/assets/javascripts/conservative_etags.coffee
|
53
|
+
- lib/conservative_etags.rb
|
54
|
+
- lib/conservative_etags/engine.rb
|
55
|
+
- lib/conservative_etags/version.rb
|
56
|
+
homepage: http://github.com/tylergannon/conservative_etags
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.5.1
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: A Gem for helping to manage page staleness, compatible with Turbolinks 5.
|
80
|
+
test_files: []
|