PWA with Angular : Introduction
Even Stack Overflow can't answer!
Ajit Kumar Singh
Doing Software Stuff
Doing frontend stuff.
I ❤️ web despite its weirdness, except
Crying over NullPointers in Java
Hype train is coming
Progressive
Web
+
Application
+
=
?
The buzzword
PWA are websites that use modern web capabilities to deliver a native app-like user experience.
The term PWA is like a quality stamp.
Communicates about a certain quality level, instead of some feature list or technical details.
Those who named it say that:
It’s just a useful term for a toolkit of features.
Alex Russell
The name isn’t for you […]
The name is for your boss, for your investor, for your marketeer.
Frances Berriman
... just a bunch of technologies with a zingy-new brandname that keeps the open web going a bit longer.....
Frances's Blog: https://fberriman.com/2017/06/26/naming-progressive-web-apps/
Progressive enhancement
Web Application is what it really is.
A development approach, not a technology choice
Progressive is the keyword
Too Long; Didn't Listen
Benefits
| Android | PWA | |
|---|---|---|
| 17.3 MB | 1.3M | |
| Google Map | 25 MB | 208k |
I was too lazy to add more 😬
Business and market benefit stats: https://www.pwastats.com
Who is in the hype train?
Find more: https://appsco.pe
Very long checklist
Very long checklist: https://developers.google.com/web/progressive-web-apps/checklist
Source: https://tinyurl.com/y4a9fu2f
Just enough to close the ticket needs...
To convert your web app to "appy-like" PWA:
(even if only a custom offline page)
HTTPS or localhost
Web App Manifest
Service Workers
What is it?
Simple JSON file that defines some key properties of PWA like
Metadata for PWA
Where does it go?
<!doctype html>
<head>
...
<link rel="manifest" href="manifest.webmanifest">
...
</head>
<body>
...
</body>
</html>index.html
Adding to home screen
{
"name": "NG Leipzig PWA Demo",
"short_name": "NG Leipzig",
"theme_color": "#985151",
"background_color": "#3b906a",
"display": "standalone",
"Scope": "/",
"start_url": "/",
"icons": [
{
"src": "assets/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
.......
]
}Splash screen
{
"name": "NG Leipzig PWA Demo",
"short_name": "NG Leipzig",
"theme_color": "#985151",
"background_color": "#3b906a",
"display": "standalone",
"Scope": "/",
"start_url": "/",
"icons": [
{
"src": "assets/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
.......
]
}Home screen
{
"name": "NG Leipzig PWA Demo",
"short_name": "NG Leipzig",
"theme_color": "#985151",
"background_color": "#3b906a",
"display": "standalone",
"Scope": "/",
"start_url": "/",
"icons": [
{
"src": "assets/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
.......
]
}I am lazy edition!
What is it?
Source: https://tinyurl.com/y526jzel
Programmable network proxy that sits between the web page and the network.
( Good place for doing caching stuff )
Some facts
Lifecycle
// Registration
if (navigator.serviceWorker) {
navigator.serviceWorker.register('/serviceworker.js');
}Start with registering a service worker
Lifecycle - Installation
Ideal time to cache some static assets.
👉
//Take care of precaching the resources we need.
self.addEventListener('install', event => {
event.waitUntil(
caches.open('cache-name')
.then(cache => cache.addAll(PRECACHE_URLS))
);
});Installed if all the files are cached successfully.
Lifecycle - Activated
Ideal place for cleaning old caches from older service worker
👉
// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', event => {
event.waitUntil(cleanCache());
});Lifecycle - Termination
👉
* A user agent may terminate service workers at any time it:
Has no event to handle.
Detects abnormal operation: such as infinite loops and tasks exceeding imposed time limits (if any) while handling the events.
*Ctrl+C and Ctrl+V from: https://www.w3.org/TR/service-workers-1/#service-worker-lifetime
Lifecycle - Fetch
👉
self.addEventListener('fetch', event => event.respondWith(
// Various caching strategies possible here
));Service worker will begin to receive fetch events
Caching Flow
The most important thing you need to know is :
By using Service Worker Cache you are NOT disabling Browser Cache.
The basic flow is:
Service Worker can hijack any request.
Caching Strategies
Network First
self.addEventListener('fetch', event => event.respondWith(
fromNetwork(event.request).catch(() => fromCache(event.request))
));FRESHNESS
Caching Strategies
Stale-while-revalidate
self.addEventListener('fetch', function (event) {
// answer immediately from cache
event.respondWith(fromCache(event.request));
// update the cache with network response for next time
event.waitUntil(updateCache(event.request));
});PERFORMANCE
Moment of truth!
ng new << YOUR PROJECT >>ng add @angular/pwang build --prodWhat changed?
git status and git diff time
Angular PWA facts
Configurable via file: ngsw-config.json
Configuration
export interface Schema {
appData?: {
[k: string]: any;
};
index: string;
assetGroups?: {
name: string;
installMode?: "prefetch" | "lazy";
updateMode?: "prefetch" | "lazy";
resources: {
files?: string[];
urls?: string[];
};
}[];
dataGroups?: {
name: string;
urls: string[];
version?: number;
cacheConfig: {
maxSize: number;
maxAge: string;
timeout?: string;
strategy?: "freshness" | "performance";
};
}[];
navigationUrls?: string[];
}index: Usually this is /index.html
ngsw-config.json
assetGroups: Static assets to cache
dataGroup: API endpoints you want to cache
maxAge: How long to cache
maxSize: How many to cache; LRU caching
timeOut: How long to wait in case of freshness strategy
navigationUrls: list of URLs that will be redirected to the index file
appData: Any data you want to add with this version of the app
files: Your app static assets
urls: URL to external static assets e.g. google fonts
How does it work?
Angular Service worker:
Checking for updates:
User opens app or reloads
Angular Service worker will look for updates to the ngsw.json
If an update is found, it is downloaded and cached (Can listen to event with SwUpdate service)
Updated version is served next time the application is loaded
Might save your Friday!
Bypassing the service worker: Set ngsw-bypass as a request header, or as a query parameter.
Value doesn't matter, can be empty.
Self-destruct: Make ngsw.json return 404 (rename or delete it).
The service worker will remove all of its caches and de-registers itself.
Can do all what web technologies allow.
Can ONLY do what web technologies allow.
What is the limit?
Please don't start A vs B fight
Native applications still:
PWA struggles:
* The 1000 Challenges of PWA on iOS: https://onilab.com/blog/the-1000-challenges-of-progressive-web-apps-on-ios/
Asking questions helps.
and so on ..
What else ? 🤔
| Where? | Here |
| Does it has a name? | Well dah!! It's called software enthusiasts |
| What can I expect? | 🍕 🍺 and awesome talks by awesome jambitees 😎 |
| What awesome talks? | All kinda tech stuff but first one will be about: 1. Modern React with Hooks by Santo Pfingsten, Software Engineer 2. An introduction to service meshes by Max Brenner, DevOps Engineer |
| When? | Tuesday, November 26, 2019, 6:30 PM to 9:30 PM |
| I have more questions! | Ask Philip Achenbach for more info and let me go now please 😰🙏 |
Check it out: https://www.meetup.com/software-enthusiasts/