Slide 1

Slide 1 text

Going offline Offline caching for a more responsive, more reliable eb for everybody ndre Betts (@triblondon) Director, FT Labs (@ftlabs)

Slide 2

Slide 2 text

1880 1900 1920 1940 1960 1980 2000 2020

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

orks offline Portable Long battery life Can be read in bright sunlight Cheap Ubiquity Bookmarking Sharing Nesprint Fast start up Clipping/saving Can be read in the dark Updates in real time Electronic delivery Search Personalisation Deep linking

Slide 5

Slide 5 text

orks offline Portable Long battery life Can be read in bright sunlight Cheap Ubiquity Bookmarking Sharing ‘Traditional’ eb Fast start up Clipping/saving Can be read in the dark Updates in real time Electronic delivery Search Personalisation Deep linking

Slide 6

Slide 6 text

orks offline Portable Long battery life Can be read in bright sunlight Cheap Ubiquity Bookmarking Sharing pps Fast start up Clipping/saving Can be read in the dark Updates in real time Electronic delivery Search Personalisation Deep linking

Slide 7

Slide 7 text

e need to care about supporting existing features as much as getting ne ones.

Slide 8

Slide 8 text

elcome back to the eb The FT eb app provides a touch optimised user experience ithout native code.

Slide 9

Slide 9 text

orks offline Portable Long battery life Can be read in bright sunlight Cheap Ubiquity Bookmarking Sharing Ne eb Fast start up Clipping/saving Can be read in the dark Updates in real time Electronic delivery Search Personalisation Deep linking

Slide 10

Slide 10 text

Learn ho the creates offline eb apps ith scale, performance and reliabilty.

Slide 11

Slide 11 text

Sometimes it’s hat e’ve done But e’ve learned a lot, so to be honest sometimes it’s hat e ould do if e did it again.

Slide 12

Slide 12 text

hat is an app?

Slide 13

Slide 13 text

Objective C vs JavaScript pps are ritten in platform specific, proprietary stack. If it’s ritten just using eb technologies, it’s not an app.

Slide 14

Slide 14 text

‘Do stuff’ vs ‘Get stuff’ pps allo you to ‘do’ things, alloing you to take control of your environment in some ay. Things that don’t, aren’t apps.

Slide 15

Slide 15 text

‘Interface’ vs ‘document’ n app has an control interface that isn’t based on a page metaphor. ebsites aren’t apps because they deliver interfaces using pages.

Slide 16

Slide 16 text

Made for me. n app is an experience that feels like it as designed for you, to use in the ay you ant it to ork. It fits you like your favourite pair of shoes.

Slide 17

Slide 17 text

genda •  The client-side storage family •  Example techniques and challenges – Bootstrapping – Storage optimisation – Connection state detection

Slide 18

Slide 18 text

The client-side storage family •  HTML5 application cache •  localStorage and sessionStorage •  ebSQL and IndexedDB •  Cookies

Slide 19

Slide 19 text

HTML5 application cache •  ttention seeker –  Used even hen online •  Non-conformist –  Ne unfamiliar manifest syntax •  Refuses to change –  Cached files only update hen manifest changes •  Ignores instructions –  Caches the page it as referenced from even though you didn’t ask it to. •  Does something ne and ingenious –  Caches HTTP responses, so can serve first page request, before JS PIs are available.

Slide 20

Slide 20 text

localStorage •  Easy to talk to, but not good at detail –  Simple key/value store •  Can’t multi-task –  Synchronous PI blocks JS execution for I/O •  Quick off the mark –  Typically lookups by key are much quicker than ebSQL or IndexedDB •  Only functions ell in familiar surroundings –  JavaScript PI requires a page already loaded

Slide 21

Slide 21 text

IndexedDB / ebSQL •  Good at complex things –  Flexible, indexed, structured data store •  Great memory –  Storage limits typically higher than localStorage •  Can multi-task –  I/O on database is asynchronous •  Takes a hile to make decisions –  Sloer than localStorage for simple lookups •  Prefers a familiar environment –  Like localStorage, a JS PI that requires a page to have loaded already.

Slide 22

Slide 22 text

Cookies •  Tiny memory –  Max 4KB per domain •  Insists on telling hat it does remember to anyone that’ll listen –  Cookie data sent to server in every request •  Tends to offend Government –  Regulators in ashington and Brussels have legislated to make cookies more akard to use.

Slide 23

Slide 23 text

Bootstrapping

Slide 24

Slide 24 text

hy Bootstrap? •  Many URLs, not all in app cache •  Need front-end router / front controller •  Handle errors •  pply capability-based behaviours •  Mollify attention seeking app cache

Slide 25

Slide 25 text

ny page ith manifest attribute ill get cached, like it or not. ny cached page ill be used in preference to fetching a fresh copy, even hen online.

Slide 26

Slide 26 text

   

Slide 27

Slide 27 text

pp cache orkaround •  Only cache one ‘master’ URL:    ...    

Slide 28

Slide 28 text

 good bootstrap •  Snippet of JS at the end of every page •  dd resources based on capabilities •  Special bootstrap-only page for fallback router

Slide 29

Slide 29 text

Let’s boot.

Slide 30

Slide 30 text

Online Request  page   Is  there  an  appcache   associated  with  this   origin?   Is  URL  in   appcache?   Is  URL  in     NETWORK?     Matching   FALLBACK?   Make  request.   Did  it  work?   Is  it  HTML?   Does  it  have  a     manifest  aIribute?   In  browser   cache?   Request  from   network.   For  each     subresource...   Make  request.   In  browser   cache?   Request  from   network.   Yes No No Yes Okay No Righto Yup Yes No ay Uh oh... On it. Did  it  work?   Is  it  HTML?   Ok, Ok. Yup Manifest?   In  browser   cache?   Different  from   exisMng  manifest?   No Er... no, obviously. The IFRME is Yes... Request   manifest.   ilco. Yes! Run JS

Slide 31

Slide 31 text

Online Is  browser  capable   of  supporMng   advanced  experience?   Are  advanced  experience   resources  available  in   localStorage?   Load  resources   from  localStorage   Add  resources   to  page   Fire  init   process   You’re  all  set   Does  page   have  content?   Does  content   match  URL?   Fabulous. Yes Yes Okay Yes Yes On it. Done

Slide 32

Slide 32 text

Offline Request  page   Is  there  an  appcache   associated  with  this   origin?   Is  URL  in   appcache?   Is  URL  in     NETWORK?     Matching   FALLBACK?   Make  request.   Did  it  work?   Use  fallback.   Does  it  have  a     manifest  aIribute?   In  browser   cache?   Request  from   network.   For  each     subresource...   Start  again.   Yes No No Yes Er... No But... NO! Oh, OK. No ay Uh oh...

Slide 33

Slide 33 text

Is  browser  capable   of  supporMng   advanced  experience?   Are  advanced  experience   resources  available  in   localStorage?   Load  resources   from  localStorage   Add  resources   to  page   Fire  init   process   You’re  all  set   Does  page   have  content?   Examine  URL  and   dispatch  controller   DOMDOMDOM.... Yes Yes Okay Oh, right. No! Panic! On it. Offline Done. Populate  page   content   Fabulous.

Slide 34

Slide 34 text

Splash  screen   Basic  CSS   Error  msgs  /  support  info   Boot  code   Main  app  code   Main  app  CSS   App  HTML  templates   natomy of a fallback: •  Just-in-case content –  Remove after boot •  Bootstrap JS •  dditional resources added by bootstrap •  dditional content added by main app code Content  

Slide 35

Slide 35 text

hat to put in app cache YES: •  Fonts •  Splash image •  pp icon •  Fallback bootstrap NO: •  CSS •  HTML •  JavaScript

Slide 36

Slide 36 text

Storage optimisation ll storage technologies are limited by tiny quotas. e need to learn to live ith less.

Slide 37

Slide 37 text

ebSQL () / IndexedDB (i) ppcache localStorage Cookies iOS (5.1) : 5MB / 50MB 10MB 5MB 4KB total Up to 50 per domain ndroid (4) ? Unlimited 5MB Safari (5.2) : 5MB / Unlimited Unlimited 5MB Chrome (18) : Unlimited 5MB 5MB IE (10) i: 500MB 50MB 10MB Opera (11) : 5MB / Unlimited 50MB / Unlimited 5MB / Unlimited Firefox (11) i: 50MB / Unlimited Unlimited 10MB Storage limits Two  figures  separated  by  /  indicate  limit  with  and  without  user  consent  prompt,  which  appears  automaMcally  when  you  try  to   store  more  than  the  lower  limit.    The  user  may  not  consent  to  more  than  the  upper  limit.  

Slide 38

Slide 38 text

Text encoding ‘compression’ •  JavaScript internally uses UTF-16 for text encoding •  Great idea for processing: fast string operations, full support for Unicode BMP •  Terrible idea for storage of English text or base-64 encoded images.

Slide 39

Slide 39 text

n analogy.

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

SCII packed into UTF-16 Text   S   i   m   p   l   e   Decimal   83   105   109   112   108   101   As  binary   01010011   01101001   01101101   01110000   01101100   01100101   Shi^ed   01010011  01101001   01101101  01110000   01101100  01100101   As  hex   53  69   6D  70   6C  65   UTF-­‐16   卩   浰   汥  

Slide 44

Slide 44 text

eird. Not your father’s compression algorithm, that’s for sure.

Slide 45

Slide 45 text

Connection state Or hy online and offline events should not be trusted to anser the question ‘are e online?’

Slide 46

Slide 46 text

Question ccording to cycling proficiency instructors, hat is a driver signaling if he or she extends their arm straight out of their car indo?

Slide 47

Slide 47 text

Question ccording to cycling proficiency instructors, hat is a driver signaling if he or she extends their arm straight out of their car indo? The indo is open

Slide 48

Slide 48 text

Q: If the value of indo.navigator.onLine is true, hat does that mean?

Slide 49

Slide 49 text

.  ny request made by the broser ill succeed. B.  The connection is nice and fast. C.  ll ebsites ill load equally ell D.  The device has sync ith the ISP E.  The last netork request succeeded

Slide 50

Slide 50 text

navigator.onLine is meaningless hen true, and only mostly meaningless hen false.

Slide 51

Slide 51 text

Better connection state data •  Lobby for better device PIs – Meanhile, polyfills using phonegap, BB eborks, Metro •  Test it in the background •  Buffer requests to send, and keep retrying. •  Be honest. – “It didn’t ork. e have no idea hy. Sorry.”

Slide 52

Slide 52 text

Summary •  The ne eb is finally the potential print successor e’ve been aiting for. •  User experience is key across varied platforms •  The ne eb can deliver reliable offline apps today. •  You don’t have to sacrifice features •  ith good optimisations you can get a great app experience

Slide 53

Slide 53 text

HTML5 is not finished, and it’s getting murkier: •  HTG maintains the ‘living standard’ •  3C creates frozen snapshot CUTION

Slide 54

Slide 54 text

“Don’t build native apps, build eb apps” -­‐  Tim  Berners-­‐Lee  

Slide 55

Slide 55 text

Thanks andre@labs.ft.com @triblondon, @FTLabs Do you ant to build this stuff? Join in. [email protected] Image  credits::     hIp://cdn3.worldcarfans.co/2008/2/medium/9080214.017.Mini1L.jpg,    hIp://www.netbasic.com/blog/2008/10/mini-­‐car-­‐parking-­‐fail,  hIp://runningstopsigns.files.wordpress.com/ 2011/04/smart-­‐car.jpg