-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configured service worker + pwa config
- Loading branch information
Showing
6 changed files
with
114 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters