Skip to content

Instantly share code, notes, and snippets.

@mattgreenfield
Last active November 13, 2017 16:28
Show Gist options
  • Save mattgreenfield/2a6c1c5475a7a847b6f592ffe47e527b to your computer and use it in GitHub Desktop.
Save mattgreenfield/2a6c1c5475a7a847b6f592ffe47e527b to your computer and use it in GitHub Desktop.
Notes from my 2 days of PWA training with Google, to share with my team at gene.co.uk

Day 1

Why pwa? Similar to our internal presentation

A PWA needs to be responsive

  • Responsive images - srcset, picture element
  • meta viewport tag
  • media queries

A PWA needs to be on HTTPS

  • unless it's local host

First, we need to understand ES6

Arrow functions

  • beware of this

Promises

  • .then()
  • .catch()
  • these can be chained
  • promise.race() is cool also

Fetch

  • .fetch()
  • a replacement for XMLHttpRequest, like jQuery ajax
  • careful - 404 is a valid response, check response.ok == true or response == 200
  • http://myjson.com/ is pretty neat

We talked about IIFE's.

Serviceworker.js Runs on a seperate 'thread' to the main javascript. Three steps required to get this working 1.Register 2.Install 3.Activate It can do all sorts of things, we'll come on to that later.

Lighthouse

The tool in chrome, it's also available on the command line and the abilty so save reports as HTML might come in handy.

manifest.json

full of metadata about your site, the Android toolbar color is a cool one. Meta tag fallbacks

Work Box

Gulp/webpack task to automate creation of serviceworker.js

Day 2

A quick overview of fetch and promises. .catch(), .then() (promsies) and .fetch() are everywhere in service workers.

It might be worth looking at some code that I wrote the next day, with some very simple implementation https://github.com/mattgreenfield/the-blues-issue/compare/pwa And Hob made his portfolio a PWA - https://hobadams.com/

Cache

caches can be set, updated, and read from anywhere in your javascript (does it have to be in the service worker?). Managaing the cache can be done on any event, so a service worked 'register' event, or something more familiar like a 'click' event. This allows things to only be cached once they are used and prevent unnecessary downloads.

How you manage cache is up to you and there are many statergies depening on the type of data you are caching and how it is used.

  • Cache -> fallback to network
  • network -> fallback to cache
  • cache and network. Cache initially, replaced with network content onces downloaded Hob makes a good point that maybe browsers will one day have intelligent defaults.

indexedDB

...is an 'object store' in the browser, we can store json, files, blobs When online, it might be usefull to store things like progress in a game, or items in a todo list. But when using it with a PWA, it's useful for storing shop categories/products. It comes in to it's own with headless as we don't want to be caching HTML pages anymore. We cache the code to make the page, and the data we pass to that code. 'indexedDB promised' is a library that makes indexedDB easier to work with. Apparently. https://github.com/jakearchibald/idb

Google Analytics

There are some new events that you may wish to start tracking with GA

  • Notifications enabled, seen, dismissed
  • Add to home screen

You cant use the standard GA JS functions when offline. You can't store them for sening once online either. GA can also be used by posting to a URL, and this can be used offiline. The 'sw-offline-google-analyitics' module is useful. It's included in WorkBox.

Notifications

Notifications are pretty simple to make.

You can set up a server to send push notifications, but there are plenty of services that will do this for you, AWS, google cloud messaging.

Notifications are best when they are relvant to the user and have context. e.g. 'Check in to your flight BA123 to London', not 'Online check in is available'

Some techniques for moving to a PWA

PWA's can be added progressively

  • just add a 'add to home screen link'
  • just cache the 'app shell'
  • just add notifications
  • cache a few files but not everything
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment