feat: Initial prototype
This commit is contained in:
commit
819f6a61cd
327 files changed
+252446
No files matched your search
@@ -0,0 +1,51 @@
|
||||
export function setupPWA({failed}) {
|
||||
const install = document.getElementById('install');
|
||||
const guide = document.getElementById('install-guide');
|
||||
const update = document.getElementById('update');
|
||||
const standalone = () => window.matchMedia('(display-mode: standalone)').matches || navigator.standalone;
|
||||
let prompt;
|
||||
guide.hidden = !!standalone();
|
||||
window.addEventListener('beforeinstallprompt', event => {
|
||||
event.preventDefault();
|
||||
prompt = event;
|
||||
install.hidden = false;
|
||||
guide.hidden = true;
|
||||
});
|
||||
install.addEventListener('click', async () => {
|
||||
if (!prompt) return;
|
||||
const pending = prompt;
|
||||
prompt = undefined;
|
||||
install.hidden = true;
|
||||
try {
|
||||
await pending.prompt();
|
||||
await pending.userChoice;
|
||||
} catch {
|
||||
// Keep manual installation guidance available if the prompt fails.
|
||||
} finally {
|
||||
guide.hidden = !!standalone();
|
||||
}
|
||||
});
|
||||
window.addEventListener('appinstalled', () => {
|
||||
prompt = undefined;
|
||||
install.hidden = true;
|
||||
guide.hidden = true;
|
||||
});
|
||||
if (!('serviceWorker' in navigator)) return;
|
||||
let controlled = !!navigator.serviceWorker.controller;
|
||||
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
||||
if (controlled) window.location.reload();
|
||||
controlled = true;
|
||||
});
|
||||
navigator.serviceWorker.register('./sw.js', {scope: './', type: 'module', updateViaCache: 'none'})
|
||||
.then(registration => {
|
||||
const showUpdate = () => { update.hidden = !registration.waiting; };
|
||||
showUpdate();
|
||||
registration.addEventListener('updatefound', () => {
|
||||
const worker = registration.installing;
|
||||
if (worker) worker.addEventListener('statechange', showUpdate);
|
||||
});
|
||||
update.addEventListener('click', () => {
|
||||
if (registration.waiting) registration.waiting.postMessage('ACTIVATE_UPDATE');
|
||||
});
|
||||
}).catch(failed);
|
||||
}
|
||||
Reference in new issue
Block a user