From 04e74f2ba32963932c0ab46ac4862e92c3e4e1e1 Mon Sep 17 00:00:00 2001 From: Phecda Su Date: Tue, 28 Mar 2023 22:50:39 +0800 Subject: [PATCH] fix: prevent open 0.0.0.0 in browser due windows problems --- lib/Server.js | 6 +++++- test/server/open-option.test.js | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 573707d640..7dfac5e169 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -2893,7 +2893,11 @@ class Server { } if (/** @type {NormalizedOpen[]} */ (this.options.open).length > 0) { - const openTarget = prettyPrintURL(this.options.host || "localhost"); + const openTarget = prettyPrintURL( + !this.options.host || this.options.host === "0.0.0.0" + ? "localhost" + : this.options.host + ); this.openBrowser(openTarget); } diff --git a/test/server/open-option.test.js b/test/server/open-option.test.js index 23e1cb1526..5a624b00bb 100644 --- a/test/server/open-option.test.js +++ b/test/server/open-option.test.js @@ -99,7 +99,7 @@ describe('"open" option', () => { }); }); - it("should work with '0.0.0.0' host", async () => { + it("should work with '0.0.0.0' host but open localhost", async () => { const host = "0.0.0.0"; const server = new Server( @@ -114,7 +114,7 @@ describe('"open" option', () => { await server.start(); await server.stop(); - expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, { + expect(open).toHaveBeenCalledWith(`http://localhost:${port}/`, { wait: false, }); });