• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
High Performance Snippets
 

High Performance Snippets

on

  • 9,790 views

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.

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.

Statistics

Views

Total Views
9,790
Views on SlideShare
9,572
Embed Views
218

Actions

Likes
9
Downloads
0
Comments
14

12 Embeds 218

http://join5works.com 66
http://blog.espol.edu.ec 60
http://lanyrd.com 25
http://us-w1.rockmelt.com 21
http://5works.co 19
http://localhost 11
http://www.alldaykids.com 7
http://tweetedtimes.com 4
http://www.twylah.com 2
http://apps.dev.agorapulse.com 1
http://www.onlydoo.com 1
http://faavorite.com 1
More...

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

110 of 14 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…

110 of 14 previous next

Post Comment
Edit your comment
  • flickr.com/photos/bestrated1/2141687384/
  • WidgetsAdsAnalytics
  • http://www.webpagetest.org/result/120530_HS_4BA/
  • http://www.webpagetest.org/result/120530_7R_4BJ/
  • http://www.webpagetest.org/result/120530_7R_4BJ/
  • http://www.webpagetest.org/result/120530_8D_4BQ/
  • flickr.com/photos/wwarby/3296379139/
  • flickr.com/photos/juditk/5024772809/whether it’s 1 or 5 requests, frontend SPOF will still happen
  • flickr.com/photos/myklroventine/4062102754/

High Performance Snippets High Performance Snippets Presentation Transcript

  • High PerformanceSnippets stevesouders.com/docs/fluent-snippets-20120530.pptx Disclaimer: This content does not necessarily reflect the opinions of my employer.
  • synchronous scripts blockall following elementsfrom renderingin all browsers
  • async#failasync sync async
  • Frontend SPOF
  • http://www.webpagetest.org/result/120529_41_HWV/
  • http://blog.patrickmeenan.com/2011/10/testing-for-frontend-spof.html
  • /etc/hosts:72.66.115.13 apis.google.com72.66.115.13 www.google-analytics.com72.66.115.13 static.ak.fbcdn.net72.66.115.13 connect.facebook.net72.66.115.13 platform.twitter.com72.66.115.13 widgets.twimg.comWebPagetest:setDnsName apis.google.com blackhole.webpagetest.orgsetDnsName www.google-analytics.com blackhole.webpagetest.orgsetDnsName static.ak.fbcdn.net blackhole.webpagetest.orgsetDnsName connect.facebook.net blackhole.webpagetest.orgsetDnsName platform.twitter.com blackhole.webpagetest.orgsetDnsName widgets.twimg.com blackhole.webpagetest.orgnavigate http://www.businessinsider.com/
  • original snippet<script src="http://widgets.twimg.com/j/2/widget.js"></script><script>new TWTR.Widget({ version: 2, type: profile, ...}).render().setUser(souders).start();</script>
  • <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 = "http://widgets.twimg.com/j/2/widget.js";var s0 = document.getElementsByTagName(script)[0];s0.parentNode.insertBefore(ts, s0);</script>
  • But... you can’t make a script async if it does document.writewidget.js does document.write:init: function(x) { ... this.id = x.id || "twtr-widget-" + this._widgetNumber; if (!x.id) { document.write(<div class="twtr-widget" id=" + this.id + "></div>) }}What if we create the DIV and specify the id?
  • 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;...
  • before
  • after
  • 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.
  • three facts: 1. failures happen 2. 304 & 200 both produce frontend SPOF 3. anywhere.js expires after 15 minutes
  • snippet cache timeshttp://platform.twitter.com/widgets.js - 30 minshttp://connect.facebook.net/en_US/all.js - 15 minshttp://www.google-analytics.com/ga.js - 120 mins
  • “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?
  • self-updating bootstrap scriptsstevesouders.com/blog/2012/05/22/self-updating-scripts/
  • part 1: update notificationpiggyback on dynamic requestpass cached version # to server: ?v=127server returns: 204 – no update JS – new version available
  • part 2: overwrite cachere-request dynamicallyrev the URL (querystring)XHR w/ setRequestHeaderreload iframe containing bootstrap script
  • example load bootstrap script: var s1=document.createElement(script); s1.async=true; s1.src=http://souders.org/tests/selfupdating/boo tstrap.js’; var s0=document.getElementsByTagName(script’)[0]; s0.parentNode.insertBefore(s1, s0);stevesouders.com/tests/selfupdating/
  • send beacon: http://souders.org/tests/selfupdating/beacon.js?v =02:29:53 beacon response triggers update: var iframe1 = document.createElement("iframe"); iframe1.style.display = "none”; iframe1.src = "http://souders.org/tests/selfupdating/update.p hp?v=02:29:53"; document.body.appendChild(iframe1);stevesouders.com/tests/selfupdating/
  • iframe reloads itself: <script src="http://souders.org/tests/selfupdating/boot strap.js"> </script> <script> if (location.hash === ) { location.hash = "check”; location.reload(true); } </script> reload triggers Conditional GET cached script is updatedstevesouders.com/tests/selfupdating/
  • voilàbootstrap scripts can have long cache timesANDget updated when necessary
  • caveatsthe update is used on the next page view (like app cache)1 reported IE8 issue
  • takeawaysload 3rd party scripts asynctest your site with blackhole.webpagetest.orghave RUM timeoutmake bootstrap scripts self- updating & increase cache times
  • Steve Souders @soudersstevesouders.com/docs/fluent-snippets-20120530.pptx