Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Single Page Applications done right

Single Page Applications done right

BrazilJS 2014

Eduardo Lundgren

August 21, 2014
Tweet

More Decks by Eduardo Lundgren

Other Decks in Technology

Transcript

  1. User Perspective 10 milliseconds can make a really good game

    into a really bad game. 10 milliseconds on an e-commerce can reduce sales. Interval User perspective 0 - 100 milliseconds Instant 100 - 300 milliseconds Perceptible delay 300 - 1000 milliseconds Task Focus, Perceptible delay 1+ second Mental context switch 10+ seconds I'll come back later
  2. 10KB of HTML 3KB of CSS 500Bytes of GIFs Hacker

    News, technically, it's fast First render in <500ms
  3. The page loads really fast, but I can't read anything

    Needs to zoom and scroll The perceived performance is actually 5+ seconds What is the problem?
  4. Actual performance GPU Acceleration RAM Usage Parsers & Lexers Layers

    & Render Trees Multi-Core Optimization Energy constrains
  5. Transport When you ship bytes, needs to execute those bytes.

    Bytes Parse What happens after shipping those bytes? Selector Matching Style Calculation Layout & Painting JavaScript GC Performance Latency Bandwidth Protocols
  6. “Performance is not just milliseconds and bytes. It's also how

    milliseconds and bytes translate to how the user perceives the app” Ilya Grigorik Developer Advocate, Google
  7. “SPA is a web site that fits on a single

    web page with the goal of providing a more fluid user experience”
  8. var xhr = new XMLHttpRequest(); xhr.onload = successFn; xhr.onerror =

    errorFn; xhr.open('GET', '/foo.html'); xhr.send(); Ajax
  9. Content is empty Link is clicked
 Request content using XHR

    Ajax http://trackingjs.com Loading... Some fancy loader appears 1 2 3
  10. var xhr = new XMLHttpRequest(); xhr.onload = successFn; xhr.onerror =

    errorFn; xhr.open('GET', '/foo.html'); xhr.send(); location.hash = '#foo'; Hijax
  11. Content is empty Link is clicked
 Request content using XHR

    Hijax http://trackingjs.com#docs Loading... Some fancy loader appears URL updates with hash bang # 1 4 2 3
  12. History API history.pushState(null, '', 'page02.html') page01.html page02.html page03.html history.replaceState({ key:

    123 }, '', 'page02.html') Navigate between pages without page refresh history.state: { key: 123 }
  13. History API history.pushState(null, '', 'page02.html') page01.html page02.html page03.html history.replaceState({ key:

    123 }, '', 'page02.html') Navigate between pages without page refresh "onpopstate" ‏ history.back(); history.state: null
  14. By using History API you can manipulate the browser history,

    so you can use browser’s back & forward buttons
  15. Once you load a certain surface this content is cached

    in memory and is retrieved later on without any additional request
  16. State retention window.history.state = {}; Memory State object size limits

    (640KB)
 Serialized objects Memory management to avoid leaks
  17. Lifecycle subsequent navigations are handled without a full page load

    Initial Page Loaded additional content is requested via Ajaxy techniques Ajax Request Update the URL Render Page Flip Screen Finalize Request pages are bookmarkable and sharable once content is ready, flip the screen render relevant parts of the page URL Routing makes it easy to wire up route handlers for different application states UI Feedback
  18. <link rel="senna-route" href="regex:.*\.html" type="senna.Screen"> ! <body data-senna> <div id="header" data-senna-surface></div>

    <div id="body" data-senna-surface></div> <div id="footer" data-senna-surface></div> </body> Data attributes
  19. Less Bandwidth Data transfered (KB) 0 350 700 1050 1400

    Page 1 Page 2 Page 3 Page 4 Refresh SPA
  20. Low Cost USD USD 20 40 USD 40 USD 100

    $30 USD 50 Monthly broadband subscription Oxford Internet Institute
  21. Main features SEO, Bookmarkability Hybrid rendering State retention Timeout detection

    Pending navigation UI Feedback History navigation Cacheable screens Resources management