Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

High Performance Snippets


Published on

What to look for and avoid when it comes to 3rd party snippets. How to test your site for frontend SPOF. Use the self-updating script pattern to increase the cacheability of bootstrap scripts.

Published in: Technology, News & Politics

High Performance Snippets

  1. High PerformanceSnippets Disclaimer: This content does not necessarily reflect the opinions of my employer.
  2. synchronous scripts blockall following elementsfrom renderingin all browsers
  3. async#failasync sync async
  4. Frontend SPOF
  7. /etc/hosts: static.ak.fbcdn.net72.66.115.13 connect.facebook.net72.66.115.13 platform.twitter.com72.66.115.13 widgets.twimg.comWebPagetest:setDnsName blackhole.webpagetest.orgsetDnsName blackhole.webpagetest.orgsetDnsName blackhole.webpagetest.orgsetDnsName blackhole.webpagetest.orgsetDnsName blackhole.webpagetest.orgsetDnsName blackhole.webpagetest.orgnavigate
  8. original snippet<script src=""></script><script>new TWTR.Widget({ version: 2, type: profile, ...}).render().setUser(souders).start();</script>
  9. <script> asyncified snippetfunction doTwitter() { if ( this.readyState && this.readyState != "complete" && this.readyState != "loaded" ) { return; } this.onload = this.onreadystatechange = null; new TWTR.Widget({ version: 2, type: profile, ... }).render().setUser(souders).start();}var ts = document.createElement("script");ts.async = true;ts.onload = ts.onreadystatechange = doTwitter;ts.src = "";var s0 = document.getElementsByTagName(script)[0];s0.parentNode.insertBefore(ts, s0);</script>
  10. But... you can’t make a script async if it does document.writewidget.js does document.write:init: function(x) { ... = || "twtr-widget-" + this._widgetNumber; if (! { document.write(<div class="twtr-widget" id=" + + "></div>) }}What if we create the DIV and specify the id?
  11. asyncified snippet plus DIV<div class=‘twtr-widget’ id=‘souderstwitter’></div><script>function doTwitter() { if ( this.readyState && this.readyState != "complete" && this.readyState != "loaded" ) { return; } this.onload = this.onreadystatechange = null; new TWTR.Widget({ id: ‘souderstwitter’, version: 2, type: profile, ... }).render().setUser(souders).start();}var ts = document.createElement("script");ts.async = true;...
  12. before
  13. after
  14. While placing JavaScript files at the bottom of thepage is a best practice for website performance,when including the anywhere.js file, always placethe file as close to the top of the page as possible.The anywhere.js file is small (<3KB) and is deliveredto the page GZIPd.Further, all dependancies for @Anywhere featuresare loaded asynchronously, on-demand so as to notimpact performance of the host page.
  15. three facts: 1. failures happen 2. 304 & 200 both produce frontend SPOF 3. anywhere.js expires after 15 minutes
  16. snippet cache times - 30 mins - 15 mins - 120 mins
  17. “bootstrap scripts” often have short cache timesmore frequent Conditional GET requests means frontend SPOF is more likelybut short cache times is the only way snippet owners can push updatesor is it?
  18. self-updating bootstrap
  19. part 1: update notificationpiggyback on dynamic requestpass cached version # to server: ?v=127server returns: 204 – no update JS – new version available
  20. part 2: overwrite cachere-request dynamicallyrev the URL (querystring)XHR w/ setRequestHeaderreload iframe containing bootstrap script
  21. example load bootstrap script: var s1=document.createElement(script); s1.async=true; s1.src= tstrap.js’; var s0=document.getElementsByTagName(script’)[0]; s0.parentNode.insertBefore(s1, s0);
  22. send beacon: =02:29:53 beacon response triggers update: var iframe1 = document.createElement("iframe"); = "none”; iframe1.src = " hp?v=02:29:53"; document.body.appendChild(iframe1);
  23. iframe reloads itself: <script src=" strap.js"> </script> <script> if (location.hash === ) { location.hash = "check”; location.reload(true); } </script> reload triggers Conditional GET cached script is
  24. voilàbootstrap scripts can have long cache timesANDget updated when necessary
  25. caveatsthe update is used on the next page view (like app cache)1 reported IE8 issue
  26. takeawaysload 3rd party scripts asynctest your site with blackhole.webpagetest.orghave RUM timeoutmake bootstrap scripts self- updating & increase cache times
  27. Steve Souders