Good day all, I have tried visiting this page: https://web.dev/customize-install/, stackover flow questions and google pages but I still cannot get it write. I am trying to achieve three things with my PWA app:
- Open "cache" and get files with the install event.
- Prevent the default PWA from install using "beforeinstallprompt" listener.
- Display a bootstrap modal with a "download" button.
- When that button is clicked, trigger the "fetch" event which installs the app.
I am able to do 1 and 2. I am triggering my modal on main.js. I tried in the service worker but I could not. The download button on the modal also does not
index.php
<div class="modal fade" id="myModal3" role="dialog">
<div class="modal-dialog">
<div class="modal-content" >
<div class="modal-body">
<center><p><i class="fa fa-arrow-circle-down"></i></p>
<h5 style="margin-top:0vh;" >Download App</h5>
<p>Download our super cool app.
<div >
<div class="wrap-login100-form-btn" >
<div class="login100-form-bgbtn"></div>
<button class="login100-form-btn" class="AndaniDownload" id="btnDownloadAppOffline" >
Download </button>
</div>
</div>
</center>
</div>
</div>
</div>
</div>
Main.js
$(document).ready(function(){
$('#myModal3').modal('show');
});
pwabuilder-sw.js
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
const CACHE = "pwabuilder-page";
const offlineFallbackPage = "offline.html";
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});
self.addEventListener('install', async (event) => {
event.waitUntil(
caches.open(CACHE)
.then((cache) => cache.add(offlineFallbackPage))
);
});
if (workbox.navigationPreload.isSupported()) {
workbox.navigationPreload.enable();
}
self.addEventListener('fetch', (event) => {
if (event.request.mode === 'navigate') {
event.respondWith((async () => {
try {
const preloadResp = await event.preloadResponse;
if (preloadResp) {
return preloadResp;
}
const networkResp = await fetch(event.request);
return networkResp;
} catch (error) {
const cache = await caches.open(CACHE);
const cachedResp = await cache.match(offlineFallbackPage);
return cachedResp;
}
})());
}
});
self.addEventListener('beforeinstallprompt', event => {
event.preventDefault();
var button =self.querySelector('.AndaniDownload');
//button.removeAttribute('hidden');
button.addEventListener('click', () => {
event.prompt();
button.setAttribute('disabled', true);
});
});