Home / Function/ handleMessage() — vite Function Reference

handleMessage() — vite Function Reference

Architecture documentation for the handleMessage() function in client.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  d1f1e08a_d62c_ffc8_40e7_039fffb49665["handleMessage()"]
  85908ff5_4d12_826d_8235_531f91538758["client.ts"]
  d1f1e08a_d62c_ffc8_40e7_039fffb49665 -->|defined in| 85908ff5_4d12_826d_8235_531f91538758
  efd2ed23_9225_aef5_6607_d3415a0b5352["notifyListeners()"]
  d1f1e08a_d62c_ffc8_40e7_039fffb49665 -->|calls| efd2ed23_9225_aef5_6607_d3415a0b5352
  99d08ed2_ad2d_3826_ded4_2c220d8efeed["hasErrorOverlay()"]
  d1f1e08a_d62c_ffc8_40e7_039fffb49665 -->|calls| 99d08ed2_ad2d_3826_ded4_2c220d8efeed
  bb515d8a_1083_ea46_dfdc_ec1e75395bb2["clearErrorOverlay()"]
  d1f1e08a_d62c_ffc8_40e7_039fffb49665 -->|calls| bb515d8a_1083_ea46_dfdc_ec1e75395bb2
  dc6afbd1_68d8_b85c_ede6_e2f3839fdccd["queueUpdate()"]
  d1f1e08a_d62c_ffc8_40e7_039fffb49665 -->|calls| dc6afbd1_68d8_b85c_ede6_e2f3839fdccd
  70809559_bf4a_cc1b_4ee8_db539eabb215["cleanUrl()"]
  d1f1e08a_d62c_ffc8_40e7_039fffb49665 -->|calls| 70809559_bf4a_cc1b_4ee8_db539eabb215
  62c89f60_860f_098a_c8e3_9d5d77bd220d["waitForSuccessfulPing()"]
  d1f1e08a_d62c_ffc8_40e7_039fffb49665 -->|calls| 62c89f60_860f_098a_c8e3_9d5d77bd220d
  877e0b79_8207_e08f_1255_5ea1250a15c6["prunePaths()"]
  d1f1e08a_d62c_ffc8_40e7_039fffb49665 -->|calls| 877e0b79_8207_e08f_1255_5ea1250a15c6
  48d70ee4_37db_090f_6d7b_43252d2444a1["createErrorOverlay()"]
  d1f1e08a_d62c_ffc8_40e7_039fffb49665 -->|calls| 48d70ee4_37db_090f_6d7b_43252d2444a1
  style d1f1e08a_d62c_ffc8_40e7_039fffb49665 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/client/client.ts lines 199–331

async function handleMessage(payload: HotPayload) {
  switch (payload.type) {
    case 'connected':
      console.debug(`[vite] connected.`)
      break
    case 'update':
      await hmrClient.notifyListeners('vite:beforeUpdate', payload)
      if (hasDocument) {
        // if this is the first update and there's already an error overlay, it
        // means the page opened with existing server compile error and the whole
        // module script failed to load (since one of the nested imports is 500).
        // in this case a normal update won't work and a full reload is needed.
        if (isFirstUpdate && hasErrorOverlay()) {
          location.reload()
          return
        } else {
          if (enableOverlay) {
            clearErrorOverlay()
          }
          isFirstUpdate = false
        }
      }
      await Promise.all(
        payload.updates.map(async (update): Promise<void> => {
          if (update.type === 'js-update') {
            return hmrClient.queueUpdate(update)
          }

          // css-update
          // this is only sent when a css file referenced with <link> is updated
          const { path, timestamp } = update
          const searchUrl = cleanUrl(path)
          // can't use querySelector with `[href*=]` here since the link may be
          // using relative paths so we need to use link.href to grab the full
          // URL for the include check.
          const el = Array.from(
            document.querySelectorAll<HTMLLinkElement>('link'),
          ).find(
            (e) =>
              !outdatedLinkTags.has(e) && cleanUrl(e.href).includes(searchUrl),
          )

          if (!el) {
            return
          }

          const newPath = `${base}${searchUrl.slice(1)}${
            searchUrl.includes('?') ? '&' : '?'
          }t=${timestamp}`

          // rather than swapping the href on the existing tag, we will
          // create a new link tag. Once the new stylesheet has loaded we
          // will remove the existing link tag. This removes a Flash Of
          // Unstyled Content that can occur when swapping out the tag href
          // directly, as the new stylesheet has not yet been loaded.
          return new Promise((resolve) => {
            const newLinkTag = el.cloneNode() as HTMLLinkElement
            newLinkTag.href = new URL(newPath, el.href).href
            const removeOldEl = () => {
              el.remove()
              console.debug(`[vite] css hot updated: ${searchUrl}`)
              resolve()
            }
            newLinkTag.addEventListener('load', removeOldEl)
            newLinkTag.addEventListener('error', removeOldEl)
            outdatedLinkTags.add(el)
            el.after(newLinkTag)
          })
        }),
      )
      await hmrClient.notifyListeners('vite:afterUpdate', payload)
      break
    case 'custom': {
      await hmrClient.notifyListeners(payload.event, payload.data)
      if (payload.event === 'vite:ws:disconnect') {
        if (hasDocument && !willUnload) {
          console.log(`[vite] server connection lost. Polling for restart...`)
          const socket = payload.data.webSocket as WebSocket
          const url = new URL(socket.url)
          url.search = '' // remove query string including `token`
          await waitForSuccessfulPing(url.href)

Domain

Subdomains

Frequently Asked Questions

What does handleMessage() do?
handleMessage() is a function in the vite codebase, defined in packages/vite/src/client/client.ts.
Where is handleMessage() defined?
handleMessage() is defined in packages/vite/src/client/client.ts at line 199.
What does handleMessage() call?
handleMessage() calls 8 function(s): cleanUrl, clearErrorOverlay, createErrorOverlay, hasErrorOverlay, notifyListeners, prunePaths, queueUpdate, waitForSuccessfulPing.

Analyze Your Own Codebase

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

Try Supermodel Free