Open josefdlange opened 4 years ago
It could be done very simplisticly by checking if the first character of url
is the forward slash. If it is, then prepend window.location.origin
to the given url
. That would be consistent with the native Share API.
I think the native Share API throws an error when only the host is given without the protocol, i.e. example.com/articles/123
throws an error whereas https://example.com/articles/123
does not. So the polyfill should ideally throw an error in the case of a host without a protocol.
if (url.startsWith('/')) {
url = `${window.location.origin}${url}`
} else if (!url.startsWith('http')) {
// throw error
}
Sounds good. Happy to push a PR with that in a bit here. Thanks for getting back to me!
As you said yourself, I'm also not sure if this is documented behavior. I tested it on the new chromium-based Edge desktop browser. There, the protocol and host are prepended to absolute paths starting with /
.
I'm honestly not sure if this is documented behavior of the Web Share API but I'm seeing diverging behavior in this instance:
navigator.share
with theurl
argument as a/
-prefixed path, like/articles/123/
navigator.share
interprets that into<protocol>://<current-domain-and-port>/articles/123/
when passing through to share target. Kind of how thehref
of an<a>
element is understood correctly when it's just/articles/123/
./articles/123/
as the "URL" to any share target.I'm happy to work on this as a PR to this project but wanted to propose it first and get approval before investing the work.