From ea26eeb4c274ffd3283dc74a3477424c89282c67 Mon Sep 17 00:00:00 2001 From: rajun Date: Tue, 27 Oct 2026 16:28:49 +0530 Subject: [PATCH] feat: make local dev URLs reachable from phones on the network --- app/lib/local-url.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 app/lib/local-url.ts 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(/\/+$/, ''); +}