diff --git a/app/lib/local-url.ts b/app/lib/local-url.ts new file mode 100644 index 0000000..468b859 --- /dev/null +++ b/app/lib/local-url.ts @@ -0,0 +1,12 @@ +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(/\/+$/, ''); +}