( server: ViteDevServer, publicFiles?: Set<string>, )
| 78 | } |
| 79 | |
| 80 | export 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 | |
| 124 | export function serveStaticMiddleware( |
| 125 | server: ViteDevServer, |
no test coverage detected