Skip to content

Instantly share code, notes, and snippets.

View webmaxru's full-sized avatar
😀
Progressive Web Max

Maxim Salnikov webmaxru

😀
Progressive Web Max
View GitHub Profile
@webmaxru
webmaxru / app.module.ts
Created November 3, 2017 23:42
Registering Angular Service Worker
import { ServiceWorkerModule } from '@angular/service-worker'
import { environment } from '../environments/environment';
...
@NgModule({
imports: [
...
environment.production ? ServiceWorkerModule.register('/ngsw-worker.js') : []
],
@webmaxru
webmaxru / package.json
Created November 4, 2017 00:06
Emulate Angular 1.6 NGSW build support
{
...
"scripts": {
...
"ngsw-config": "node_modules/.bin/ngsw-config dist src/ngsw-config.json",
"ngsw-copy": "cp node_modules/@angular/service-worker/ngsw-worker.js dist/",
"build-prod-ngsw": "ng build --prod && npm run ngsw-config && npm run ngsw-copy",
"serve-prod-ngsw": "npm run build-prod-ngsw && http-server dist -p 8080"
}
}
@webmaxru
webmaxru / ngsw-config.json
Created November 4, 2017 10:13
Adding external resources to app shell
{
...
"assetGroups": [
...
{
"name": "fonts",
"resources": {
"urls": [
"https://fonts.googleapis.com/**",
"https://fonts.gstatic.com/**"
@webmaxru
webmaxru / ngsw-config.json
Last active August 12, 2018 14:19
Data groups for runtime caching
{
...
"dataGroups": [{
"name": "api-freshness",
"urls": [
"/timeline"
],
"cacheConfig": {
"strategy": "freshness",
"maxSize": 100,
@webmaxru
webmaxru / control-push.component.ts
Created November 4, 2017 12:09
Subscription to push using SwPush
import { SwPush } from '@angular/service-worker';
...
// Injecting SwPush dependency
constructor(private pushService: PushService, private swPush: SwPush) {}
subscribeToPush() {
// Requesting messaging service to subscribe current client (browser)
@webmaxru
webmaxru / notification-data.json
Created November 4, 2017 13:00
Notification payload format expected by NGSW from the backend
{
"notification": {
"title": "Ranjeet Kumar",
"actions": [
{
"action": "opentweet",
"title": "Open tweet"
}
],
"body": "The latest The Top Javascript Blogs Daily! https://t.co/o3PSNkk9Di Thanks to @LifeWithKathy #makeyourownlane",
@webmaxru
webmaxru / control-push.component.ts
Created November 4, 2017 13:04
Unsubscription using SwPush
unsubscribeFromPush() {
// Get active subscription
this.swPush.subscription
.take(1)
.subscribe(pushSubscription => {
console.log('[App] pushSubscription', pushSubscription)
@webmaxru
webmaxru / control-push.component.ts
Created November 4, 2017 13:05
Subscribe for the push messages in our app
showMessages() {
this.swPush.messages
.subscribe(message => {
console.log('[App] Push message received', message)
let notification = message['notification']
this.tweets.unshift({
import { SwUpdate } from '@angular/service-worker';
...
export class ControlBroadcastComponent implements OnInit {
constructor(private swUpdate: SwUpdate, ...) { }
ngOnInit() {
this.swUpdate.available.subscribe(event => {
@webmaxru
webmaxru / control-broadcast.component.ts
Created November 4, 2017 15:48
checkForUpdate and activateUpdate methods of SwUpdate
checkForUpdate() {
console.log('[App] checkForUpdate started')
this.swUpdate.checkForUpdate()
.then(() => {
console.log('[App] checkForUpdate completed')
})
.catch(err => {
console.error(err);
})
}