Skip to content

Commit 694d783

Browse files
committed
fix(preview): open local environment ports on localhost
A Vite server bound to ::1 or 127.0.0.1 was advertised as that exact host, so a dual-stack Chromium guest often failed to load the other stack. Environment-port URLs now use localhost so the guest can reach either bind.
1 parent cd096b9 commit 694d783

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎apps/web/src/browser/browserTargetResolver.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,19 @@ describe("browser target resolver", () => {
173173
kind: "environment-port",
174174
port: 5173,
175175
}).resolvedUrl,
176-
).toBe("http://[::1]:5173/");
176+
).toBe("http://localhost:5173/");
177+
});
178+
179+
it("maps local IPv4 environment ports onto localhost for dual-stack guests", async () => {
180+
readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });
181+
const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
182+
expect(
183+
resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
184+
kind: "environment-port",
185+
port: 5173,
186+
path: "/app",
187+
}).resolvedUrl,
188+
).toBe("http://localhost:5173/app");
177189
});
178190

179191
it("leaves malformed input for the normal navigation error path", async () => {

‎apps/web/src/browser/browserTargetResolver.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,13 @@ const resolveEnvironmentPortTarget = (
178178
const protocol = target.protocol ?? "http";
179179
const path = target.path?.startsWith("/") ? target.path : `/${target.path ?? ""}`;
180180
const normalizedEnvironmentHost = environmentUrl.hostname.replace(/^\[|\]$/g, "");
181-
const resolvedHost = normalizedEnvironmentHost.includes(":")
182-
? `[${normalizedEnvironmentHost}]`
183-
: normalizedEnvironmentHost;
181+
// Local loopback environments should advertise `localhost` so Chromium
182+
// dual-stack lookup can reach a Vite server bound only to ::1 or 127.0.0.1.
183+
const resolvedHost = isLocalLoopbackHost(normalizedEnvironmentHost)
184+
? "localhost"
185+
: normalizedEnvironmentHost.includes(":")
186+
? `[${normalizedEnvironmentHost}]`
187+
: normalizedEnvironmentHost;
184188
const resolved = sourceUrl
185189
? new URL(sourceUrl)
186190
: new URL(path, `${protocol}://${resolvedHost}:${target.port}`);

0 commit comments

Comments
 (0)