Home / Function/ resolveServerUrls() — vite Function Reference

resolveServerUrls() — vite Function Reference

Architecture documentation for the resolveServerUrls() function in utils.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  0459d58b_04e4_11c6_50b5_ce20e10714a2["resolveServerUrls()"]
  031bc221_67a8_c579_f2bf_bb30a08beeb2["utils.ts"]
  0459d58b_04e4_11c6_50b5_ce20e10714a2 -->|defined in| 031bc221_67a8_c579_f2bf_bb30a08beeb2
  5c50110b_5c76_c14f_b1dd_3efd3df7f375["preview()"]
  5c50110b_5c76_c14f_b1dd_3efd3df7f375 -->|calls| 0459d58b_04e4_11c6_50b5_ce20e10714a2
  24ecf2a1_3c09_d451_76f3_9485b4e993f8["_createServer()"]
  24ecf2a1_3c09_d451_76f3_9485b4e993f8 -->|calls| 0459d58b_04e4_11c6_50b5_ce20e10714a2
  a45bfbed_fe1c_16b4_78b4_e482556a50d1["extractHostnamesFromCerts()"]
  0459d58b_04e4_11c6_50b5_ce20e10714a2 -->|calls| a45bfbed_fe1c_16b4_78b4_e482556a50d1
  style 0459d58b_04e4_11c6_50b5_ce20e10714a2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/utils.ts lines 1007–1070

export function resolveServerUrls(
  server: Server,
  options: CommonServerOptions,
  hostname: Hostname,
  httpsOptions: HttpsServerOptions | undefined,
  config: ResolvedConfig,
): ResolvedServerUrls {
  const address = server.address()

  const isAddressInfo = (x: any): x is AddressInfo => x?.address
  if (!isAddressInfo(address)) {
    return { local: [], network: [] }
  }

  const local: string[] = []
  const network: string[] = []
  const protocol = options.https ? 'https' : 'http'
  const port = address.port
  const base =
    config.rawBase === './' || config.rawBase === '' ? '/' : config.rawBase

  if (hostname.host !== undefined && !wildcardHosts.has(hostname.host)) {
    let hostnameName = hostname.name
    // ipv6 host
    if (hostnameName.includes(':')) {
      hostnameName = `[${hostnameName}]`
    }
    const address = `${protocol}://${hostnameName}:${port}${base}`
    if (loopbackHosts.has(hostname.host)) {
      local.push(address)
    } else {
      network.push(address)
    }
  } else {
    Object.values(os.networkInterfaces())
      .flatMap((nInterface) => nInterface ?? [])
      .filter((detail) => detail.address && detail.family === 'IPv4')
      .forEach((detail) => {
        let host = detail.address.replace('127.0.0.1', hostname.name)
        // ipv6 host
        if (host.includes(':')) {
          host = `[${host}]`
        }
        const url = `${protocol}://${host}:${port}${base}`
        if (detail.address.includes('127.0.0.1')) {
          local.push(url)
        } else {
          network.push(url)
        }
      })
  }

  const hostnamesFromCert = extractHostnamesFromCerts(httpsOptions?.cert)
  if (hostnamesFromCert.length > 0) {
    const existings = new Set([...local, ...network])
    local.push(
      ...hostnamesFromCert
        .map((hostname) => `${protocol}://${hostname}:${port}${base}`)
        .filter((url) => !existings.has(url)),
    )
  }

  return { local, network }
}

Domain

Subdomains

Frequently Asked Questions

What does resolveServerUrls() do?
resolveServerUrls() is a function in the vite codebase, defined in packages/vite/src/node/utils.ts.
Where is resolveServerUrls() defined?
resolveServerUrls() is defined in packages/vite/src/node/utils.ts at line 1007.
What does resolveServerUrls() call?
resolveServerUrls() calls 1 function(s): extractHostnamesFromCerts.
What calls resolveServerUrls()?
resolveServerUrls() is called by 2 function(s): _createServer, preview.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free