offline webapps

38
Taking Your Web Apps Offline Mike Nitchie

Upload: mnitchie

Post on 19-Aug-2015

128 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Offline Webapps

Taking Your Web Apps Offline

Mike Nitchie

Page 2: Offline Webapps

Why Offline?

Page 3: Offline Webapps

The lines between web, desktop, and mobile apps

are being increasingly blurred.[citation needed]

Page 4: Offline Webapps

HTML5 Puppy to the Rescue

Page 5: Offline Webapps

My Relationship with Application Cache

Pictured from Left to Right: Me, Appcache

Page 6: Offline Webapps

All is Not Lost

Page 7: Offline Webapps

Let’s talk about how to use it.

Let’s talk about when to use it.

Let’s talk about how to improve it.

Page 8: Offline Webapps

www.mikenitchie.com

github.com/mnitchie

@mnitchie

[email protected]

Page 9: Offline Webapps

Cat slap 1

Setting up a local development environment.

localhost is a loopback…

From Wikipedia

It is implemented entirely within the operating system's networking software and passes no packets to any network interface controller. Any traffic that a computer program sends to a loopback IP address is simply and immediately passed back up the network software stack as if it had been received from another device.

Page 10: Offline Webapps

?

Page 11: Offline Webapps

Mac: /etc/hosts

Windows: c:\Windows\System32\Drivers\etc\hosts

[localIP] [dummy.domain]

192.168.1.10 offline.me

Page 12: Offline Webapps

Demos!

www.mikenitchie.com

github.com/mnitchie/OfflineDemo1/

github.com/mnitchie/OfflineDemo2/

Page 13: Offline Webapps

Cats

lap

2

The cache manifest file IS

the app. If a resource isn’t

listed there, it’s not available to

the app.

Page 14: Offline Webapps

Catslap 3

… Except for the first time you visit the page. Then, it pulls everything from the server.

Page 15: Offline Webapps
Page 16: Offline Webapps
Page 17: Offline Webapps

Catslap 4

After the first caching, resources

will always be loaded from cache. Never the server.

Until the manifest file itself changes…

Page 18: Offline Webapps

Cats

lap

5

Cache-control headers… So much nerd rage.

cache-control:’no-cache, max-age=0’

Expires: Date.now()

Page 19: Offline Webapps

Catslap 6

Using a cache manifest is great for performance,

except…

Page 20: Offline Webapps

Back to the Demos!

Page 21: Offline Webapps

Let’s Crowd-Source Some Facts

http://bit.ly/1DrQegP

Page 22: Offline Webapps

> Passive Cache

FALLBACK:

/ /offline.html

NETWORK:

*

Page 23: Offline Webapps

> Application Cache API

Methods

void update()

void swapCache()

void abort()

Page 24: Offline Webapps

> Application Cache API

Events

checking

error

noupdate

downloading

progress

updateready

cached

obsolete

Page 25: Offline Webapps

> Application Cache API

States

uncached (0)

idle (1)

checking (2)

downloading (3)

updateready (4)

obsolete (5)

Page 26: Offline Webapps

MOAR DEMOS!

Page 27: Offline Webapps

> LocalStorage

void setItem(String key, String data)

String key(int)

String getItem(String key)

void removeItem(String key)

void clear()

Page 28: Offline Webapps

Syncing

Send all new, modified, and deleted data to the server.

After that operation completes, load all relevent state from the server and replace the local state with the server state.

Page 29: Offline Webapps

Syn

cin

g

• Give the object a unique identifier• Once synced with the server, replace with the server-

assigned ID• Set an isNew flag or specify a pattern in the ID which

implicitly indicates that the object is new

CREATED

• Set an isDirty flag

MODIFIED

• Set an isDeleted flag• If the object isNew when isDeleted is set, immediately

remove it from local storage.• Otherwise, sync it to the server when next online.

DELETED

Page 30: Offline Webapps

How Hard Can It Be?

The user could refresh during the sync operation.

An ajax call could fail.

The user could make a change while the sync operation is

occurring.

The user could lose network connectivity while the sync

operation is ocurring.

Page 31: Offline Webapps

Conflict Resolution

PREFER NEWEST

GUIDED MERGE

AUTOMATIC MERGE

VERSIONING

Page 32: Offline Webapps

navigator

navigator.onLine

false if the user is definitely offline.

true if the user might be online.

Page 33: Offline Webapps

PouchDB and CouchDB

Page 34: Offline Webapps

PouchDB/CouchDBFrom pouchdb.com

PouchDB is an open-source JavaScript database inspired by Apache CouchDB that is designed to run well within the browser.

PouchDB was created to help web developers build applications that work as well offline as they do online.

It enables applications to store data locally while offline, then synchronize it with CouchDB and compatible servers when the application is back online, keeping the user's data in sync no matter where they next login.

Page 35: Offline Webapps

> PouchDB API

var db = new Pouch(‘mydb’),

remoteDB = http://api.example.com/db

db.post()

db.put()

db.get()

db.remove()

db.sync(remoteDB)

Page 36: Offline Webapps

When does

offline make

sense?

Straight-forward “look-stuff-up” or “do-stuff” applications.

When it is explicitly requested by a customer.

When it is a better alternative to writing the app for multiple platforms.

When we are absolutely positively sure that our users will know it is there and take advantage of it.

Page 37: Offline Webapps

What’s the way forward?

Always show the most up-to-date version when online

Only update changed resources

Give programatic control over how much can be stored in the appcache.

Give programatic control over the contents of the appcache.

Better user awareness

Perhaps less-sandboxed browser designed for delivering applications

Complaints are just feature requests in disguise.

Page 38: Offline Webapps

???