File size: 792 Bytes
00a912e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const LOCALHOST_HOSTS = new Set(['localhost', '127.0.0.1', '::1'])

function trimTrailingSlash(url: string): string {
	return url.replace(/\/+$/, '')
}

function resolveServerUrl(): string {
	const configuredUrl = import.meta.env.VITE_SERVER_URL?.trim()

	if (configuredUrl) {
		try {
			const parsed = new URL(configuredUrl)
			if (import.meta.env.DEV && LOCALHOST_HOSTS.has(parsed.hostname) && !LOCALHOST_HOSTS.has(window.location.hostname)) {
				parsed.hostname = window.location.hostname
			}
			return trimTrailingSlash(parsed.toString())
		} catch {
			return trimTrailingSlash(configuredUrl)
		}
	}

	if (import.meta.env.DEV) {
		return `${window.location.protocol}//${window.location.hostname}:3001`
	}

	return window.location.origin
}

export const SERVER_URL = resolveServerUrl()