fix(preview): open local environment ports on localhost (#7300)

This commit is contained in:
Guilherme Barros authored and GitHub committed 2026-08-18 13:39:08 -04:00
1 parent 1896f39a38
commit a87f691bd4
2 files changed
+20 -4

No files matched your search

@@ -173,7 +173,19 @@ describe("browser target resolver", () => {
kind: "environment-port",
port: 5173,
}).resolvedUrl,
).toBe("http://[::1]:5173/");
).toBe("http://localhost:5173/");
});
it("maps local IPv4 environment ports onto localhost for dual-stack guests", async () => {
readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });
const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");
expect(
resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {
kind: "environment-port",
port: 5173,
path: "/app",
}).resolvedUrl,
).toBe("http://localhost:5173/app");
});
it("leaves malformed input for the normal navigation error path", async () => {
@@ -178,9 +178,13 @@ const resolveEnvironmentPortTarget = (
const protocol = target.protocol ?? "http";
const path = target.path?.startsWith("/") ? target.path : `/${target.path ?? ""}`;
const normalizedEnvironmentHost = environmentUrl.hostname.replace(/^\[|\]$/g, "");
const resolvedHost = normalizedEnvironmentHost.includes(":")
? `[${normalizedEnvironmentHost}]`
: normalizedEnvironmentHost;
// Local loopback environments should advertise `localhost` so Chromium
// dual-stack lookup can reach a Vite server bound only to ::1 or 127.0.0.1.
const resolvedHost = isLocalLoopbackHost(normalizedEnvironmentHost)
? "localhost"
: normalizedEnvironmentHost.includes(":")
? `[${normalizedEnvironmentHost}]`
: normalizedEnvironmentHost;
const resolved = sourceUrl
? new URL(sourceUrl)
: new URL(path, `${protocol}://${resolvedHost}:${target.port}`);