Howdy. Below is the simplest code by which it is obvious that the ‘beforeinstallprompt’ event is never triggered in a certain situation. The source code is:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="manifest" href="/manifest.json">
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
let deferredPrompt;
window.addEventListener('beforeinstallprompt', function(e) {
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
// Update UI to notify the user they can add to home screen
console.log("beforeinstallprompt!");
});
// this is the line that create the problem, no matter what are the parameters
window.history.replaceState( {}, "", "" );
</script>
</head>
<body>
test
</body>
</html>
If I remove this line window.history.replaceState( {}, "", "" )
, event ‘beforeinstallprompt’ is fired and the output in console is:
ServiceWorker registration successful with scope: https://example.com/
beforeinstallprompt!
Banner not shown: beforeinstallpromptevent.preventDefault() called. The page must call beforeinstallpromptevent.prompt() to show the banner.
If I do not remove the line window.history.replaceState( {}, "", "" )
, event ‘beforeinstallprompt’ is never fired and the output in console is:
ServiceWorker registration successful with scope: https://example.com/
I think it’s a bug. In Chrome is working.
Can it be investigated and resolved?
Thanks.
C