feat: make local dev URLs reachable from phones on the network

This commit is contained in:
rajun 2026-10-27 16:28:49 +05:30
parent d8d472bc60
commit ea26eeb4c2

12
app/lib/local-url.ts Normal file
View File

@ -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(/\/+$/, '');
}