13 lines
731 B
TypeScript
13 lines
731 B
TypeScript
const LOOPBACK = /^(localhost|127\.0\.0\.1|\[::1\])$/;
|
|
|
|
// Local dev addresses (localhost / 127.0.0.1) only work on the computer running the servers. When the app is opened
|
|
// from another device on the network (e.g. a phone at http://10.20.2.120:5173), use the address the page was opened
|
|
// with instead, so that device reaches the same computer. Any other URL (production) is returned unchanged.
|
|
export function reachableUrl(url: string): string {
|
|
if (typeof window === 'undefined') return url;
|
|
const target = new URL(url);
|
|
if (!LOOPBACK.test(target.hostname) || LOOPBACK.test(window.location.hostname)) return url;
|
|
target.hostname = window.location.hostname;
|
|
return target.toString().replace(/\/+$/, '');
|
|
}
|