Web Share API fails on second execution

703 views Asked by At

I implemented the navigator.share() function in my code. It works on the first call, but when I try to call it again through the same event, I get the NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.

I found out that this is usually caused by navigator.share() not being directly called by user input, formerly also if called in a fetch callback. However, today it should be allowed as part of a fetch callback. The behaviour is also strange considering that it works on the first try and just fails on consecutive ones.

Any ideas would be highly appreciated.

1

There are 1 answers

3
DenverCoder9 On BEST ANSWER

Most probably you are not catching the exception the API throws when the user cancels the share. I just tried canceling and then sharing again for a random article on my blog, and it worked. Here is the code I use:

const share = async () => {
  try {
    await navigator.share({
      title: '',
      text: `“${document.title}” by @tomayac:`,
      url: document.querySelector('link[rel=canonical]').href,
    });
  } catch (err) {
    console.warn(err.name, err.message);
  }
};