MCPcopy
hub / github.com/vitejs/vite / servePublicMiddleware

Function servePublicMiddleware

packages/vite/src/node/server/middlewares/static.ts:80–122  ·  view source on GitHub ↗
(
  server: ViteDevServer,
  publicFiles?: Set<string>,
)

Source from the content-addressed store, hash-verified

78}
79
80export function servePublicMiddleware(
81 server: ViteDevServer,
82 publicFiles?: Set<string>,
83): Connect.NextHandleFunction {
84 const dir = server.config.publicDir
85 const serve = sirv(
86 dir,
87 sirvOptions({
88 config: server.config,
89 getHeaders: () => server.config.server.headers,
90 disableFsServeCheck: true,
91 }),
92 )
93
94 const toFilePath = (url: string) => {
95 let filePath = cleanUrl(url)
96 if (filePath.includes('%')) {
97 try {
98 filePath = decodeURI(filePath)
99 } catch {
100 /* malform uri */
101 }
102 }
103 return normalizePath(filePath)
104 }
105
106 // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
107 return function viteServePublicMiddleware(req, res, next) {
108 // To avoid the performance impact of `existsSync` on every request, we check against an
109 // in-memory set of known public files. This set is updated on restarts.
110 // also skip import request and internal requests `/@fs/ /@vite-client` etc...
111 if (
112 (publicFiles && !publicFiles.has(toFilePath(req.url!))) ||
113 isImportRequest(req.url!) ||
114 isInternalRequest(req.url!) ||
115 // for `/public-file.js?url` to be transformed
116 urlRE.test(req.url!)
117 ) {
118 return next()
119 }
120 serve(req, res, next)
121 }
122}
123
124export function serveStaticMiddleware(
125 server: ViteDevServer,

Callers 1

_createServerFunction · 0.90

Calls 7

isImportRequestFunction · 0.90
isInternalRequestFunction · 0.90
sirvOptionsFunction · 0.85
toFilePathFunction · 0.85
nextFunction · 0.85
hasMethod · 0.80
serveFunction · 0.50

Tested by

no test coverage detected