Best way to use the current URL as a parameter to external API in javascript

287 Views Asked by At

I would like to know what is the best way fetch a resource from our client's site to our servers with the Fetch API (GET). In order to do so we need to send the current URL since the resource depends on that.

We've come up with two choices so far:

  • Sending the current URL encoded with encodeURIComponent as part of the query string. Could be also base64 if it is safer. In this case, which is the best function that is PHP compatible for decoding?
  • Using the referer header of the HTTP Request. We found out that this works without additional effort but the default value for this is not consistent among browsers due to privacy concerns [1]. We could try to force the referrer Policy to no-referrer-when-downgrade for this particular request but in doing so we wouldn't want to hurt the user's privacy.

Thanks

[1] https://web.dev/referrer-best-practices/

0

There are 0 best solutions below