Skip to content

Commit

Permalink
Fix publicUrl function
Browse files Browse the repository at this point in the history
  • Loading branch information
heyqbnk committed Nov 16, 2024
1 parent 6becb39 commit 524c516
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/helpers/publicUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,30 @@
* @param path - path to prepend prefix to
*/
export function publicUrl(path: string): string {
// The baseUrl must be ending with the slash. The reason is if the baseUrl will
// equal to "/my-base", then passing the path equal to "tonconnect-manifest.json" will not
// give us the expected result, it will actually be "/tonconnect-manifest.json", but the expected
// one is "/my-base/tonconnect-manifest.json". This is due to the URL constructor.
let baseUrl = import.meta.env.BASE_URL;
if (!baseUrl.endsWith('/')) {
baseUrl += '/';
}

let isBaseAbsolute = false;
try {
new URL(baseUrl);
isBaseAbsolute = true;
} catch { /* empty */
}

return new URL(
// The path is not allowed to be starting with the slash as long as it will break the
// base URL. For instance, having the "/my-base/" base URL and path
// equal to "/tonconnect-manifest.json", we will not get the expected result like
// "/my-base/tonconnect-manifest.json", but "/tonconnect-manifest.json".
path.replace(/^\/+/, ''),
window.location.origin + import.meta.env.BASE_URL
isBaseAbsolute
? baseUrl
: window.location.origin + baseUrl,
).toString();
}

0 comments on commit 524c516

Please sign in to comment.