The upcoming HTML5 feature known as Service Worker is set to turn the web on its head, enabling websites to run without an internet connection and to load quickly even when the connection is bad.
But with that evolutionary step, web applications need to become more complex, and to handle the fact that their source code may remain on a device for much longer than a single session. Without the safety net of starting fresh every time the user hits a link, considerations such as memory management, data consistency, encapsulation and backwards compatibility now come into play. Front-end developers are suddenly fully-fledged software engineers! The web page is our app platform, and the browser our runtime.
The fastest byte is a byte not sent, so the fastest website is the website that doesn’t need to load anything. Step with me into a world of good hacks, unbeatable performance and smart, multivariate responsiveness, drawn from experience building web apps for the FT and The Economist.
18. AppCache Hacks
• Varying the response of every page based on whether a cookie is set
(hack needs to be added to our CDN as well as back end)
• Returning an HTTP error on the manifest request when that cookie is
not set;
• Created an iframe to load a page that loads the manifest rather
than referencing it directly.
• Without the NETWORK: * hack in your AppCache manifest you will
break all non-offline'd resources.
• Putting all our API methods on a path starting with /api so that our
FALLBACK doesn’t interfere with ajax requests.
• Returning different Cache-Control headers on Internet Explorer 10+.
23. var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
alert(xhr.responseText);
} else {
alert('There was a problem.');
}
}
};
xhr.ontimeout = function() {
alert('There was a problem.');
};
xhr.open('GET', 'http://a.b/c.html');
with XMLHttpRequest
24. fetch('http://a.b/c.html')
.then(function(response) {
return response.text(); // can be .json, .blob
})
.then(function(text) {
alert(text);
})
.catch(function() {
alert('There was a problem.');
});
with Fetch API
25. Using Fetch
Native within Service Workers only
Browser-only polyfill:-
github.com/github/fetch
‘Isomorphic’ Fetch Polyfill that works in
Node and in the browser via
Browserify:-
bit.ly/iso-fetch
30. Put ‘fake’ responses into the Cache
WARNING: the difference between ‘add’ & ’put’ is
different to the difference between IndexedDB’s
‘add’ & ‘put’
add put
IndexedDB INSERT INSERT OR UPDATE
Service Worker
Caches
INSERT OR UPDATE INSERT OR UPDATE
31. caches.open(‘my-app’)
.then(function(cache) {
return cache.keys();
})
.then(function(keys) {
return cache.addAll(keys);
});
Update content in the Cache
WARNING: This will ‘break’ any ‘fake requests’ that have
been ‘put’ted
WARNING: The Service Worker Cache is in addition to
the regular HTTP Cache.
42. No Caches
The Service Worker Cache API does
not work yet but a Polyfill exists that
uses IndexedDB underneath:-
bit.ly/caches-polyfill
43. Storage Limits
• Every origin (protocol+domain+port) gets an
allowance of offline storage, typically 50mb
• Allowance shared between IndexedDB &
Service Workers
• Each origin only has access to data in its
caches & databases.
44. Some JavaScript APIs are not
available in Service Worker:-
• LocalStorage
• Synchronous ajax
48. Advanced Offline
• Simple Caches not always enough
• Use IndexedDB for more structured
data, e.g. user preferences, bookmarks
• Don’t use WebSQL it’s deprecated, use
the polyfill. bit.ly/idbpolyfill
49. • Origins can have multiple IndexedDB
databases
• Each database can have multiple ‘object
stores’ (like SQL tables)
• An object store must have at least one
index – IDB is NoSQL – can only be queried
by pre-defined indexes
• No free text search
52. function databaseOpen() {!
! var db = new Dexie('todos');!
! db.version(1).stores({ todo: '_id' });!
! return db.open();!
}
with Dexie.org
53. Summary
• Service Worker is coming so get ready:-
https, Promises, Caches Polyfill …
• Use the Fetch API in your applications today
bit.ly/iso-fetch / github.com/github/fetch
• Want to use IDB? Use dexie.org. Want to support
WebSQL? Use the polyfill bit.ly/idbpolyfill.
• I’ve written tutorials on how to use all the HTML5 offline
tech available for free here:-
bit.ly/offline-workshop
54. Thanks!
• Open Source:-
github.com/ftlabs
FastClick, Polyfill.io, DOM Delegate, Ellipsis, & more
• Jobs:-
labs.ft.com/jobs
• Me:-
@andrewsmatt / mattandre.ws