the once and future script loader (v3)

52
The Once and Future Script Loader Kyle Simpson @getify http://getify.me

Upload: kyle-simpson

Post on 26-Jun-2015

3.053 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: The Once And Future Script Loader (v3)

The Once and Future Script Loader

Kyle Simpson @getify http://getify.me

Page 2: The Once And Future Script Loader (v3)

<script>

Page 3: The Once And Future Script Loader (v3)

Why not just

combine all your files?

• different pages, different scripts

• cacheability

• parallel loading

• on-demand/lazy-loading

Page 4: The Once And Future Script Loader (v3)

concat

vs. parallel?BOTH

Page 5: The Once And Future Script Loader (v3)

document.write()

Page 6: The Once And Future Script Loader (v3)

document.write() Must Die!

Page 7: The Once And Future Script Loader (v3)

Performance

Page 8: The Once And Future Script Loader (v3)

IE7-, FF3-, Opera

Page 9: The Once And Future Script Loader (v3)

IE8, FF3.5/3.6, Chr

14-

Page 10: The Once And Future Script Loader (v3)

IE9+, FF4+

Script Loaders

Page 11: The Once And Future Script Loader (v3)

comparing DOM-ready times across loading techniques

Page 12: The Once And Future Script Loader (v3)

<script> tags also suck because...

• browser-specific scripts

• conditional loading/URLs

• event handling

Page 13: The Once And Future Script Loader (v3)

WTF Loader

document.write("<script src='...'></sc"+"ript>");

Page 14: The Once And Future Script Loader (v3)

Surely we can do better!?

Page 15: The Once And Future Script Loader (v3)

XHR?

Page 16: The Once And Future Script Loader (v3)

Making progress... but not there

yet

Page 17: The Once And Future Script Loader (v3)

What's hard about script loading?

Page 18: The Once And Future Script Loader (v3)

A general script loader should...

load any script, at any time, from anywhere... except document.write()

have performance at or better than <script> in modern browsers

• equivalent behavior in all

browsers

Page 19: The Once And Future Script Loader (v3)

A general script loader should also...

• avoid: hacks, UA sniffing

• feature-detect

• have as few exception-cases as possible

Page 20: The Once And Future Script Loader (v3)

General script loader feature creep...

• trying

to handle document.write()

• loading CSS

• dependency management

• delaying DOM-ready

Page 21: The Once And Future Script Loader (v3)

loading many scripts in parallel (race to finish loading ASAP)

• ensuring execution order (not

ASAP)

Script loading requires...

Page 22: The Once And Future Script Loader (v3)

LABjs

Page 23: The Once And Future Script Loader (v3)

LABjs: performance script loader

Page 24: The Once And Future Script Loader (v3)

LABjs: performance script loader

Page 25: The Once And Future Script Loader (v3)

What was

wrong with LABjs?

• ugly source code

• lots of hacks

• exception cases

• brittle (not future proof)

Page 26: The Once And Future Script Loader (v3)

LABjs

2.0

• readable

source code

• "future proof " feature detects

• fewer

exception cases

• better performance

http://labjs.com

Page 27: The Once And Future Script Loader (v3)

Anyone can write a loader!

(and many

devs

have)

Page 28: The Once And Future Script Loader (v3)

https://spreadsheets0.google.com/spreadsheet/ccc?key=tDdcrv9wNQRCNCRCflWxhYQ

Page 29: The Once And Future Script Loader (v3)

Loaders are competing more on APIs than on

features or performance

Page 30: The Once And Future Script Loader (v3)

They're also copying each other (good and bad)

Page 31: The Once And Future Script Loader (v3)

But, can their functionality

be

trusted?

Page 32: The Once And Future Script Loader (v3)

Testing is much

harder than most realize

Page 33: The Once And Future Script Loader (v3)

What should

a loader do?

Page 34: The Once And Future Script Loader (v3)

Real Preloading

http://wiki.whatwg.org/wiki/Script_Execution_Control

Page 35: The Once And Future Script Loader (v3)

IE4+ ftw?

Page 36: The Once And Future Script Loader (v3)

async=false

http://wiki.whatwg.org/wiki/Dynamic_Script_Execution_Order

Ordered Async

FF4+, Chr

12+, IE10p2+, Webkit/Safari, Opera (soon!)

Page 37: The Once And Future Script Loader (v3)

1. try real preloading

2. try ordered async

3. try same-domain XHR

4. fall back on "cache preloading"

How?

Page 38: The Once And Future Script Loader (v3)

Competition is good, only

if community is educated

Page 39: The Once And Future Script Loader (v3)

Browsers do not yet

give us what we really

need

Page 40: The Once And Future Script Loader (v3)

Co-opetition

is much healthier for the

community

Page 41: The Once And Future Script Loader (v3)

W3C, WHATWG

http://wiki.whatwg.org/wiki/Category:Proposals

Page 42: The Once And Future Script Loader (v3)

W3C, WHATWG

http://ygp.go.ly/script-preloading

http://odq.go.ly/load-error-events

Page 43: The Once And Future Script Loader (v3)

We'll accomplish more if we work together

Page 44: The Once And Future Script Loader (v3)

Future, The Script Loader

Page 45: The Once And Future Script Loader (v3)

Preloading

(deferred execution)

Page 46: The Once And Future Script Loader (v3)

Modules

(CommonJS, AMD, etc)

Page 47: The Once And Future Script Loader (v3)

Native Modules

(ES-Harmony?)

Page 48: The Once And Future Script Loader (v3)
Page 49: The Once And Future Script Loader (v3)

"Script Loader of my dreams"

Page 50: The Once And Future Script Loader (v3)

• timeouts

• load abort

• error handling

• load priority

What else?

Page 51: The Once And Future Script Loader (v3)

And now for something completely different...

Questions?

Page 52: The Once And Future Script Loader (v3)

Kyle Simpson @getify http://getify.me

http://labjs.com

http://wiki.whatwg.org/wiki/Category:Proposals