Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absolute path url arguments do not get merged with current domain before passing through to share targets in fallback mode #61

Open
josefdlange opened this issue Nov 12, 2020 · 3 comments

Comments

@josefdlange
Copy link

I'm honestly not sure if this is documented behavior of the Web Share API but I'm seeing diverging behavior in this instance:

  1. Call navigator.share with the url argument as a /-prefixed path, like /articles/123/
  2. Native navigator.share interprets that into <protocol>://<current-domain-and-port>/articles/123/ when passing through to share target. Kind of how the href of an <a> element is understood correctly when it's just /articles/123/.
  3. Polyfill fallback behavior does not do this and just passes through /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.

@josefdlange josefdlange changed the title Absolute path url arguments are not fixed to be at the current page's domain Absolute path url arguments do not get merged with current domain before passing through to share targets in fallback mode Nov 12, 2020
@valentinoli
Copy link
Contributor

valentinoli commented Nov 24, 2020

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
}

@josefdlange
Copy link
Author

Sounds good. Happy to push a PR with that in a bit here. Thanks for getting back to me!

@valentinoli
Copy link
Contributor

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 /.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants