From 524c51659c59c73c65a86f672fe0d23bbcb31fec Mon Sep 17 00:00:00 2001 From: Vladislav Kibenko Date: Sun, 17 Nov 2024 01:14:32 +0500 Subject: [PATCH] Fix publicUrl function --- src/helpers/publicUrl.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/helpers/publicUrl.ts b/src/helpers/publicUrl.ts index 05a6d9c..1bdb0de 100644 --- a/src/helpers/publicUrl.ts +++ b/src/helpers/publicUrl.ts @@ -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(); } \ No newline at end of file