Abusing Javascript to speedup mobile web sites

18
Abusing Javascript to speedup mobile web sites.

description

A few ways to speedup mobile web sites using different caching techniques.

Transcript of Abusing Javascript to speedup mobile web sites

Page 1: Abusing Javascript to speedup mobile web sites

Abusing Javascript to speedup mobile web sites.

Page 2: Abusing Javascript to speedup mobile web sites

Mobile browsers are dog slow

Only if they are used to render web sites designed for desktop browsers.

One code for any browser and device is actually a dream.

Bad code tends to have more impact on mobile browsers.

Page 3: Abusing Javascript to speedup mobile web sites

Radio networks are slow

The main issue is not speed but latency.

4 Mb downloaded over a single request: 2 seconds.

Same data downloaded as 20 small files: 8 seconds.

Page 4: Abusing Javascript to speedup mobile web sites

A 304 reply code is nearly as bad as a 200

404 replies are rarely cached. Kill them all.

Proper caching of anything cacheable is required

Don't overestimate the browser cache and HTTP keep-alive.

Page 5: Abusing Javascript to speedup mobile web sites

HTML5 cache manifest

Doesn't bring much over proper HTTP caching for online apps.

Everything in the manifest is preloaded even when useless for the current page. Double-sided sword.

Requests from a cached page have no referrer.

No granularity: everything depends on the freshness of the manifest.

Page 6: Abusing Javascript to speedup mobile web sites

Mobile browsers have unpredicable caching

strategy

Even on the same operating system

iOS 4.1 on iPhone 3G doesn't cache some tiny content that could really be cached. And that the same OS on other devices does.

Page 7: Abusing Javascript to speedup mobile web sites

JavaScript to the rescue!

Page 8: Abusing Javascript to speedup mobile web sites

Slower may feel faster

Mobile devices have small screens.

Load visible content first, and use XHR to load the rest.

Don't wait until the user scrolls, though. Latency + tendency to scroll and zoom on mobile devices would make the page feel slow.

Provide feedback.

Page 9: Abusing Javascript to speedup mobile web sites

JS + HTTP caching = not so bad

Don't repeat yourself: use static JS to render common, static fragments.

Use XHR to render dynamic parts that are not immediately visible.

Different parts can have different expiration.

Page 10: Abusing Javascript to speedup mobile web sites

HTTP cache is quite unpredictable

No control over actual eviction.

Might feel good, even for large requests, until everything is evicted.

The server controls the cache, not the client (which could come in handy to avoid inconsistencies).

Page 11: Abusing Javascript to speedup mobile web sites

Localstorage is the real shit

Seamlessly keeps over 2.5 Mb of data per domain.

Mostly persistent.

Provides a flexible client-controlled cache for any resource.

Trivial to implement.

Page 12: Abusing Javascript to speedup mobile web sites

if (window.localStorage["geoapi_js-v5"]) { window.eval(window.localStorage["geoapi_js-v5"]);} else { var xhr = new XMLHttpRequest; xhr.onreadystatechange = function() {  if (this.readyState != 4) {    return;  }  if (this.status != 200) {    alert("Erreur reseau: " + this.status);    return;  }  try {    window.localStorage["geoapi_js-v5"] = this.responseText;  } catch (e) { }  window.eval(this.responseText); }; xhr.open("GET", "geoapi-v5.js", true); xhr.send();}

Page 13: Abusing Javascript to speedup mobile web sites

Client-side fragment caching

Dramatically improves performance of a non-AJAXified page with minor changes.

Use localstorage to store common fragments.

Tell the server about known fragments so that only identifiers from the previous page are sent, in place of the actual content.

Page 14: Abusing Javascript to speedup mobile web sites

<!doctype html><html><head> <title>Transparent client-side fragment cache demo</title></head><body><!--fragment navbar-290d996311209f1897516b2caa2cc611--> <nav> Navigation bar </nav><!--/fragment--><!--fragment ads-bd779001f9cad4bfb74e563eb6bbf5c5--> <div id="ads"> Ads </div><!--/fragment--> <section id="hey"> I ain't no <a href="/">fragment</a>! </section> <script src="disco.js"></script></body></html>

Page 15: Abusing Javascript to speedup mobile web sites

<section id="hey"> I ain't no <a href="/">fragment</a>! </section>

gets dynamically rewritten as:

<section id="hey"> I ain't no <a href="/?_fragments=navbar-290d996311209f1897516b2caa2cc611+ads-bd779001f9cad4bfb74e563eb6bbf5c5">fragment</a>! </section>

Page 16: Abusing Javascript to speedup mobile web sites

<!doctype html><html><head> <title>Transparent client-side fragment cache demo</title></head><body><!--cached navbar-290d996311209f1897516b2caa2cc611--><!--cached ads-bd779001f9cad4bfb74e563eb6bbf5c5--> <section id="hey"> I ain't no <a href="/">fragment</a>! </section> <script src="disco.js"></script></body></html>

Page 17: Abusing Javascript to speedup mobile web sites

http://00f.net/2010/09/22/transparent-client-side-

fragment-cache/

Page 18: Abusing Javascript to speedup mobile web sites

Frank@jedisct1