Skip to content

Commit

Permalink
configured service worker + pwa config
Browse files Browse the repository at this point in the history
  • Loading branch information
fcamblor committed May 8, 2021
1 parent dfebdfa commit ecb5381
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 20 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<link rel="apple-touch-icon" sizes="180x180" href="/assets/images/favicon/apple-touch-icon.png?v=oLnE8zppbY">
<link rel="icon" type="image/png" sizes="32x32" href="/assets/images/favicon/favicon-32x32.png?v=oLnE8zppbY">
<link rel="icon" type="image/png" sizes="16x16" href="/assets/images/favicon/favicon-16x16.png?v=oLnE8zppbY">
<link rel="manifest" href="/assets/images/favicon/site.webmanifest?v=oLnE8zppbY">
<link rel="mask-icon" href="/assets/images/favicon/safari-pinned-tab.svg?v=oLnE8zppbY" color="#5561d9">
<link rel="shortcut icon" href="/assets/images/favicon/favicon.ico?v=oLnE8zppbY">
<link rel="manifest" href="/manifest.webmanifest">
<meta name="apple-mobile-web-app-title" content="Vite Ma Dose">
<meta name="application-name" content="Vite Ma Dose">
<meta name="msapplication-TileColor" content="#585b79">
Expand Down
19 changes: 0 additions & 19 deletions public/assets/images/favicon/site.webmanifest

This file was deleted.

29 changes: 29 additions & 0 deletions public/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"short_name": "ViteMaDose",
"name": "ViteMaDose : Trouver un vaccin facilement et rapidement",
"icons": [
{
"src": "/assets/images/favicon/android-chrome-192x192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "/assets/images/favicon/android-chrome-512x512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": "/",
"background_color": "#5561d9",
"display": "minimal-ui",
"theme_color": "#5561d9",
"shortcuts": [],
"description": "Trouver un vaccin facilement et rapidement",
"screenshots": [
{
"src": "/assets/images/social/vitemadose.png",
"type": "image/png",
"sizes": "2704x1476"
}
]
}
31 changes: 31 additions & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

function clientRootUrl() {
return self.location.href.replace("sw.js","");
}

function env() {
return (self.location.hostname === "vitemadose.covidtracker.fr")?'prod':'dev';
}


self.addEventListener('install', function(event) {
console.log('Service Worker activating...');
// event.waitUntil(self.skipWaiting()); // Activate worker immediately
});

self.addEventListener('activate', function(event) {
console.log('Service Worker activating...');
event.waitUntil(
Promise.all([
Promise.resolve()
]).then(function() {
console.log('SW activation finished !');
return self.clients.claim();
})
);
});

// Dummy fetch handler to make PWA installable (without this, we won't have the installation CTA on the website)
self.addEventListener('fetch', function(event) {
//console.log("in dummy fetch handler");
});
50 changes: 50 additions & 0 deletions src/utils/ServiceWorkers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {Router} from "../routing/Router";

export class ServiceWorkers {

public static readonly INSTANCE = new ServiceWorkers();

private constructor() {
}

async startup() {
// Registering background synchronization
if (!navigator.serviceWorker){
console.info("Service Worker not supported")
return false;
}

// Waiting for 'load' event to start service worker registration
// see https://developers.google.com/web/fundamentals/primers/service-workers/registration#improving_the_boilerplate
await new Promise((resolve) => window.addEventListener('load', resolve));

const serviceWorkerRegistration = await navigator.serviceWorker.register(`${Router.basePath}sw.js`);

navigator.serviceWorker.addEventListener('controllerchange', () => {
console.log("controllerchange called !");
});

// Cases :
// - navigator.serviceWorker.controller is undefined : this occurs the first time the sw is installed
// in that case, we should look for updatefound + statechange=activated events to resolve controller
// - navigator.serviceWorker.controller is defined, we should play with is, but don't forget to register a
// controller change event in case a new version of the sw is deployed

await new Promise<{controller: ServiceWorker, updated: boolean}>((resolve) => {
if(navigator.serviceWorker.controller) {
resolve({controller: navigator.serviceWorker.controller, updated: false});
}

serviceWorkerRegistration.addEventListener('updatefound', () => {
const newWorker = serviceWorkerRegistration.installing!;
newWorker.addEventListener('statechange', () => {
if(newWorker.state === 'activated') {
resolve({ controller: navigator.serviceWorker.controller!, updated: true});
}
})
});
})

return true;
}
}
3 changes: 3 additions & 0 deletions src/vmd-app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {LitElement, html, customElement, property, css } from 'lit-element';
import {Router, SlottedTemplateResultFactory} from "./routing/Router";
import smoothscroll from 'smoothscroll-polyfill';
import {CSS_Global} from "./styles/ConstructibleStyleSheets";
import {ServiceWorkers} from "./utils/ServiceWorkers";

@customElement('vmd-app')
export class VmdAppComponent extends LitElement {
Expand All @@ -27,6 +28,8 @@ export class VmdAppComponent extends LitElement {
Router.installRoutes((viewTemplateResult) => {
this.viewTemplateResult = viewTemplateResult;
})

ServiceWorkers.INSTANCE.startup();
}

render() {
Expand Down

0 comments on commit ecb5381

Please sign in to comment.