Top Banner
High-Performance Social Plugins @stoyanstefanov, phpied.com Web Performance Summit Aug 29, 2012
40

High Performance Social Plugins

Jul 15, 2015

Download

Technology

Stoyan Stefanov
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: High Performance Social Plugins

High-Performance Social Plugins

@stoyanstefanov, phpied.com

Web Performance Summit Aug 29, 2012

Page 2: High Performance Social Plugins

// todo

• What's a social plugin?

• How does it plug?

• How does it work/optimize?

Page 3: High Performance Social Plugins

Plugin?

• is a third-party iframe

• grows sometimes

Page 4: High Performance Social Plugins

Plug?

#1 - You write the iframe

#2 - Third party JS writes iframe

Page 5: High Performance Social Plugins
Page 6: High Performance Social Plugins
Page 7: High Performance Social Plugins
Page 8: High Performance Social Plugins

#1: u-frame?

• Can't resize

• Some optimisations fail

• Not always advertised

• … but no 3rd party JS

Page 9: High Performance Social Plugins

#2: 3rd party JS

• all.js, plusone.js, widgets.js

• <script src="…">

• async JS

Page 10: High Performance Social Plugins

<script src="…">

• bad, bad

• blocks everything there is

• never ever

http://www.phpied.com/3po-fail/+ SPOF-O-Matic

Page 11: High Performance Social Plugins

Async JS

• dynamic script node

• not as short

• mostly default now

• only blocks onload in !IE

http://calendar.perfplanet.com/2011/the-art-and-craft-of-the-async-snippet/http://www.phpied.com/social-button-bffs/

Page 12: High Performance Social Plugins

unblocking onload

• does it matter?

1. window.onload =

function(){...};

2. FIF/Friendly iFrames/Meebo

Page 13: High Performance Social Plugins

fif

1) createiframe

src="js:false"

2) in the frame doc.write a <body onload …

3) …that loads JS

Page 14: High Performance Social Plugins

fif (snip)

Page 15: High Performance Social Plugins

fif

• unblocks onload (yey! but…)

• more complex

• requires 3rd party cooperationbecause JS now runs in a different window

Page 16: High Performance Social Plugins

fif coopBEFORE:

(function() {

// magic with window

// and document

}())

Page 17: High Performance Social Plugins

fif coopAFTER:

(function(window) {

var document = window.document;

// magic with window

// and document

}(window.inDapIF ?

parent.window : window))

Page 18: High Performance Social Plugins

inDapIF

• you signal to third party you load them in a frame

• defined by IAB

http://www.iab.net/media/file/rich_media_ajax_best_practices.pdf

Page 19: High Performance Social Plugins

fif

• experimental support in FB JS SDK

• [RFC] try it!

Page 20: High Performance Social Plugins

http://jsbin.com/axibow/1/edit

Page 21: High Performance Social Plugins

Plug?

#1 - You write the iframe

#2 - Third party JS writes iframe

Page 22: High Performance Social Plugins

Plug?

#1 - You write the iframe

#2 - Third party JS writes iframe• load 3rd party JS async

• try FIF

Page 23: High Performance Social Plugins

Plug?

#1 - You write the iframe• FIF too?

http://jsbin.com/uyepoj/1/edit

#2 - Third party JS writes iframe• load 3rd party JS async

• try FIF

Page 25: High Performance Social Plugins

// todo

• What's a social plugin?

• How does it plug?

• How does it work/optimize?

Page 26: High Performance Social Plugins

Like button's tasks

1. Show up

2. Resize itself (optional)

3. Handle user actions: like, comment

Page 27: High Performance Social Plugins

1. Show up

• Fast initial paint

• Single request

• Inline CSS

• Sprite vs. Data URI

• Inline async JS loader

Page 28: High Performance Social Plugins

2. Resize

• Inline JS

Page 29: High Performance Social Plugins

3. User actions

• Lazy

• Preload JS asap

• But eval JS only if necessary

Page 30: High Performance Social Plugins

Single request

Page 31: High Performance Social Plugins

Single request

Page 32: High Performance Social Plugins

JS preload #fail

• new Image().src

• <object>/<iframe>

• <link type=stylesheet>

• script type="cache/invalid"

• XMLHttpRequest

• script.preload = true

Page 33: High Performance Social Plugins

JS preload in Like button

• CORS: .com -> CDN

• XHR2 (and XDomainRequestfor IE8)

• IE6 and 7?

Page 34: High Performance Social Plugins

Progressive enhancement

<form><button type="submit">

+ Async JS ("ajaxification")

+ Preload (faster)

Page 35: High Performance Social Plugins

lazy evalfunction load() {

if (!preload()) {

return execute();

}

onmouseover = execute;

}

Page 36: High Performance Social Plugins

lazy evalfunction execute() {

onmouseover = null;

var js = document.createElement('script');

js.src = FILE;

document.head.appendChild(js);

}

Page 37: High Performance Social Plugins

lazy evalfunction preload() {

var xhr;

if (typeof XDomainRequest !== 'undefined') { // IE8

xhr = new XDomainRequest();

} else if (typeof XMLHttpRequest !== 'undefined') {

xhr = new XMLHttpRequest();

if (!("withCredentials" in xhr)) {

return; // sorry, XHR2 needed

}

} else {

return; // give up

}

xhr.open("GET", FILE, true);

xhr.send(null);

return true;

}

Page 38: High Performance Social Plugins

// todo

• What's a social plugin?

• How does it plug?

• How does it work/optimize?

Page 39: High Performance Social Plugins

Thank you!

Page 40: High Performance Social Plugins