Skip to content

Instantly share code, notes, and snippets.

@fancyremarker
Last active December 18, 2015 08:29
Show Gist options
  • Save fancyremarker/5754275 to your computer and use it in GitHub Desktop.
Save fancyremarker/5754275 to your computer and use it in GitHub Desktop.
Demonstration of browsers' failure to honor HTTP caching headers on history navigation
#!/usr/bin/env ruby
require "sinatra"
require "json"
get "/data.json" do
content_type :json
headers({
"Cache-Control" => "must-revalidate, no-store, no-cache, private",
"Pragma" => "no-store, no-cache",
"Etag" => "\"#{SecureRandom.hex}\"",
})
{ value: SecureRandom.hex }.to_json
end
get "/" do
content_type :html
<<-HTML
<!doctype html>
<html>
<body>
<h1 id="data-header"></h1>
<a href="/other">Go To Other</a>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$.getJSON('/data.json', function(data, text_status, xhr) {
$('#data-header').html(data['value']);
});
});
$(window).unload(function(){});
</script>
</body>
</html>
HTML
end
get "/other" do
content_type :html
<<-HTML
<!doctype html>
<html>
<body>
<h1>Welcome To Other</h1>
<a href="/">Go Home</a>
</body>
</html>
HTML
end
source "https://rubygems.org"
gem "sinatra"
@fancyremarker
Copy link
Author

To demo:

bundle install
ruby app.rb

Then open http://localhost:4567/ in a browser. Fails on: Chrome, Firefox, Safari.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment