Offline-First
The painless way
Who is this guy?
MarcelKalveram
IWorkatHanno
Iamfromgermany
ILiveinValencia/Spain
javaScript’ingsincearound2008
RemoteUXTeam
weworkwithstartups
socialbusiness
hanno.co
@wearehanno
@marcelkalveram
Do we really need offline-first?
Traveling Commuting
Roaming Internetoutage saturatednetwork
What does offline-first mean?
Tomanagedatainanappinsuchaway
thatwedon'tneedserveraccessinorderto
doanykindofoperation
some well-considered
s
What is this talk about?
Native
apps
Web
sites
Web apps
Look stuff upDo stuff
When does it make sense?
content-heavysites(Nativeapps)
singlepagewebapps
staticsites
real-timeapps
self-containedapps
not our business
What are the benefits?
Nodata-lossfortheuser
Offline-firstappsarefaster…
appsareusableallthetime
HOW
to do offline-first…?
Offline challenges
cachingdynamicassets
Cachingstaticassets
PrepareUIforofflinestate
xhr, remote api calls
.json, .xml, .csv
xhr, remote api calls
Caching mechanisms
staticdata dynamicdata
page assets
.html, .css, .jpg, .js, .ttf
page assets
browsercache
serviceworkers
appcache
vs.
indexedDB
webSql
webstorage
browsercache
serviceworkers
appcache
Caching mechanisms
can’t trust him-
lack of browser support-
staticdata
page assets
.html, .css, .jpg, .js, .ttf
page assets
indexedDB
webSql
webstorage
Caching mechanisms
localforage
pouchDB
}lack of browser support -
api is overly complex -
dynamicdata
xhr, remote api calls
.json, .xml, .csv
xhr, remote api calls
Challenge #1
Caching static assets
App cache
appcache
intercepts http request
manifestfile
<html manifest="my.appcache">
cache,networkandfallback user
browser
returns cached data
server
server
App cache
cache,networkandfallback
resources can be
used offline.
online whitelist substitute non-
cached resources
appcache
user
browser
app.js
image.png
font.ttf
Manifestr
js
*
cache
network
fallback
-
App cache gotchas
1.filesalwayscomefromthecache
server side generated websites
single page applications
2.onlyupdatesifmanifestfilechange
3.needtoswapthecachemanually
serverappcache
user
browser
applicationCache.swapCache();
Browser support for…appcache
10+ 4+ 3.5+4+ 2.1+ 3.2+
Browser support for…service workers
n/a n/a n/a40+ n/a n/a
Challenge #2
caching dynamic assets
Caching mechanisms
dynamicdata
xhr, remote api calls
.json, .xml, .csv
xhr, remote api calls
indexedDB
webSql
webstorage
localforage
pouchDB
}lack of browser support -
api is overly complex -
Using web storage
no online
connection required
sessionstorage or localstorage
key/value store
appcachestorage
user
browser
Using web storage APIs
currentHighscore
lastMove
levelsCompleted
1890
D4E5
12
keys values
Using web storage APIs
localStorage.setItem("lastMove", "D4E5");
localStorage.getItem("levelsCompleted");
currentHighscore
lastMove
levelsCompleted
1890
D4E5
12
Browser support for…localstorage
8+ 3.5+ 4+4+ 2.1+ 3.2+
asynchronous
Why don’t we just use…indexedDB?
webstorage indexedDB
synchronous
limited to strings supports large data sets
no indexes indexes
10mb storage 50mb storage
Browser support for…indexedDB?
*Subfeatures not supported
**buggy behavior in iOS8
10+ 10+ 7.1+23+ 4.4+ 8+* **
Browser support for… webSQL?
*is no longer being maintained
*
n/a n/a 3.1+4+ 2.1+ 3.2+
browser APIs…
localStorage
webSql
indexedDB
limited storage options
lack of browser support
overly complex api
localForage…
…limitationsoflocalStorage
helps us overcome
…browserdifferences
supports all js objects
asynchronous
indexedDB, webSQL and localstorage
loads best driver for us
json
currentHighscore
lastMove
levelsCompleted
1890
12
localForage
localForage.setItem("lastMove", incrediblyComplexJsonObject)
localForage.getItem("levelsCompleted", function(err, val) {…});
takes care of
(de-)serialization
.then(…)
supports promises
https://github.com/mozilla/localForage
What about syncing?
localstorage
indexedDB
localforage
our responsibility
What about syncing?
localstorage
indexedDB
localforage
pouchDB
our responsibility
object
pouchDB API
pdb.put(o, cb) pdb.get(id, cb)
currentHighscore
lastMove
levelsCompleted
1890
12
_id: "lastMove",
lastPos: "D4"
newPos: "E5"
"lastMove"
pouchDB sync
pdb.sync(remoteDB);
new PouchDB("http://localhost:5984/myremotedb")
https://github.com/pouchdb/pouchdb
Challenge #3
preparing our ui
yes
no
no
yes
ami
online…
doihave
updates…
syncwith
server
offline
app
store
locally
How to detect online status
1
navigator
events
2
appcache
events
3
xhr
events
Navigator events
window.addEventListener("online", function(e) {alert("online");})
online/offline
navigator.onLine
true/false
Navigator events
is always onlineworks only
in offline-modeshows online even if
connection is unreliable
Appcache events
applicationCache.addEventListener("error", function(e) { … });
checking progress
cached
error
XHR events
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(e) {
}
if (xhr.status != 200) {
}
thereisprobablyaconnectionissue
https://github.com/HubSpot/offline
static and dynamic data
with remote server
what to take offline
if offline-first makes sense
useevents
prepareui for flawless user experience
consider
decide
to detect offline state
cache
sync
with your users
some good in the world
empathize
Do
Thanks!
hanno.co

Offline first, the painless way