Created
January 2, 2013 19:30
-
-
Save feulf/4437179 to your computer and use it in GitHub Desktop.
This snippet is an HTML page with a Javascript that "emulate" Varnish loading the <esi:include> blocks client side. The idea is to create a dynamic website in HTML loaded in S3, with groundbreaking performances. Hope this experiment will inspire better solutions.
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head></head> | |
<body> | |
<esi:include src="head.html" alt="not-found.html" onerror="continue"></esi:include> | |
<esi:include src="body.php" alt="not-found.html" onerror="continue"/> | |
<h2>HTML</h2> | |
</esi:include> | |
<esi:include src="footer.html" alt="not-found.html" onerror="continue"/></esi:include> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | |
<script> | |
function esiGet( obj ) { | |
var value, alt, error; | |
for( var j in obj.attributes ){ | |
switch( obj.attributes[j].name ){ | |
case "src": value = obj.attributes[j].value; | |
case "alt": alt = obj.attributes[j].value;; | |
} | |
} | |
$.ajax({ | |
url: value, | |
success: function( html ){ | |
$(obj).replaceWith(html); | |
}, | |
error: function(){ | |
$.ajax({ | |
url: alt, | |
success: function( html ){ | |
$(obj).replaceWith(html); | |
} | |
}) | |
} | |
}); | |
}; | |
var esi = document.getElementsByTagName("esi:include"); | |
for (var i = 0; i < esi.length; i++) { | |
esiGet( esi[i] ) | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a great day. ServiceWorkers can access the browser's cache, which can be used to accomplish the same end goal. No jQuery required.
https://developers.google.com/web/fundamentals/codelabs/offline/