/**
* Runtime configuration, written by the server as `config.js` when it places a
* build — never baked in.
*
* One build is promoted between environments unchanged, so an API URL compiled
* at build time would be the *source* environment's URL everywhere it lands.
*
* SAME_ORIGIN exists because an empty string is falsy and could never survive
* the `||` chain, which is exactly what "use the current origin" has to do.
*/
export function resolveConfigValue(key, fallback = '') {
const runtime = (window.__RUNTIME_CONFIG__ || {})[key]
// Gated on DEV: a production build with no config.js must fail loudly rather
// than quietly calling whichever backend happened to build it.
const devFallback = import.meta.env.DEV ? (import.meta.env[key] ?? '') : ''
const value = runtime || devFallback || fallback
return value === 'SAME_ORIGIN' ? window.location.origin.replace(/\/$/, '') : value
}
export function requireConfigValue(key) {
const value = resolveConfigValue(key)
if (!value) {
throw new Error(
`Missing runtime config "${key}". The server writes config.js when it ` +
`places the build; for local dev set ${key} in .env.`,
)
}
return value
}
/**
* The mount path this app is served under, read from the `` the
* server writes into index.html. Drives the router basename, so one build
* serves any mount path without a rebuild.
*
* `import.meta.env.BASE_URL` is NOT usable here — with Vite's relative base it
* resolves to "./".
*
* It reads the TAG, not `document.baseURI`. With no in the document —
* which is every `npm run dev` session, where the placeholder is still an HTML
* comment — baseURI falls back to the current page URL, so the basename became
* whatever deep path happened to be loaded. Open /lead/16911 directly and every
* link then rendered under it: /lead/16911/lead/11411, and again on the next
* click. Unmounted means mounted at the root.
*/
export function basePath() {
const tag = document.querySelector('base[href]')
if (!tag) return '/'
const path = new URL(tag.href, window.location.origin).pathname
// A relative href ("./") resolves against the current URL and would reintroduce
// exactly the same drift. Only an absolute mount path is a mount path.
return tag.getAttribute('href').startsWith('/') ? path : '/'
}