JS in Rural Communities

43
JS in Rural Communities Matthew Keas

description

This talk is anecdotal about the story of YouTube Feather, the woes of browser caching, the effects of HTML5 Unicorns, and the undying love for all things kitten videos. Given at http://www.jseverywhere.org/ on October 25, 2013 in San Francisco by Matthew Keas. http://mkeas.org http://twitter.com/matthiasak

Transcript of JS in Rural Communities

Page 1: JS in Rural Communities

JS in Rural Communities

MatthewKeas

Page 2: JS in Rural Communities

http://mkeas.org

@matthiasak

http://parivedasolutions.com

Page 3: JS in Rural Communities
Page 4: JS in Rural Communities

may h

appen upon

here your eye

HARK!

a story of d

ragons

Page 5: JS in Rural Communities

"one of the senior engineers began a rant about thepage weight of the video watch page being far too

large"

Page 6: JS in Rural Communities

"1.2MB and dozens of requests""if they can write an entire Quake clone in under

100KB, we have no excuse for this!"

Page 7: JS in Rural Communities

tinyHTML

"After  three  painstaking  days,  I  had  arrived  at  a  much  leaner

solution.  It  still  was  not  under  100KB  though.  Having  just

finished  writing  the  HTML5  video  player,  I  decided  to  plug  it

in  instead  of  the  far  heavier  Flash  player.  Bam!  98KB  and

only  14  requests."

Page 8: JS in Rural Communities

"I decided to limit the functionality"

mastheadvideo playerfive related videossharing buttonflagging toolten comments loaded in via AJAX

remedy?

Page 9: JS in Rural Communities

"After  a  week  of  data  collection,  the  numbers  came  back…and  they  were  baffling.  The  average  aggregate  page  latency

under  Feather  had  actually  INCREASED"

HALLOWEENParty

Page 10: JS in Rural Communities

"We  plotted  the  data  geographically  and  compared  it  to  our  totalnumbers  broken  out  by  region"

howdy!

hello!

YOUTUBE

.  YES!!

Page 11: JS in Rural Communities

Come in, WE’RE

OPENSouth America Southeast Asia

Africa

Remote Siberia

Page 12: JS in Rural Communities

Average page load time under Feather was over TWOMINUTES!

Normal page would take over 20 minutes.

50POSTAGE

STAMP

Page 13: JS in Rural Communities

"By keeping your client side code small andlightweight, you can literally open your product up

to new markets."

ADDING MATT'ISM

Improve you experience and broaden your reachwith:

• Fewer requests per page load• Smaller requests per page load• Less frequent requests per page load

Page 14: JS in Rural Communities

ADDING MATT'ISM

Improve you experience and broaden your reachwith:

• Fewer requests per page load (inlining, base64encoding)• Smaller requests per page load (concatenation,compression, minification)• Less frequent requests per page load (caching)

Page 15: JS in Rural Communities

IF YOU CARE

OR BEWARE

CACHE

Page 16: JS in Rural Communities

Methods of Caching?

HTML <meta> tags(http://www.mnot.net/cache_docs/)HTTP Headers(http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/)Persist data via cookies...Appcache JavaScript?

Implied persistence

ADMIT

NONE23 17

Page 17: JS in Rural Communities

HTML <meta> tags

Meta tags are easy to use, but aren’t very effective. That’s because they’re only honored by a fewbrowser caches, not proxy caches (which almostnever read the HTML in the document).

Page 18: JS in Rural Communities

HTTP Headers

HTTP/1.1 200 OKCache-Control: no-transform,public,max-age=300,s-maxage=900Content-Type: text/html; charset=UTF-8Date: Mon, 29 Apr 2013 16:38:15 GMTETag: "bbea5db7e1785119a7f94fdd504c546e"Last-Modified: Sat, 27 Apr 2013 00:44:54 GMTServer: AmazonS3Vary: Accept-EncodingX-Cache: HIT

Example Caching headers for dynamic content (Wordpress blog):

Page 19: JS in Rural Communities

HTML5 Appcache

Pretty Sexy

No support IE9- (trolololollllll)Offline browsing - users can navigate your full sitewhen they're offline. (good)Speed - cached resources are local, and thereforeload faster. (good)Reduced server load (good)

(http://caniuse.com/#search=appcache)

Page 20: JS in Rural Communities

HTML Appcache

Page 21: JS in Rural Communities

:–(Más Problemas

One failed file in CACHE section => entire cachedisregarded.Regardless of whether you include the addressof the current page in the manifest, it will becached.In Firefox, any resources served with Cache-control: no-store will not be cached, even ifthey're explicitly included in the manifest.No granular dynamic control from JS.

http://appcachefacts.info/

Page 22: JS in Rural Communities

Is using JS to manage your cachereasonable?

Yes. (http://caniuse.com/#feat=namevalue-storage)

If you need to support IE7 or Opera Mobile, thenemploy HTTP Headers and/or just load resourceslazily.

Also, polyfills.

IE8 implemented this?Wow.

Page 23: JS in Rural Communities

Is this an original idea?

Sort of.

Analytics

Page 24: JS in Rural Communities

Many Script Loaders EmployedToday

Page 25: JS in Rural Communities

We truly are spoiled with such anawesome community

Page 26: JS in Rural Communities

We started POC'ing LocalStorage as a cache

KICKSGRASS

Local Storage

SINCE IE8

Page 27: JS in Rural Communities

Local Storage

PersistentAt least 5MBNever attached blindly to requestsEvent model to keep other tabs and windowssynchronizedPolyfills

Like  cookies

Page 28: JS in Rural Communities

Broke-al Storage

String values only — serialization may benecessary (awkward)Unstructured data with no transactions, indexingor searching facilities (awkward)May exhibit poor performance on large datasets(bad)

Page 29: JS in Rural Communities

Loader

Loadername of the year

honorary

https://github.com/matthiasak/Loader

Page 30: JS in Rural Communities

Loading Browser Assets

CORS compatible or Same Origin? AJAX inparallel, cache, and execute/embed sequentially.Not Same Origin? Add Script/Link and set thesrc/href.

Page 31: JS in Rural Communities

Loading Browser Assets

Promises to control flow of async downloads and"readystates".Small abstraction for making a GETSmall functions to write tags to the <HEAD>Tiny abstraction for Local Storage and errorhandlingAutomatically look for files to load (in similarfashion to Require.js)

Page 32: JS in Rural Communities

Begin by Loading Loader

Page 33: JS in Rural Communities

Load Only What Is Needed

12

3

Page 34: JS in Rural Communities

Performance, Pro-formance

http://www.stevesouders.com/blog/2011/03/28/storager-case-study-bing-google/

"Bing and Google Search make extensive use oflocalStorage for stashing SCRIPT and STYLE blocksthat are used on subsequent page views. None of

the other top sites from my previous post uselocalStorage in this way. Are Bing and Google

Search onto something?

Yes, definitely."

Page 35: JS in Rural Communities

Real-World Testing?

http://jsperf.com/localstorage-versus-browser-cache/6

Page 36: JS in Rural Communities

"Cutting the mustard"

http://responsivenews.co.uk/post/18948466399/cutting-the-mustard

Page 37: JS in Rural Communities
Page 38: JS in Rural Communities

Where I Put My Experiments Before

They Are Ready For Primetime

Page 39: JS in Rural Communities

FIRS

T

VISIT

Page 40: JS in Rural Communities

FIRSTVISIT

Page 41: JS in Rural Communities

SPEED

DEMON?

SECOND VISIT

!

Page 42: JS in Rural Communities

SECONDVISIT

Page 43: JS in Rural Communities

THANKYOU

@matthiasak