Home / Function/ send() — vite Function Reference

send() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  7c73fde5_2ada_95e2_0b3f_1ad78222da67["send()"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93["send.ts"]
  7c73fde5_2ada_95e2_0b3f_1ad78222da67 -->|defined in| b6714c40_d79b_b9e8_4a00_ef2de34c1c93
  fc571085_3ab0_feca_5965_61c2be1d5c08["indexHtmlMiddleware()"]
  fc571085_3ab0_feca_5965_61c2be1d5c08 -->|calls| 7c73fde5_2ada_95e2_0b3f_1ad78222da67
  13770e2a_c6d8_fb1f_6562_e66de7bc1deb["transformMiddleware()"]
  13770e2a_c6d8_fb1f_6562_e66de7bc1deb -->|calls| 7c73fde5_2ada_95e2_0b3f_1ad78222da67
  0d5a3ce6_a346_6e0c_cb12_77d5afc6444c["getCodeWithSourcemap()"]
  7c73fde5_2ada_95e2_0b3f_1ad78222da67 -->|calls| 0d5a3ce6_a346_6e0c_cb12_77d5afc6444c
  3f57c8be_be57_4cf4_aa11_4ed077229c70["removeTimestampQuery()"]
  7c73fde5_2ada_95e2_0b3f_1ad78222da67 -->|calls| 3f57c8be_be57_4cf4_aa11_4ed077229c70
  style 7c73fde5_2ada_95e2_0b3f_1ad78222da67 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/send.ts lines 32–101

export function send(
  req: IncomingMessage,
  res: ServerResponse,
  content: string | Buffer,
  type: string,
  options: SendOptions,
): void {
  const {
    etag = getEtag(content, { weak: true }),
    cacheControl = 'no-cache',
    headers,
    map,
  } = options

  if (res.writableEnded) {
    return
  }

  if (req.headers['if-none-match'] === etag) {
    res.statusCode = 304
    res.end()
    return
  }

  res.setHeader('Content-Type', alias[type] || type)
  res.setHeader('Cache-Control', cacheControl)
  res.setHeader('Etag', etag)

  if (headers) {
    for (const name in headers) {
      res.setHeader(name, headers[name]!)
    }
  }

  // inject source map reference
  if (map && 'version' in map && map.mappings) {
    if (type === 'js' || type === 'css') {
      content = getCodeWithSourcemap(type, content.toString(), map)
    }
  }
  // inject fallback sourcemap for js for improved debugging
  // https://github.com/vitejs/vite/pull/13514#issuecomment-1592431496
  else if (type === 'js' && (!map || map.mappings !== '')) {
    const code = content.toString()
    // if the code has existing inline sourcemap, assume it's correct and skip
    if (convertSourceMap.mapFileCommentRegex.test(code)) {
      debug?.(`Skipped injecting fallback sourcemap for ${req.url}`)
    } else {
      const urlWithoutTimestamp = removeTimestampQuery(req.url!)
      const ms = new MagicString(code)
      content = getCodeWithSourcemap(
        type,
        code,
        ms.generateMap({
          source: path.basename(urlWithoutTimestamp),
          hires: 'boundary',
          includeContent: true,
        }) as SourceMap,
      )
    }
  }

  res.statusCode = 200
  if (req.method === 'HEAD') {
    res.end()
  } else {
    res.end(content)
  }
  return
}

Domain

Subdomains

Frequently Asked Questions

What does send() do?
send() is a function in the vite codebase, defined in packages/vite/src/node/server/send.ts.
Where is send() defined?
send() is defined in packages/vite/src/node/server/send.ts at line 32.
What does send() call?
send() calls 2 function(s): getCodeWithSourcemap, removeTimestampQuery.
What calls send()?
send() is called by 2 function(s): indexHtmlMiddleware, transformMiddleware.

Analyze Your Own Codebase

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

Try Supermodel Free