Skip to content

Instantly share code, notes, and snippets.

@Uriel29
Last active November 8, 2023 01:24
Show Gist options
  • Save Uriel29/e4fa54c0ddf173a5c348266a34e972e2 to your computer and use it in GitHub Desktop.
Save Uriel29/e4fa54c0ddf173a5c348266a34e972e2 to your computer and use it in GitHub Desktop.
Instalar PWA com botão que aparece assim que o Service esta registrado e o beforeinstallprompt esta funcional! Deixei o botão como none:
window.onload = function() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js').then(function() {
console.log('Service Worker Registered');
});
}
let deferredPrompt;
const addBtn = document.querySelector('#enable-banner-install');
window.addEventListener('beforeinstallprompt', (e) => {
console.log('beforeinstallprompt...');
addBtn.style.display = 'block';
e.preventDefault();
deferredPrompt = e;
addBtn.addEventListener('click', (e) => {
addBtn.style.display = 'none';
deferredPrompt.prompt();
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the prompt');
} else {
console.log('User dismissed the prompt');
}
deferredPrompt = null;
});
});
});
};
<head>
<link rel="manifest" href="manifest.json" />
<meta name="theme-color" content="#f2f2f2"/>
<style>
#enable-banner-install{
display:none ;
}
</style>
<link rel="stylesheet" type="text/css" href="tes.css">
</head>
<!DOCTYPE html>
<html>
<body>
<button type="button" id="enable-banner-install">clique e me instale na sua tela inicial</button>
<h1>Nada a declarar neste h1 </h1>
<p>Este paragrafo nem quer dizer nada.</p>
<script src="app.js"></script>
</body>
</html>
{
"name": "APP I TESTE",
"short_name": "APPI",
"theme_color": "#2196f3",
"background_color": "#2196f3",
"display": "standalone",
"Scope": "/",
"start_url": "http://localhost/pw/index.html",
"icons": [
{
"src": "images/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
{
"src": "images/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "images/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png"
},
{
"src": "images/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "images/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png"
}
],
"splash_pages": null
}
var VERSION = '22';
this.addEventListener('install', function(e) {
e.waitUntil(caches.open(VERSION).then(cache => {
return cache.addAll([
'http://localhost/pw/tes.css'
]);
}))
});
this.addEventListener('fetch', function(e) {
var tryInCachesFirst = caches.open(VERSION).then(cache => {
return cache.match(e.request).then(response => {
if (!response) {
return handleNoCacheMatch(e);
}
fetchFromNetworkAndCache(e);
return response
});
});
e.respondWith(tryInCachesFirst);
});
this.addEventListener('activate', function(e) {
e.waitUntil(caches.keys().then(keys => {
return Promise.all(keys.map(key => {
if (key !== VERSION)
return caches.delete(key);
}));
}));
});
function fetchFromNetworkAndCache(e) {
if (e.request.cache === 'only-if-cached' && e.request.mode !== 'same-origin') return;
return fetch(e.request).then(res => {
if (!res.url) return res;
if (new URL(res.url).origin !== location.origin) return res;
return caches.open(VERSION).then(cache => {
return res;
});
}).catch(err => console.error(e.request.url, err));
}
function handleNoCacheMatch(e) {
return fetchFromNetworkAndCache(e);
}
@Uriel29
Copy link
Author

Uriel29 commented Feb 16, 2019

De onde retirei os códigos > https://github.com/mdn/pwa-examples

@petronioamaral
Copy link

Show cara!! Testei aqui funcionou 100%, estava alguns dias buscando um código mais limpo e funcional. 👍 +1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment