28
Updates on Offline “My AppCache won’t come back” and “ServiceWorker Tricks for Cache” Natasha Rooney Web Technologist GSM Association @thisNatasha 1

FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

Embed Size (px)

DESCRIPTION

New updated version of my Offline presentation detailing the new awesomeness of ServiceWorker!

Citation preview

Page 1: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

Updates on Offline“My AppCache won’t come back” and “ServiceWorker Tricks for Cache”

Natasha RooneyWeb TechnologistGSM Association@thisNatasha

1

Page 2: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

1 boring slide about me.Aw, do we have to?Yup! It’s relevant (sorta)

2

http://www.w3.org/Mobile/IG/

@thisNatasha

Page 3: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

1 boring slide about me.Aw, do we have to?Yup! It’s relevant (sorta)

2

http://www.w3.org/Mobile/IG/

Offline

Web Apps

@thisNatasha

Page 4: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

totemo CONFUSING desu ne?

紛らわしいからですね!

3 @thisNatasha

Page 5: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

totemo CONFUSING desu ne?

紛らわしいからですね!

3

Web StorageKey-value mapping

IndexedDBKey-value mapping +

indexes

File AccessReading file content

New HTML5 APIs

@thisNatasha

Page 6: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

totemo CONFUSING desu ne?

紛らわしいからですね!

3

App CacheBRAND NEW cache

mechanism of wonder which will answer all your questions of life’s worries and fulfill your every day

with the realisation of your dreams of riches, love, intelligence, regular full

nights of sleep and a real life Jurassic Park...

New HTML5 APIs

@thisNatasha

Page 7: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

4

https://marketplace.firefox.com/developers/docs/apps_offline

App Cache: COOL! How do I do it!?

@thisNatasha

Page 8: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

4

Manifest File

But understanding App Cache comes with an interesting story…

Content vs. Shell...

App Cache: COOL! How do I do it!?

@thisNatasha

Page 9: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

5

shell

content

@thisNatasha

Page 10: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

6

content shell

@thisNatasha

Page 11: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

Use Case mismatch lead to...

7

App Cache is a Douchebag Gotchas1. Files always come from the App Cache, even if you are online2. The App only updates if the manifest file changes3. The App Cache is an additional cache, not an alternative one4. Never future-cache the manifest5. Non cached resources (e.g. images) will not load in a cached

page6. No more conditional downloads, including responsive images7. No idea how the fallback page works8. Redirects to other domains are treated like failures

Source: http://alistapart.com/article/application-cache-is-a-douchebag

@thisNatasha

Page 12: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

The moral of the story...

DO IT!

ShellJavascript, CSS and HTML for the shell

● Fonts● Sprites● Splash screen images

(Guidance from FT Labs)

DON’T DO IT!

Content!

Disclaimer: Still a lot of opinions in this area!

8 @thisNatasha

Page 13: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

Another solution?

9 @thisNatasha

Page 14: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

ServiceWorker!10

Source: https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md

➔ New API➔ Power to the dev!➔ Apps, not pages - multi caches

Some quick interesting bits:➔ Documents and Service Workers are BFFs➔ Default Fallback: Normal resource loading➔ Wildcards are OK➔ 2 Service Workers are OK (default is last one)➔ Inception is NOT OK

Register

@thisNatasha

Page 15: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

ServiceWorker: ctrl.js

11

Source: https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md

@thisNatasha

Page 16: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

ServiceWorker: Caching!

12

Source: https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md

@thisNatasha

Page 17: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

BREAK: Promises

① History➔ Multilisp➔ Java Futures

BREAK

Page 18: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

BREAK: Promises

① Current Complexity

BREAK

Source: http://www.html5rocks.com/en/tutorials/async/deferred/

Page 19: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

BREAK: Promises

① New Promises

BREAK

Source: http://www.html5rocks.com/en/tutorials/async/deferred/

Page 20: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

ServiceWorker: Caching!

12

Source: https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md

@thisNatasha

Page 21: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

ServiceWorker: Caching

13

Source: https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md

cont...

@thisNatasha

Page 22: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

cont...

ServiceWorker: Caching

Also use ➔ e.respondWith(this.caches.match("shell-v1", e.request.url));

13

Source: https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md

@thisNatasha

Page 23: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

Offline? Yeah!

14 @thisNatasha

Page 24: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

ServiceWorker: Fallbacks

15

Source: https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md

@thisNatasha

Page 25: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

ServiceWorker: RULES

16

Source: https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md

1. Documents and Service workers are BFFs2. ServiceWorkers should be written to avoid holding global

state3. New tab means same ServiceWorker4. Version your ServiceWorkers!5. ServiceWorkers cannot be served on CDNs6. CORS7. importScripts() that run before oninstall finishes will go in

browser cache8. Use Cache-Control: no-cache or Cache-Control: private

@thisNatasha

Page 26: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

The bits I REALLY like...

Some cool stuff about ServiceWorkers

➔ Fallbacks for fallbacks for fallbacks➔ It’s 100% or nothing➔ “User chooses” or “dev chooses” both OK!➔ Work with Quota API➔ Works with Installed Apps

What about Network Information APIs?

17 @thisNatasha

Page 27: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

What we learnt...Did we learn anything?!

- The W3C cares about mobile!

- Offline solutions are confusing

- App Cache was not fit for purpose

- Shell and Content

- Service WorkerOffline as a side effectPromisesBit of rule followingmulti cache, multi fallback

18 @thisNatasha

Page 28: FirefoxOS Meetup - Updates on Offline in HTML5 Web Apps

Thanks!

19

Especially...

Alex Russell | Google@slighltylate

Jake Archibald | Google@jaffathecake

Jonas Sicking | Mozilla @SickingJ

Jeremy Chone | Britesnow@JeremyChone

@thisNatasha