Skip to content

Instantly share code, notes, and snippets.

@kmaraz
Created December 6, 2019 23:11
Show Gist options
  • Save kmaraz/bea955a4c6d4bbb14c54cc94a9f52ec2 to your computer and use it in GitHub Desktop.
Save kmaraz/bea955a4c6d4bbb14c54cc94a9f52ec2 to your computer and use it in GitHub Desktop.
@Injectable({
providedIn: 'root'
})
export class WorkerService {
initialize() {
if ('serviceWorker' in navigator && 'BroadcastChannel' in window) {
// Initialize worker
this.getWorkboxInstance()
.subscribe((Workbox) => {
if (Workbox) {
const wb = new Workbox(`/${WORKER_NAME}`);
wb.register();
}
});
}
}
private getWorkboxInstance(): Observable<any> {
return new Observable((observer: Observer<any>) => {
import(
/* webpackChunkName: "workboxWindow" */
'workbox-window' as any
)
.then(({ Workbox }) => observer.next(Workbox))
.catch(() => observer.next(null));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment