Hello I'm trying to install a custom PWA "Add to Homescreen".
The ServiceWorkerRegistration is successful.
But the function beforeinstallpromp is not calling after register.
<script type="text/javascript">
function request_debug(paramdata){
document.getElementById('output').innerHTML += '<BR>'+ paramdata;
}
window.addEventListener('load', function() {
document.getElementById('output').style.display = "block";
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js').then(function(registration) {
console.log('Service worker registrado com sucesso:', registration);
request_debug(registration);
}).catch(function(error) {
console.log('Falha ao Registrar o Service Worker:', error);
request_debug(error);
});
var isTooSoon = true;
window.addEventListener('beforeinstallprompt', function(e) {
//e.preventDefault();
//e.prompt();
//promptEvent = e;
request_debug(' window.addEventListener beforeinstallprompt fired!')
if (isTooSoon) {
//e.preventDefault(); // Prevents prompt display
// Prompt later instead:
setTimeout(function() {
isTooSoon = false;
e.prompt(); // Throws if called more than once or default not prevented
}, 4000);
}
});
}else{
console.log('serviceWorker not in navigator');
request_debug('serviceWorker not in navigator');
}
});
</script>
Also my service worker in root directory...
HTTPS is secure!
my manifest:
{
"background_color": "purple",
"description": "lojaportaldotricot TESTE",
"display": "standalone",
"icons": [
{
"src": "/componentes/serviceWorker/fox-icon.png",
"sizes": "192x192",
"type": "image/png"
}
],
"name": "lojaportaldotricot",
"short_name": "lojaportaldotricot",
"start_url": "/dashboard"
}
It's only workes when I set "Enable" chrome://flags/#bypass-app-banner-engagement-checks
Edit: Look's like I've found the problem. The Audits tabs of Chrome's DevTools(F12) gives debugging information.
Try this :
<script>
let deferredPrompt;
window.addEventListener('beforeinstallprompt', function(event) {
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
});
// Installation must be done by a user gesture! Here, the button click
btnAdd.addEventListener('click', (e) => {
// hide our user interface that shows our A2HS button
btnAdd.style.display = 'none';
// Show the prompt
deferredPrompt.prompt();
// Wait for the user to respond to the prompt
deferredPrompt.userChoice
.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the A2HS prompt');
} else {
console.log('User dismissed the A2HS prompt');
}
deferredPrompt = null;
});
});
</script>
beforeinstallprompt will only be fired when some conditions are true :
The PWA must not already be installed
Meets a user engagement heuristic (previously, the user had to interact with the domain for at least 30 seconds, this is not a requirement anymore).
Your web app must include a web app manifest.
Your web app must be served over a secure HTTPS connection.
Has registered a service worker with a fetch event handler.
Along with all of those steps above, also check that the app is uninstalled here:
chrome://apps
Just deleting the app from the Chrome Apps folder on your Mac does not seem to remove it from Chrome
If the app was previously installed, the beforeinstallprompt will not be triggered, and no errors will be thrown either :(
Yes, the "start_url" is incorrect in the manifest.
IF ANY PART OF THE MANIFEST IS BROKEN 'beforeinstallprompt' is not fired.
The event is not fired because... the manifest start_url is incorrect.
My favorite way to figure this out is to look in the > NETWORK tab of DevTools for 404's.
AND the other way to see why manifest is broken is to run > AUDIT in DevTools and see what the error is. Like what #sealabr found:
"Failures: Service worker does not successfully serve the manifest's start_url, Timed out waiting for fetched start_url.' Which means the 'start_url"
This thread was a big help troubleshooting production. Thanks.
Are you including the manifest file in the page header?
<link rel="manifest" href="/manifest.json">
To whoever needs to read this: A little side-note that I ran into when working on my Vue3 app while trying to figure out the prompt:
The beforeInstallPrompt will trigger shortly after the page load.
So make sure you set up the event listener close to the page load. Took me a couple of hours to find this out. I was trying to add the event listener somewhere down the line of onboarding the user. Way after the page load. And couldn't figure out why the prompt didn't show.
here is another reason why beforeinstallprompt is not triggered on a mobile device (observed on an Android device with Chrome):
A symbol file defined in manifest.webmanifest could not be found on the web server (the mobile browser reported a 404). That was the reason why beforeinstallprompt was not triggered in my case.
#example
// your manifest
{
/* ... */
"icons": [
{
"src": "maskable_icon_x192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "any maskable"
},
/*...*/
]
}
Make sure that all icon files (such as maskable_icon_x192.png) are present on your web server.
All the best,
Tom
Related
My flutter web app won't start, I see the following errors in the browser console:
This behavior only occurs when deployed on vercel. I don't get this error when deploying on firebase hosting.
Furthermore, this error only occurs for nested routes. It works when I open my deployed app without a subpath in the URL.
The error must occur somewhere in the loadEntrypoint function
<script>
window.addEventListener('load', function (ev) {
console.log("LOAD!");
// Download main.dart.js
_flutter.loader.loadEntrypoint({
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
}
}).then(function (engineInitializer) {
console.log("INIT");
return engineInitializer.initializeEngine();
}).then(function (appRunner) {
console.log("RUN");
return appRunner.runApp();
});
});
</script>
Interesting here is that it says Failed to register a ServiceWorker for scope ('https://domainname.net/home/') even though I load the page https://domainname.net/home/questionnaire. In general I expect it would register the ServiceWorker at https://domainname.net but I don't know much about ServiceWorkers anyways...
I'm especially puzzled about this because this only happens on vercel but as the error occurs somewhere in web/index.js, it assume my hosting provider should have not influence on this behavior?
Any ideas?
It is caused by this bug: https://github.com/flutter/flutter/issues/116360
It is already fixed in master: https://github.com/flutter/flutter/pull/118684
Unfortunately, the fix is not in the stable channel yet (as of 3/02/22), but there is a cherry-pick request.
Temporal workaround: (ref)
Add these two lines in your index.html:
<script>
window.addEventListener('load', function(ev) {
// Download main.dart.js
_flutter.loader.loadEntrypoint({
entrypointUrl: "/main.dart.js", // <-- THIS LINE
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
serviceWorkerUrl: "/flutter_service_worker.js?v=", // <-- THIS LINE
},
onEntrypointLoaded: function(engineInitializer) {
engineInitializer.initializeEngine().then(function(appRunner) {
appRunner.runApp();
});
}
});
});
</script>
I have found a solution to this problem.
When I replace the script in my question above with the following script, it seems to resolve the issue.
var serviceWorkerVersion = null;
var scriptLoaded = false;
function loadMainDartJs() {
if (scriptLoaded) {
return;
}
scriptLoaded = true;
var scriptTag = document.createElement('script');
scriptTag.src = 'main.dart.js';
scriptTag.type = 'application/javascript';
document.body.append(scriptTag);
}
if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
}
});
}
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing ?? reg.waiting);
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('Loading app from service worker.');
loadMainDartJs();
}
});
// If service worker doesn't succeed in a reasonable amount of time,
// fallback to plaint <script> tag.
setTimeout(() => {
if (!scriptLoaded) {
console.warn(
'Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
});
} else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
Unfortunately, I have not really found out what exactly the problem was and why this is fixing it but I noticed this issue was occurring only since I updated my flutter version and regenerate my index.html. The script I'm now using is the script used by an older flutter version. I'm not sure if this has other effects but for now I'm going with the older script.
Run into the same issue today.
After trying many times, I realized the file flutter.js was not included in the build folder after running the command.
flutter build web
It was generating the build/web folder but missing some files. No idea why.
The fact is that after running the command flutter run web --release the folder build/web was populated correctly and I could see the "flutter.js" file in the folder.
After the deployment (in my case firebase hosting) everything was back to normal and working.
My app is under ionic 4 angular.
I've installed the pwa part with :
ng add #angular/pwa --project app
Then I build with : ionic build --prod
and deployed to firebase with : firebase deploy
But I have 2 problems :
1) the banner "add to screen" is not shown when I browse the app from my android phone.
Even with this code on the root url :
showBtn: boolean = false;
deferredPrompt;
constructor(private modalController: ModalController, public authUser: AuthUserService, private router: Router){}
ionViewWillEnter(){
window.addEventListener('beforeinstallprompt', (e) => {
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later on the button event.
this.deferredPrompt = e;
// Update UI by showing a button to notify the user they can add to home screen
this.showBtn = true;
});
//button click event to show the promt
window.addEventListener('appinstalled', (event) => {
alert('installed');
});
if (window.matchMedia('(display-mode: standalone)').matches) {
alert('display-mode is standalone');
}
}
2) When I launch lighthouse audit I get this warning :
Does not register a service worker that controls page and start_url
I've tried to uninstall, reinstall, rebuild everything but nothing works.
On ionic docs I can't find any clue to fix this problem.
After many days I was able to make it works.
First I add this following snippet to the firebase.json file to the hosting property:
{
"source": "ngsw-worker.js",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache"
}
]
}
Then I add this script in my index.html :
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('ngsw-worker.js')
.then(() => console.log('service worker installed'))
.catch(err => console.error('Error', err));
}
</script>
Now it works !
I'm trying to update a page in a Chrome App but I get the error:
Can't open same-window link to "chrome-extension://XXXX/page.html"; try target="_blank".
My manifest.json:
"app": {
"background": {
"scripts": [ "background.js" ]
},
"persistent": false
},
my background.js:
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('page.html', {
'outerBounds': {
'width': 7000,
'height': 7000
}
});
});
and a window.location.reload(); in page.js
P.S.: I considered chrome.runtime.reload();, but it will restart the window/browser, I just wanted to refresh the page without the window closing and opening.
I've created another page "main.html" and I'm loading the "page.html" from that one on an iframe and I'm able to reload it that way,
Just posting my own solution in hope that it might help someone.
I am trying to test my code with
.controller('AppCtrl', function ($scope, $ionicModal, $cordovaOauth, $localStorage, $location) {
$scope.login = function() {
$cordovaOauth.facebook("APPID", ["email", "read_stream", "user_website", "user_location", "user_relationships"]).then(function(result) {
$localStorage.accessToken = result.access_token;
alert("facebook login correctly");
$location.path("/profile");
}, function(error) {
alert(error);
console.log(error);
});
};
But all the time getting error:
cannot authenticate via a web browser
I am using ionic and cordovaOauth, when I am trying to test the app with android I don't get any response.The question is how can I debug it really with simulator or something easily like PhoneGap app, I tried to work with phone gap but can't know how can I debug it.
I've met the same problem and I have found this : https://github.com/nraboy/ng-cordova-oauth/issues/46
Basically, you should test your code in an emulator not in a browser.
I am attempting to set up a chrome extension similar to http://code.google.com/chrome/extensions/trunk/samples.html#webrequest except that it would us the onErrorOccurred listener instead to redirect to a specific known page when the get request fails for any reason.
manifest.json:
{
"name": "Custom Error",
"version": "0.1",
"description": "Redirect all navigation errors to specified location/file.",
"permissions": [
"webRequest",
"tabs",
"<all_urls>"
],
"background": {
"page": ["error_listener.html"]
}
}
error_listener.html:
<!doctype html>
<script>
chrome.webRequest.onErrorOccurred.addListener(
function onErrorOccurred(details) {
console.log('onBeforeRequest ', details.url);
return { redirectUrl: 'http://www.google.com' }
},
{urls: ["<all_urls>"]}
//{urls: ["http://*/*", "https://*/*"]}
);
//chrome.tabs.update(details.tabId, {url: "http://www.google.com", ['blocking']});
//alert("what?");
</script>
The extension loads without any errors indicated yet the browser tab is not redirected. I have tried this using both Chrome 16 and Chrome 17; when using Chrome 16, I did change "chrome.webRequest" to "chrome.experimental.webRequest" and added "experimental" to the permissions list.
So far it seems like the problem is that while the extension appears to be loaded when looking at chrome://extensions, the files are not actually loaded--when using Developer Tools, I don't see any reference to error_listener.html.
I have also tried running Chrome 17 with the following flags:
8611 25/01/12-11:22:05> google-chrome --restore-last-session
--debug-on-start --log-level=0 --enable-logging
--enable-extension-activity-logging --enable-extension-alerts
--debug-plugin-loading --debug-print | tee > log1.txt
Obviously, I am just kind of poking around in the dark with that command line. Anyone have any clue as to how to get this working? Thanks in advance for your help!
There is no webRequest.onErrorOccurred event. You may use webNavigation.onErrorOccurred. If you want to catch DNS errors and redirect to another url you may use the code:
<script>
chrome.webNavigation.onErrorOccurred.addListener(function(details)
{
if (details.frameId != 0) //ignore subframes. 0 is main frame
{ return; }
chrome.tabs.update(details.tabId, {url: "https://www.google.com/search?q=" + details.url});
});
</script>
In Chrome 16, you should be using:
"background_page": "background.html"
rather than
"background": {
"page": ["error_listener.html"]
}
This fixes the problem with the background page not loading. It looks like this may have changed in the trunk docs compared to the current docs, and I'm not sure which version of Chrome starts implementing the new manifest.json format.