From 3967a3f13fef8abf0d8dc11761f9670b47883851 Mon Sep 17 00:00:00 2001 From: IRHM Date: Wed, 29 Jul 2026 18:43:38 +0100 Subject: [PATCH] api: Update prod baseURL to be absolute Only an absolute baseURL will work with our new Reqer class. --- src/lib/util/api.ts | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/lib/util/api.ts b/src/lib/util/api.ts index a7004fca..2ca3a1ab 100644 --- a/src/lib/util/api.ts +++ b/src/lib/util/api.ts @@ -18,14 +18,37 @@ import { import { Reqer, ReqerError } from "./fetch"; import { notify, unNotify } from "./notify"; import { browser } from "$app/environment"; +import { page } from "$app/state"; const { MODE } = import.meta.env; -export const baseURL = - MODE === "development" - ? browser - ? `${location.protocol}//${location.hostname}:3080/api` - : "http://127.0.0.1:3080/api" - : "/api"; +export const baseURL: string = ((): string => { + try { + // NOTE: Only the values returned under the if (browser) statements matter, + // since we only use this variable from the browser, I've left in the + // untested fallbacks anyways. + if (MODE === "development") { + if (browser) { + return `${location.protocol}//${location.hostname}:3080/api`; + } + return "http://127.0.0.1:3080/api"; + } + // prod + if (browser) { + return `${location.origin}/api`; + } + return `${page.url.origin}/api`; + } catch (err) { + console.error("api: baseURL construction failed!", err); + if (browser) { + notify({ + type: "error", + text: "Failed to create your API baseURL. Please look in console for more details.", + }); + } + // This return value won't work, but just returning something anyways. + return "/api"; + } +})(); console.log("api: baseURL constructed:", baseURL); export const req = new Reqer(baseURL, true);