Skip to content

Instantly share code, notes, and snippets.

@ghoullier
Created October 18, 2013 15:59
Show Gist options
  • Save ghoullier/7043717 to your computer and use it in GitHub Desktop.
Save ghoullier/7043717 to your computer and use it in GitHub Desktop.
Iframe manifest proxy, avoid main page caching
<!DOCTYPE html>
<html manifest="manifest.appcache">
<body>
<script>
(function (root) {
// Local variables
var parent = root.parent,
location = root.location;
// Add event when root is loaded
root.addEventListener('load', function onRootLoad() {
var appCache = root.applicationCache;
if (appCache) {
appCache.addEventListener('cached', function onCached() {
detachIframe();
});
appCache.addEventListener('noupdate', function onNoUpdate() {
detachIframe();
});
appCache.addEventListener('updateready', function onUpdateReady() {
location.reload();
});
} else {
// ApplicationCache is not supported
}
});
/**
* Detach iframe from parent context
* @return {void}
*/
function detachIframe() {
if (parent && parent !== root) {
var parentDoc = parent.document,
proxy = parentDoc.querySelector('[data-manifest-proxy]');
if (proxy) {
proxy.parentNode.removeChild(proxy);
} else {
// Unable to find #manifest-proxy element
}
} else {
// Iframe parent context is not defined
}
}
}(this));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment