Home / Function/ serveStaticMiddleware() — vite Function Reference

serveStaticMiddleware() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  7e76fb1f_0d85_1b5a_15ce_269a9f256af9["serveStaticMiddleware()"]
  d91d6c8f_e2c2_3b92_12c3_391c9ba06183["static.ts"]
  7e76fb1f_0d85_1b5a_15ce_269a9f256af9 -->|defined in| d91d6c8f_e2c2_3b92_12c3_391c9ba06183
  24ecf2a1_3c09_d451_76f3_9485b4e993f8["_createServer()"]
  24ecf2a1_3c09_d451_76f3_9485b4e993f8 -->|calls| 7e76fb1f_0d85_1b5a_15ce_269a9f256af9
  449af0c1_44a5_1ad5_ce35_a183c1ef89e6["sirvOptions()"]
  7e76fb1f_0d85_1b5a_15ce_269a9f256af9 -->|calls| 449af0c1_44a5_1ad5_ce35_a183c1ef89e6
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  7e76fb1f_0d85_1b5a_15ce_269a9f256af9 -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  02cc1763_d5fc_6d3a_58ef_310caf680b7e["isInternalRequest()"]
  7e76fb1f_0d85_1b5a_15ce_269a9f256af9 -->|calls| 02cc1763_d5fc_6d3a_58ef_310caf680b7e
  b785bb42_81b4_52a8_f27e_13aacc0eea06["decodeURIIfPossible()"]
  7e76fb1f_0d85_1b5a_15ce_269a9f256af9 -->|calls| b785bb42_81b4_52a8_f27e_13aacc0eea06
  1a3bec7b_1a11_316f_5831_a0535b829bbf["withTrailingSlash()"]
  7e76fb1f_0d85_1b5a_15ce_269a9f256af9 -->|calls| 1a3bec7b_1a11_316f_5831_a0535b829bbf
  821f5140_d604_b209_3c9a_17ee29fefe11["removeLeadingSlash()"]
  7e76fb1f_0d85_1b5a_15ce_269a9f256af9 -->|calls| 821f5140_d604_b209_3c9a_17ee29fefe11
  f06a4729_72a9_90d9_1358_3db737d66a6e["respondWithAccessDenied()"]
  7e76fb1f_0d85_1b5a_15ce_269a9f256af9 -->|calls| f06a4729_72a9_90d9_1358_3db737d66a6e
  style 7e76fb1f_0d85_1b5a_15ce_269a9f256af9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/middlewares/static.ts lines 124–199

export function serveStaticMiddleware(
  server: ViteDevServer,
): Connect.NextHandleFunction {
  const dir = server.config.root
  const serve = sirv(
    dir,
    sirvOptions({
      config: server.config,
      getHeaders: () => server.config.server.headers,
    }),
  )

  // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
  return function viteServeStaticMiddleware(req, res, next) {
    // only serve the file if it's not an html request or ends with `/`
    // so that html requests can fallthrough to our html middleware for
    // special processing
    // also skip internal requests `/@fs/ /@vite-client` etc...
    const cleanedUrl = cleanUrl(req.url!)
    if (
      cleanedUrl.endsWith('/') ||
      path.extname(cleanedUrl) === '.html' ||
      isInternalRequest(req.url!) ||
      // skip url starting with // as these will be interpreted as
      // scheme relative URLs by new URL() and will not be a valid file path
      req.url?.startsWith('//')
    ) {
      return next()
    }

    const url = new URL(req.url!, 'http://example.com')
    const pathname = decodeURIIfPossible(url.pathname)
    if (pathname === undefined) {
      return next()
    }

    // apply aliases to static requests as well
    let redirectedPathname: string | undefined
    for (const { find, replacement } of server.config.resolve.alias) {
      const matches =
        typeof find === 'string'
          ? pathname.startsWith(find)
          : find.test(pathname)
      if (matches) {
        redirectedPathname = pathname.replace(find, replacement)
        break
      }
    }
    if (redirectedPathname) {
      // dir is pre-normalized to posix style
      if (redirectedPathname.startsWith(withTrailingSlash(dir))) {
        redirectedPathname = redirectedPathname.slice(dir.length)
      }
    }

    const resolvedPathname = redirectedPathname || pathname
    let fileUrl = path.resolve(dir, removeLeadingSlash(resolvedPathname))
    if (resolvedPathname.endsWith('/') && fileUrl[fileUrl.length - 1] !== '/') {
      fileUrl = withTrailingSlash(fileUrl)
    }
    if (redirectedPathname) {
      url.pathname = encodeURI(redirectedPathname)
      req.url = url.href.slice(url.origin.length)
    }

    try {
      serve(req, res, next)
    } catch (e) {
      if (e && 'code' in e && e.code === ERR_DENIED_FILE) {
        respondWithAccessDenied(e.path, server, res)
        return
      }
      throw e
    }
  }
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does serveStaticMiddleware() do?
serveStaticMiddleware() is a function in the vite codebase, defined in packages/vite/src/node/server/middlewares/static.ts.
Where is serveStaticMiddleware() defined?
serveStaticMiddleware() is defined in packages/vite/src/node/server/middlewares/static.ts at line 124.
What does serveStaticMiddleware() call?
serveStaticMiddleware() calls 7 function(s): cleanUrl, decodeURIIfPossible, isInternalRequest, removeLeadingSlash, respondWithAccessDenied, sirvOptions, withTrailingSlash.
What calls serveStaticMiddleware()?
serveStaticMiddleware() is called by 1 function(s): _createServer.

Analyze Your Own Codebase

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

Try Supermodel Free