Slide 1

Slide 1 text

Software Developer, Newlogic Arnelle Balane @arnellebalane Building PWAs using Workbox

Slide 2

Slide 2 text

Arnelle Balane Software Developer at Newlogic Google Developers Expert for Web Technologies I write about Web stuff on my blog, arnellebalane.com @arnellebalane

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Progressive Web Apps Websites built using Web technologies, and act and feel like native apps

Slide 5

Slide 5 text

❖ Can be installed to the home screen ❖ Works well even in intermittent or unreliable network conditions ❖ Works on any device ❖ Send notifications even when app is closed Progressive Web Apps

Slide 6

Slide 6 text

❖ Usually built using the application shell model, minimizing page refresh ❖ Generally easier to deploy and maintain than native apps ❖ SEO, URLs, etc. Progressive Web Apps

Slide 7

Slide 7 text

Service Workers if (navigator.serviceWorker) { window.addEventListener('load', () => { navigator.serviceWorker.register('/sw.js'); }); }

Slide 8

Slide 8 text

Service Workers // sw.js addEventListener('install', event => { /**/ }); addEventListener('activate', event => { /**/ }); addEventListener('fetch', event => { /**/ });

Slide 9

Slide 9 text

❖ Service Workers ❖ Cache API ❖ Push Notifications ❖ Background Sync ❖ Background Fetch ❖ Web Application Manifest Moar APIs for our PWAs!

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Workbox JavaScript libraries for adding offline support to Web applications

Slide 12

Slide 12 text

❖ from CDN ❖ Node module ❖ Webpack plugin Using Workbox

Slide 13

Slide 13 text

Credits to Afrian Hanafi dribbble.com/shots/13125619-Makers-Workshop-Concept-II

Slide 14

Slide 14 text

Credits to Afrian Hanafi dribbble.com/shots/13125619-Makers-Workshop-Concept-II 2 pages 2 images 1 stylesheet 1 script

Slide 15

Slide 15 text

Import the library // sw.js importScripts('https://storage.googleapis.com/workbox-cdn/ releases/5.1.2/workbox-sw.js');

Slide 16

Slide 16 text

Precache site assets (deprecated) workbox.precaching.precache([ '/', '/classes', '/index.css', '/index.js', '/images/image-01.jpg', '/images/image-02.jpg' ]);

Slide 17

Slide 17 text

Precache site assets, with versioning workbox.precaching.precache([ {url: '/', revision: 'v1'}, {url: '/classes', revision: 'v1'}, {url: '/index.css', revision: 'v1'}, {url: '/index.js', revision: 'v1'}, {url: '/images/image-01.jpg', revision: 'v1'}, {url: '/images/image-02.jpg', revision: 'v1'} ]);

Slide 18

Slide 18 text

Verify cache in Chrome Devtools

Slide 19

Slide 19 text

Y u no work?

Slide 20

Slide 20 text

Route precached assets workbox.precaching.precache([ /* ... */ ]); workbox.precaching.addRoute();

Slide 21

Slide 21 text

Precache and route site assets workbox.precaching.precacheAndRoute([ {url: '/', revision: 'v1'}, {url: '/classes', revision: 'v1'}, {url: '/index.css', revision: 'v1'}, {url: '/index.js', revision: 'v1'}, {url: '/images/image-01.jpg', revision: 'v1'}, {url: '/images/image-02.jpg', revision: 'v1'} ]);

Slide 22

Slide 22 text

Now it works offline!

Slide 23

Slide 23 text

Precaching Adding items to the cache in advance, even before they are used

Slide 24

Slide 24 text

Gradually adding items to the cache as they are requested Runtime caching

Slide 25

Slide 25 text

Cache a specific URL at runtime workbox.routing.registerRoute( '/images/image-01.jpg', new workbox.strategies.NetworkFirst() );

Slide 26

Slide 26 text

Verify cache in Chrome Devtools

Slide 27

Slide 27 text

Cache a URL pattern at runtime workbox.routing.registerRoute( /\.jpg$/, new workbox.strategies.NetworkFirst() );

Slide 28

Slide 28 text

Runtime caching using a “match callback” workbox.routing.registerRoute( (context) => context.url.origin === 'https://arnelle.me', new workbox.strategies.NetworkFirst() ); workbox.routing.registerRoute( (context) => context.request.destination === 'image', new workbox.strategies.NetworkFirst() );

Slide 29

Slide 29 text

Runtime caching https://developers.google.com/web/tools/workbox/reference-docs /latest/module-workbox-routing Precaching https://developers.google.com/web/tools/workbox/reference-docs /latest/module-workbox-precaching More Info

Slide 30

Slide 30 text

Workbox provides simple implementations for common service worker caching strategies Caching strategies

Slide 31

Slide 31 text

Network-First Image from web.dev

Slide 32

Slide 32 text

Network-First workbox.routing.registerRoute( /\.jpg$/, new workbox.strategies.NetworkFirst() );

Slide 33

Slide 33 text

Network-First workbox.routing.registerRoute( /\.jpg$/, new workbox.strategies.NetworkFirst({ networkTimeoutSeconds: 3 }) );

Slide 34

Slide 34 text

Cache-First Image from web.dev

Slide 35

Slide 35 text

Cache-First workbox.routing.registerRoute( /\.css$/, new workbox.strategies.CacheFirst() );

Slide 36

Slide 36 text

Stale-While-Revalidate Image from web.dev

Slide 37

Slide 37 text

Stale-While-Revalidate workbox.routing.registerRoute( (context) => context.url.pathname.startsWith('/blog'), new workbox.strategies.StaleWhileRevalidate() );

Slide 38

Slide 38 text

❖ Nothing special other than they can use Workbox Plugins Cache-Only Network-Only

Slide 39

Slide 39 text

The Offline Cookbook https://developers.google.com/web/fundamentals/instant-and -offline/offline-cookbook Workbox Strategies https://developers.google.com/web/tools/workbox/reference-docs /latest/module-workbox-strategies More Info

Slide 40

Slide 40 text

Additional service worker behaviours without writing more boilerplate code Workbox plugins

Slide 41

Slide 41 text

❖ ExpirationPlugin ❖ BroadcastUpdatePlugin ❖ BackgroundSyncPlugin ❖ CacheableResponsePlugin ❖ RangeRequestsPlugin Workbox plugins

Slide 42

Slide 42 text

Using plugins workbox.routing.registerRoute( /\.jpg$/, new workbox.strategies.CacheFirst({ plugins: [ /* plugins here */ ] }) );

Slide 43

Slide 43 text

ExpirationPlugin, expire after some time workbox.routing.registerRoute( /\.jpg$/, new workbox.strategies.CacheFirst({ cacheName: 'image-cache', plugins: [ new workbox.expiration.ExpirationPlugin({ maxAgeSeconds: 10 }) ] }) );

Slide 44

Slide 44 text

ExpirationPlugin, custom cache name workbox.routing.registerRoute( /\.jpg$/, new workbox.strategies.CacheFirst({ cacheName: 'image-cache', plugins: [ new workbox.expiration.ExpirationPlugin({ maxAgeSeconds: 10 }) ] }) );

Slide 45

Slide 45 text

ExpirationPlugin, limit number of entries workbox.routing.registerRoute( /\.jpg$/, new workbox.strategies.CacheFirst({ cacheName: 'image-cache', plugins: [ new workbox.expiration.ExpirationPlugin({ maxEntries: 1 }) ] }) );

Slide 46

Slide 46 text

ExpirationPlugin

Slide 47

Slide 47 text

ExpirationPlugin

Slide 48

Slide 48 text

BroadcastUpdatePlugin workbox.routing.registerRoute( /\/blog\/.+$/, new workbox.strategies.StaleWhileRevalidate({ plugins: [ new workbox.broadcastUpdate.BroadcastUpdatePlugin() ] }) );

Slide 49

Slide 49 text

BroadcastUpdatePlugin, handle in main page // index.js navigator.serviceWorker.onmessage = event => { if (event.data.meta === 'workbox-broadcast-update') { displayUpdateAvailableUI(); } };

Slide 50

Slide 50 text

BroadcastUpdatePlugin, handle in main page // index.js navigator.serviceWorker.onmessage = event => { if (event.data.meta === 'workbox-broadcast-update') { displayUpdateAvailableUI(); } };

Slide 51

Slide 51 text

BroadcastUpdatePlugin

Slide 52

Slide 52 text

Custom Plugins https://developers.google.com/web/tools/workbox/guides /using-plugins#custom_plugins Using Plugins https://developers.google.com/web/tools/workbox/guides /using-plugins More Info

Slide 53

Slide 53 text

Use Workbox with popular JavaScript frameworks. Webpack plugin

Slide 54

Slide 54 text

Install dependencies npm install -D workbox-webpack-plugin

Slide 55

Slide 55 text

GenerateSW plugin const {GenerateSW} = require('workbox-webpack-plugin'); module.exports = { /* ... */ plugins: [ new GenerateSW({ swDest: 'sw.js' }) ] };

Slide 56

Slide 56 text

InjectManifest plugin const {InjectManifest} = require('workbox-webpack-plugin'); module.exports = { /* ... */ plugins: [ new InjectManifest({ swSrc: './www/sw.js' }) ] };

Slide 57

Slide 57 text

InjectManifest plugin // sw.js importScripts('https://storage.googleapis.com/...'); workbox.precaching.precacheAndRoute([ {url: '/', revision: 'v1'}, {url: '/classes', revision: 'v1'}, {url: '/index.css', revision: 'v1'}, {url: '/index.js', revision: 'v1'} ]);

Slide 58

Slide 58 text

InjectManifest plugin, add injection point // sw.js importScripts('https://storage.googleapis.com/...'); workbox.precaching.precacheAndRoute( self.__WB_MANIFEST );

Slide 59

Slide 59 text

InjectManifest plugin, build result // dist/sw.js importScripts('https://storage.googleapis.com/...'); workbox.precaching.precacheAndRoute([ {'revision':'b91028...','url':'index.329fc4.js'}, {'revision':'20420e...','url':'index.html'} ]);

Slide 60

Slide 60 text

Update service worker imports // sw.js import {precacheAndRoute} from 'workbox-precaching'; precacheAndRoute(self.__WB_MANIFEST);

Slide 61

Slide 61 text

Update service worker imports import {registerRoute} from 'workbox-routing'; import {StaleWhileRevalidate} from 'workbox-strategies'; import {BroadcastUpdatePlugin} from 'workbox-broadcast-update'; registerRoute( /\/blog\/.+$/, new StaleWhileRevalidate({ plugins: [new BroadcastUpdatePlugin()] }) );

Slide 62

Slide 62 text

Webpack plugin reference https://developers.google.com/web/tools/workbox/reference-doc s/latest/module-workbox-webpack-plugin Using bundlers with Workbox https://developers.google.com/web/tools/workbox/guides /using-bundlers More Info

Slide 63

Slide 63 text

a.k.a I didn’t know where to put these content so I’ll just put them at the end... Common recipes

Slide 64

Slide 64 text

Cache Google Fonts workbox.routing.registerRoute( (context) => context.url.origin === 'https://fonts.googleapis.com', new workbox.strategies.StaleWhileRevalidate({ cacheName: 'google-fonts-stylesheets' }) );

Slide 65

Slide 65 text

Cache Google Fonts workbox.routing.registerRoute( (context) => context.url.origin === 'https://fonts.gstatic.com', new workbox.strategies.CacheFirst({ cacheName: 'google-fonts-webfonts', plugins: [ new workbox.expiration.ExpirationPlugin({ maxAgeSeconds: 60 * 60 * 24 * 365, // one year maxEntries: 30 }) ] }) );

Slide 66

Slide 66 text

Generic fallbacks

Slide 67

Slide 67 text

Generic fallbacks

Slide 68

Slide 68 text

Generic fallbacks, precache fallback image workbox.precaching.precache([ {url: '/images/fallback-image.png', revision: 'v1'}, ]);

Slide 69

Slide 69 text

Generic fallbacks, route all images workbox.precaching.precache([ {url: '/images/fallback-image.png', revision: 'v1'}, ]); workbox.routing.registerRoute( (context) => context.request.destination === 'image', new workbox.strategies.NetworkOnly() );

Slide 70

Slide 70 text

Generic fallbacks workbox.routing.setCatchHandler(async context => { if (context.request.destination === 'image') { return workbox.precaching.matchPrecache( '/images/fallback-image.png' ); } });

Slide 71

Slide 71 text

Generic fallbacks, respond with fallback image workbox.routing.setCatchHandler(async context => { if (context.request.destination === 'image') { return workbox.precaching.matchPrecache( '/images/fallback-image.png' ); } });

Slide 72

Slide 72 text

Generic fallbacks

Slide 73

Slide 73 text

Generic fallbacks, for other pages

Slide 74

Slide 74 text

Workbox https://developers.google.com/web/tools/workbox Common Recipes https://developers.google.com/web/tools/workbox/guides /common-recipes More Info

Slide 75

Slide 75 text

❖ Workbox caching strategies ❖ Workbox plugins ❖ Using Workbox with Webpack ❖ Common Workbox recipes Recap!

Slide 76

Slide 76 text

❖ Web Application Manifest ❖ Push Notifications ❖ Background Sync ❖ Web Capabilities What’s next?

Slide 77

Slide 77 text

Building PWAs using Workbox Arnelle Balane That’s all for today! Thank you!