MicrosoftEdge / MSEdgeExplainers

Home for explainer documents originated by the Microsoft Edge team
Creative Commons Attribution 4.0 International
1.29k stars 205 forks source link

[Web Install] Make navigation.install do the right thing for same-origin installation #658

Closed dfabulich closed 1 year ago

dfabulich commented 1 year ago

onbeforeinstallprompt has always had a weirdness in its API: you have to store a reference to the onbeforeinstallprompt event and use that event object to initiate the prompt.

Code sample from MDN:

let installPrompt = null;
const installButton = document.querySelector("#install");

window.addEventListener("beforeinstallprompt", (event) => {
  event.preventDefault();
  installPrompt = event;
  installButton.removeAttribute("hidden");
});
installButton.addEventListener("click", async () => {
  if (!installPrompt) {
    return;
  }
  const result = await installPrompt.prompt();
  console.log(`Install prompt was: ${result.outcome}`);
  installPrompt = null;
  installButton.setAttribute("hidden", "");
});

It would be nice if navigator.install() would do the right thing for same-origin installation as well, perhaps with no URL parameter in that case. That could change the sample to:

const installButton = document.querySelector("#install");

window.addEventListener("beforeinstallprompt", (event) => {
  event.preventDefault();
  installButton.removeAttribute("hidden");
});
installButton.addEventListener("click", () => {
  navigator.install().then( origin => console.log( `Installation started from ${origin}` ));
});
diekus commented 1 year ago

+1. We're looking into the implications of this. It'd be nice to have one consistent ergonomic API that would cover both installation types (same and cross domain).

diekus commented 1 year ago

We'd be keeping the same threshold of onbeforeinstallprompt for navigator.install() same domain installs, but plan at phasing out the former for the latter. Do let me know your thoughts, and if everything seems alright I'll proceed to close this issue.