Progressive Web Apps 

New Features

https://sajeetharan.com | @kokkisajee

University of Ruhuna | 29/06/2019

I'm Sajeetharan Sinnathurai

@kokkisajee

ng-Hello!

 

In a nutshell

Communities

Social Developer

Recognitions

  • Technology Evangelist @ Microsoft
  • Full-stack developer

 

THE CHALLENGE

"How to create Progressive Web App?"

If you don't know what to do

Watch his bowling vs England

1995
JS
creation
1997
ECMA
standard
2007
V8 project starts
2008 - 2010
Race for speed
2009
AngularJS
2013
React
2009
NodeJS
2014
VueJS
2018
> 5B
devices

Here's a story

a sweet little Web developer...

Yep, this stickman dude

He was happy

writing desktop &

Web apps...

Like normal happy

Then mobile apps

took over the world
and he was like...

"Man, I know nothing
but JavaScript, HTML
and CSS!" #Meh

Then he discovered

Angular + PWA

Websites: How?

Traditional Website

Request

Client requests website via URL.

Server responds with HTML document. This includes CSS and JavaScript links.

Mobile Apps: How?

Mobile App

Request

Mobile app already includes layout files, requests data from server.

Server responds with data (JSON). Mobile app populates local layout with returned data.

Hybrid Apps

A mixture of native and web technologies

Write app using JavaScript

Wrap in a native container, deliver through app stores

'Feels' like a native app, but code can be reused across platforms

Web Apps

Regular web pages, designed for small screens

Accessed through web browser on any device

Access to some hardware sensors (geolocation, accelerometer)

Will never be as performant as native apps

No App Store presence

Which type to use?

Games, highly customised designs: native will be the most perfomant.

Simple web presence, runs on a broad range of devices, don't care about app stores: mobile web.

Otherwise: Many apps will benefit from a hybrid approach.

"PWAs are the missing technology link to bring the best features of the web to solve the issues of native apps and closed marketplaces"


Chris Heilmann

76% more conversions between browsers

 

14% more active users/month on iOS; 30% more in Android

 

4X more rate of action from "Add to Homescreen"

Some Facts

10 characteristics

  • Progressive
  • Discoverable
  • Linkable
  • App-like
  • Responsive
  • Connectivity-independent
  • Re-engageable
  • Installable
  • Fresh
  • Safe

 Service Worker API

Push API and Notifications API

Web App Manifest spec

  • Make some features of the web app function offline

  • Improve online performance by reducing network requests

Service Worker API

Logically

Physically

JS

-file

App

Service worker

Frameworks

  • create-react-app

  • preact-cli

  • polymer-cli

  • vue-cli

Milestones of the web

AJAX

Static

Dynamic

RWD

PWA

Involved APIs

  • Service Worker API

  • Cache API

  • Fetch API

  • Notifications API

  • Push API

  • IndexedDB API

  • Promises

Ready?

Download

https://nodejs.org/en/download/
https://code.visualstudio.com/

Install

Run!

npm install -g @angular/cli@next
ng new ngHero --routing --style=scss
cd ngHero
npm start

Two things in mind

1. Your website should be HTTPS enabled

2. Your website should be responsive

Building Blocks

  • App Manifest
  • Application Shell
  • Service worker

 

 

Service
Worker
Client
Server

Generate a new Angular PWA

$ ng new myPWA
  • Angular CLI 8

  • Angular Service Worker 8

  • Web App Manifest

  • @Schematics

$ ng add @angular/pwa --project=myPWA

Building Angular PWA

$ ng build --prod

ngsw-worker.js

ngsw.json

dist/

Running Angular PWA

npm install -g http-server@0.9.0
http-server -p 8080

Hint #1: Checking the status

https://yourwebsite.com/ngsw/state

NGSW Debug Info:

Driver state: NORMAL ((nominal))
Latest manifest hash: cd4716ff2d3e24f4292010c929ff429d9eeead73
Last update check: 9s215u

=== Version 34c3fd2361735b1330a23c32880640febd059305 ===

Clients: 7eb10c76-d9ed-493a-be12-93f305394a77

=== Version cd4716ff2d3e24f4292010c929ff429d9eeead73 ===

Clients: ee22d69e-37f1-439d-acd3-4f1f366ec8e1

=== Idle Task Queue ===
Last update tick: 4s602u
Last update run: 9s222u
Task queue:


Debug log:

Adding NGSW to the existing app

$ ng set apps.0.serviceWorker=true
$ npm install @angular/service-worker --save

1. Install the package

2. Enable build support

3. Register NGSW for your app

4. Create configuration file

NGSW configuration file

src/ngsw-config.json

{
  "index": "/index.html",
  "assetGroups": [...],
  "dataGroups": [...]
}

Registering NGSW

import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
...
@NgModule({
  ...
  imports: [
    ...


  ]
})
export class AppModule { }
    ServiceWorkerModule.register('/ngsw-worker.js',
        { enabled: environment.production }),

app.module.ts

NGSW configuration file

src/ngsw-config.json

{
  "index": "/index.html",
  "assetGroups": [...],
  "dataGroups": [...]
}

App shell

assetGroups

{
    "name": "app",
    "installMode": "prefetch",
    "resources": {...}
}

App shell resources

assetGroups / "app" / resources

"resources": {









}
    "versionedFiles": [
      "/*.bundle.css",
      "/*.bundle.js",
      "/*.chunk.js"
    ],
    "files": [
      "/favicon.ico",
      "/index.html"
    ],

App shell / on-demand

assetGroups

{
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {...}
}

App shell / on-demand

assetGroups / "assets" / resources

"resources": {








}
    "files": [
      "/assets/**"
    ],
    "urls": [
        "https://fonts.googleapis.com/**",
        "https://fonts.gstatic.com/**"
    ]

Runtime caching

dataGroups

{
    "name": "api-freshness",
    "urls": [
      "/api/breakingnews/**"
    ],






}
    "cacheConfig": {
      "strategy": "freshness",
      "maxSize": 10,
      "maxAge": "12h",
      "timeout": "10s"
    }

Runtime caching

dataGroups

{
    "name": "api-performance",
    "urls": [
      "/api/archive/**"
    ],






}
    "cacheConfig": {
      "strategy": "performance",
      "maxSize": 100,
      "maxAge": "365d"
    }

Hint #2: Support API versioning

dataGroups

{
    "version": 1,
    "name": "api-performance",
    "urls": [
      "/api/**"
    ],
    ...
}
{
    "version": 2,
    "name": "api-performance",
    "urls": [
      "/api/**"
    ],
    ...
}

Hint #3: Notify about updates

import { SwUpdate } from '@angular/service-worker';
constructor(private swUpdate: SwUpdate) {}
this.swUpdate.available.subscribe(event => {
  let snackBarRef = this.snackBar
    .open('Newer version of the app is available', 'Refresh');

  snackBarRef.onAction().subscribe(() => {
    window.location.reload()
  })
})

updates.component.ts

Push notifications

import { SwPush } from '@angular/service-worker';
constructor(private swPush: SwPush) {}
subscribeToPush() {
  this.swPush.requestSubscription({
    serverPublicKey: this.VAPID_PUBLIC_KEY
  })
    .then(pushSubscription => {
      // Pass subscription object to backend
    })
}

push.component.ts

Push notifications / send

{
  "notification": {










  }
}

server-side.js / sendNotification payload

    "title": "Very important notification",
    "body": "Angular Service Worker is cool!",
    "icon": "https://angular.io/assets/logo.png",
    "actions": [
      {
        "action": "gocheck",
        "title": "Go and check"
      }
    ],
    ...

Congratulations

Hope things go smoothly

Otherwise

Where to go from here?

- Build Apps

- Send your queries @kokkisajee

- Be a part of Ng-SriLanka

- Demo code

https://github.com/sajeetharan/angular-pwa-features

Q & A : Explore by yourself and learn.

Let's build PWA apps

By Sajeetharan Sinnathurai

Let's build PWA apps

Progressive Web Applications leverages the success of web and native mobile application. Your application can be downloaded and launched without going through the hassle of App Store / Android Store. It reduces the friction of application distribution process by leveraging the success of the web. In this talk, I will cover The need for PWA Building Blocks of PWA Service Worker Web App Manifest Building PWA with angular

  • 963