Skip to content

Commit

Permalink
feat(element): service-worker helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Jan 18, 2023
1 parent ad8e2d8 commit 6ec8b59
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
43 changes: 43 additions & 0 deletions ui/element/src/helper/service-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {createLogger} from '@alwatr/logger';
const logger = createLogger('service-worker');

export async function registerServiceWorker(
path = 'service-worker.js',
scope = '/',
): Promise<ServiceWorkerRegistration | void> {
logger.logMethodArgs('registerServiceWorker', {path, scope});

if (!('serviceWorker' in navigator)) {
logger.incident('registerServiceWorker', 'sw_unsupported', 'Service worker not supported in this browser');
return;
}

try {
const swRegistration = await navigator.serviceWorker.register(path);
swRegistration.addEventListener('updatefound', () => swUpdateFound(swRegistration.installing));
logger.logOther('Service worker registered');
return swRegistration;
}
catch (err) {
logger.error('registerServiceWorker', 'sw_reg_fail', 'Service worker registration failed');
}
}

function swUpdateFound(sw: ServiceWorker | null): void {
if (sw == null) return;
logger.logMethod('swUpdateFound');
sw.addEventListener('statechange', () => swStateChange(sw));
}

function swStateChange(sw: ServiceWorker): void {
logger.logMethodArgs('swStateChange', sw.state);
if (sw.state === 'installed') {
// if old controller available then its update else its new install
if (navigator.serviceWorker.controller) {
// send sw-updated signal;
}
}
else if (sw.state === 'redundant') {
logger.accident('swStateChange', 'sw_redundant', 'Service worker redundant');
}
}
5 changes: 0 additions & 5 deletions ui/element/src/helper/service-worker/register.ts

This file was deleted.

8 changes: 2 additions & 6 deletions ui/element/src/pwa-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {router, type RoutesConfig} from '@alwatr/router';
import {html, css, type CSSResultGroup, type PropertyValues} from 'lit';
import {cache} from 'lit/directives/cache.js';

import {registerServiceWorker} from './helper/service-worker/register.js';
import {registerServiceWorker} from './helper/service-worker.js';
import {AlwatrSmartElement} from './smart-element.js';

import '@alwatr/ui-kit/style/token.css';
Expand Down Expand Up @@ -71,10 +71,6 @@ export class AlwatrPwaElement extends AlwatrSmartElement {
protected override firstUpdated(changedProperties: PropertyValues<this>): void {
super.firstUpdated(changedProperties);
this.removeAttribute('unresolved');
registerServiceWorker('service-worker.js')
?.then(() => this._logger.logOther(`ServiceWorker registered from "service-worker.js".`))
.catch((err) => {
this._logger.logOther('ServiceWorker registration failed: ', err);
});
registerServiceWorker();
}
}

0 comments on commit 6ec8b59

Please sign in to comment.