Slide 1

Slide 1 text

Progressive WEB APPS

Slide 2

Slide 2 text

@cironunesdev

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Progressive Web App

Slide 7

Slide 7 text

Progressive Web App Enhancement

Slide 8

Slide 8 text

Progressive Web App Enhancement Like

Slide 9

Slide 9 text

http://alistapart.com/article/understandingprogressiveenhancement

Slide 10

Slide 10 text

http://alistapart.com/article/understandingprogressiveenhancement

Slide 11

Slide 11 text

App Like

Slide 12

Slide 12 text

RESPONSIVE CONNECTIVITY INDEPENDENT DISCOVERABLE ENGAGEABLE INSTALLABLE SAFE LINKABLE

Slide 13

Slide 13 text

App Like, but…

Slide 14

Slide 14 text

Service Workers Cache API Fetch API Notification API HTTPS ❤ }

Slide 15

Slide 15 text

How to build a PWA?

Slide 16

Slide 16 text

1. Web Application Manifest 2. App Shell 3. Cache (SW)

Slide 17

Slide 17 text

{ "name": "Ali Express", "short_name": "AliExpress", "icons": [{ "src": "images/icons/icon-128x128.png", "sizes": "128x128", "type": "image/png" }], "start_url": "/index.html", "display": "standalone", "background_color": "#3E4EB8", "theme_color": "#2F3BA2" } manifest.webapp

Slide 18

Slide 18 text

Splash Screen

Slide 19

Slide 19 text

1. Web Application Manifest 2. App Shell 3. Cache (SW)

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

1. Architect & implement your app shell 2. Cache via service workers

Slide 24

Slide 24 text

App shell

Menu ... ...

Slide 25

Slide 25 text

1. Architect & implement your app shell 2. Cache via service workers

Slide 26

Slide 26 text

if ('serviceWorker' in navigator) { navigator['serviceWorker'] .register('./service-worker.js') .then(() => console.log('Service Worker Registered')); } main.ts REGISTER

Slide 27

Slide 27 text

var cacheName = 'myApp'; var filesToCache = [...]; self.addEventListener('install', function(e) { e.waitUntil( caches.open(cacheName).then(function(cache) { return cache.addAll(filesToCache); }) ); }); service-worker.js ADD TO CACHE

Slide 28

Slide 28 text

self.addEventListener('activate', (e) => { e.waitUntil(self.clients.claim()); e.waitUntil( caches.keys().then((keyList) => { return Promise.all(keyList.map((key) => { if (key !== cacheName) { return caches.delete(key); } })); }) ); }); service-worker.js CLEAR CACHE

Slide 29

Slide 29 text

self.addEventListener('fetch', (e) => { e.respondWith( caches.match(e.request).then((response) => { return response || fetch(e.request); }) ); }); service-worker.js READING FROM CACHE

Slide 30

Slide 30 text

1. Web Application Manifest 2. App Shell 3. Cache (SW)

Slide 31

Slide 31 text

self.addEventListener('fetch', function(e) { var dataUrl = 'https://sample-api.com'; if (e.request.url.indexOf(dataUrl) > -1) { e.respondWith( caches.open(dataCacheName).then(function(cache) { return fetch(e.request).then(function(response){ cache.put(e.request.url, response.clone()); return response; }); }) ); } else { e.respondWith( caches.match(e.request).then(function(response) { return response || fetch(e.request); }) ); } }); service-worker.js

Slide 32

Slide 32 text

self.addEventListener('fetch', function(e) { var dataUrl = 'https://sample-api.com'; if (e.request.url.indexOf(dataUrl) > -1) { e.respondWith( caches.open(dataCacheName).then(function(cache) { return fetch(e.request).then(function(response){ cache.put(e.request.url, response.clone()); return response; }); }) ); } else { e.respondWith( caches.match(e.request).then(function(response) { return response || fetch(e.request); }) ); } }); service-worker.js

Slide 33

Slide 33 text

self.addEventListener('fetch', function(e) { var dataUrl = 'https://sample-api.com'; if (e.request.url.indexOf(dataUrl) > -1) { e.respondWith( caches.open(dataCacheName).then(function(cache) { return fetch(e.request).then(function(response){ cache.put(e.request.url, response.clone()); return response; }); }) ); } else { e.respondWith( caches.match(e.request).then(function(response) { return response || fetch(e.request); }) ); } }); service-worker.js

Slide 34

Slide 34 text

self.addEventListener('fetch', function(e) { var dataUrl = 'https://sample-api.com'; if (e.request.url.indexOf(dataUrl) > -1) { e.respondWith( caches.open(dataCacheName).then(function(cache) { return fetch(e.request).then(function(response){ cache.put(e.request.url, response.clone()); return response; }); }) ); } else { e.respondWith( caches.match(e.request).then(function(response) { return response || fetch(e.request); }) ); } }); service-worker.js

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

caches.match(e.request.clone()).then((response) => { return response || fetch(e.request.clone()).then((theResponse) => { return caches.open(dataCacheName).then((cache) => { cache.put(e.request.url, theResponse.clone()); return theResponse.clone(); }); }); }); service-worker.js

Slide 37

Slide 37 text

Profit!

Slide 38

Slide 38 text

Profit! What about a protip?

Slide 39

Slide 39 text

Lighthouse

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Bootstrap App shell Service worker Notifications

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Bootstrap App shell Service worker Notifications

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

~ $ npm i --save @angular/app-shell

Slide 49

Slide 49 text

~ $ npm i --save @angular/app-shell // App code import { AppShellModule } from '@angular/app-shell'; @NgModule({ bootstrap: [AppComponent], imports: [ BrowserModule, AppShellModule.runtime(), AppModule })

Slide 50

Slide 50 text

@Component({ selector: 'app-root-component', template: ` ` }) export class AppRootComponent {}

Slide 51

Slide 51 text

Can we be faster?

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

@NgModule({ bootstrap: [AppComponent], imports: [ AppShellModule.prerender(), AppModule, UniversalModule.withConfig({...}) })

Slide 54

Slide 54 text

Bootstrap App shell Service worker Notifications

Slide 55

Slide 55 text

~ $ npm i --save @angular/service-worker

Slide 56

Slide 56 text

~ $ npm i --save @angular/service-worker // worker-basic.min.js is copied from // node_modules/@angular/service-worker/bundles if (navigator.serviceWorker) { navigator.serviceWorker.register('/worker-basic.min.js'); }

Slide 57

Slide 57 text

{ "routing": { "routes": { "/": { "prefix": false } }, "index": "/index.html" } } ngsw-manifest.json

Slide 58

Slide 58 text

Bootstrap App shell Service worker Notifications

Slide 59

Slide 59 text

self.addEventListener('push', function(e) { e.waitUntil( fetch('http://localhost:8090/pushdata').then(function(response) { return response.json(); }).then(function(data) { return self.registration.showNotification(title, { body: body, icon: icon, tag: tag }); }, function(err) { err(err); }) ); }); worker-push.js

Slide 60

Slide 60 text

self.addEventListener('push', function(e) { e.waitUntil( fetch('http://localhost:8090/pushdata').then(function(response) { return response.json(); }).then(function(data) { return self.registration.showNotification(title, { body: body, icon: icon, tag: tag }); }, function(err) { err(err); }) ); }); worker-push.js

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

pwa.rocks Go get inspired and create something great!

Slide 63

Slide 63 text

@cironunesdev Dzięki!