Skip to content

Commit 6abbdce

Browse files
authored
fix: open first url if host does not match any urls (#19886)
1 parent f8dc424 commit 6abbdce

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

packages/vite/src/node/__tests__/utils.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
generateCodeFrame,
1313
getHash,
1414
getLocalhostAddressIfDiffersFromDNS,
15+
getServerUrlByHost,
1516
injectQuery,
1617
isFileReadable,
1718
mergeWithDefaults,
@@ -21,6 +22,7 @@ import {
2122
resolveHostname,
2223
} from '../utils'
2324
import { isWindows } from '../../shared/utils'
25+
import type { CommonServerOptions, ResolvedServerUrls } from '..'
2426

2527
describe('bareImportRE', () => {
2628
test('should work with normal package name', () => {
@@ -725,3 +727,54 @@ describe('combineSourcemaps', () => {
725727
)
726728
})
727729
})
730+
731+
describe('getServerUrlByHost', () => {
732+
const urls: ResolvedServerUrls = {
733+
local: ['http://localhost:5173'],
734+
network: ['http://foo.example.com:5173'],
735+
}
736+
const cases = [
737+
{
738+
name: 'when host is undefined',
739+
urls,
740+
host: undefined,
741+
expected: 'http://localhost:5173',
742+
},
743+
{
744+
name: 'when host is true',
745+
urls,
746+
host: true,
747+
expected: 'http://localhost:5173',
748+
},
749+
{
750+
name: 'when host is explicit string',
751+
urls,
752+
host: 'foo.example.com',
753+
expected: 'http://foo.example.com:5173',
754+
},
755+
{
756+
name: 'when host is 0.0.0.0',
757+
urls,
758+
host: '0.0.0.0',
759+
expected: 'http://localhost:5173',
760+
},
761+
{
762+
name: 'when host is ::1',
763+
urls,
764+
host: '::1',
765+
expected: 'http://localhost:5173',
766+
},
767+
] satisfies ReadonlyArray<{
768+
name: string
769+
urls: ResolvedServerUrls
770+
host: CommonServerOptions['host']
771+
expected: string | undefined
772+
}>
773+
774+
for (const { name, urls, host, expected } of cases) {
775+
test(name, () => {
776+
const actual = getServerUrlByHost(urls, host)
777+
expect(actual).toBe(expected)
778+
})
779+
}
780+
})

packages/vite/src/node/preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export async function preview(
277277
)
278278

279279
if (options.open) {
280-
const url = getServerUrlByHost(server, options.host)
280+
const url = getServerUrlByHost(server.resolvedUrls, options.host)
281281
if (url) {
282282
const path =
283283
typeof options.open === 'string' ? new URL(options.open, url).href : url

packages/vite/src/node/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ export async function _createServer(
660660
},
661661
openBrowser() {
662662
const options = server.config.server
663-
const url = getServerUrlByHost(server, options.host)
663+
const url = getServerUrlByHost(server.resolvedUrls, options.host)
664664
if (url) {
665665
const path =
666666
typeof options.open === 'string'

packages/vite/src/node/utils.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,15 +1658,17 @@ export const teardownSIGTERMListener = (
16581658
}
16591659

16601660
export function getServerUrlByHost(
1661-
server: PreviewServer | ViteDevServer,
1661+
resolvedUrls: ResolvedServerUrls | null,
16621662
host: CommonServerOptions['host'],
16631663
): string | undefined {
1664-
if (typeof host !== 'string') {
1665-
return server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0]
1664+
if (typeof host === 'string') {
1665+
const matchedUrl = [
1666+
...(resolvedUrls?.local ?? []),
1667+
...(resolvedUrls?.network ?? []),
1668+
].find((url) => url.includes(host))
1669+
if (matchedUrl) {
1670+
return matchedUrl
1671+
}
16661672
}
1667-
1668-
return [
1669-
...(server.resolvedUrls?.local ?? []),
1670-
...(server.resolvedUrls?.network ?? []),
1671-
].find((url) => url.includes(host))
1673+
return resolvedUrls?.local[0] ?? resolvedUrls?.network[0]
16721674
}

0 commit comments

Comments
 (0)