Last active
November 14, 2019 12:56
-
-
Save pierre-pretorius/25c6c67cf8745bedcc241f818189e3ed to your computer and use it in GitHub Desktop.
Store turbolinks cache in window.localStorage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NB: This is a proof of concept and needs work before using in production. | |
// https://github.com/turbolinks/turbolinks/issues/196#issuecomment-320610483 | |
// Copy paste the javascript below to somewhere that loads after turbolinks.js. | |
// Source: Converted coffee to JS with: https://awsm-tools.com/code/coffee2js | |
// https://github.com/turbolinks/turbolinks/blob/master/src/turbolinks/snapshot.coffee | |
Turbolinks.Snapshot.fromHTMLElement = function (htmlElement) { | |
var bodyElement, headDetails, headElement, _ref; | |
headElement = htmlElement.querySelector("head"); | |
bodyElement = (_ref = htmlElement.querySelector("body")) != null ? _ref : document.createElement("body"); | |
headDetails = Turbolinks.HeadDetails.fromHeadElement(headElement); | |
var snapshot = new this(headDetails, bodyElement, htmlElement.outerHTML); | |
snapshot.htmlString = htmlElement.outerHTML; | |
return snapshot; | |
}; | |
Turbolinks.Snapshot.prototype.clone = function () { | |
var snapshot = new this.constructor(this.headDetails, this.bodyElement.cloneNode(true)); | |
snapshot.htmlString = this.htmlString; | |
return snapshot; | |
}; | |
// Source: Converted coffee to JS with: https://awsm-tools.com/code/coffee2js | |
// https://github.com/turbolinks/turbolinks/blob/master/src/turbolinks/snapshot_cache.coffee | |
Turbolinks.LocalCache = (function () { | |
var keyForLocation; | |
function LocalCache(_at_size) { | |
this.size = _at_size; | |
this.keys = JSON.parse(window.localStorage.getItem('keys')) || []; | |
} | |
LocalCache.prototype.has = function (location) { | |
var key; | |
key = keyForLocation(location); | |
return window.localStorage.getItem(key) !== null; | |
}; | |
LocalCache.prototype.get = function (location) { | |
var snapshot; | |
if (!this.has(location)) { | |
return; | |
} | |
snapshot = this.read(location); | |
this.touch(location); | |
return snapshot; | |
}; | |
LocalCache.prototype.put = function (location, snapshot) { | |
this.write(location, snapshot); | |
this.touch(location); | |
return snapshot; | |
}; | |
LocalCache.prototype.read = function (location) { | |
var key; | |
key = keyForLocation(location); | |
return Turbolinks.Snapshot.wrap(window.localStorage.getItem(key)); | |
}; | |
LocalCache.prototype.write = function (location, snapshot) { | |
var key; | |
key = keyForLocation(location); | |
window.localStorage.setItem('keys', JSON.stringify(this.keys)); | |
return window.localStorage.setItem(key, snapshot.htmlString); | |
}; | |
LocalCache.prototype.touch = function (location) { | |
var index, key; | |
key = keyForLocation(location); | |
index = this.keys.indexOf(key); | |
if (index > -1) { | |
this.keys.splice(index, 1); | |
} | |
this.keys.unshift(key); | |
return this.trim(); | |
}; | |
LocalCache.prototype.trim = function () { | |
var key, _i, _len, _ref, _results; | |
_ref = this.keys.splice(this.size); | |
_results = []; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
key = _ref[_i]; | |
_results.push(window.localStorage.removeItem(key)); | |
} | |
return _results; | |
}; | |
keyForLocation = function (location) { | |
return Turbolinks.Location.wrap(location).toCacheKey(); | |
}; | |
return LocalCache; | |
})(); | |
Turbolinks.controller.cache = new Turbolinks.LocalCache(20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment