Skip to content

Commit

Permalink
fix: fix out of root static file serving on windows
Browse files Browse the repository at this point in the history
fix #1982
  • Loading branch information
yyx990803 committed Feb 11, 2021
1 parent 09b13ed commit 4d34a73
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,19 @@ export function serveStaticMiddleware(
}

export function serveRawFsMiddleware(): Connect.NextHandleFunction {
const fsRoot =
os.platform() == 'win32' ? process.cwd().split(path.sep)[0] + '/' : '/'
const serveFromRoot = sirv(fsRoot, sirvOptions)
const isWin = os.platform() === 'win32'
const serveFromRoot = sirv('/', sirvOptions)

return (req, res, next) => {
const url = req.url!
let url = req.url!
// In some cases (e.g. linked monorepos) files outside of root will
// reference assets that are also out of served root. In such cases
// the paths are rewritten to `/@fs/` prefixed paths and must be served by
// searching based from fs root.
if (url.startsWith(FS_PREFIX)) {
req.url = decodeURI(url.slice(FS_PREFIX.length))
url = url.slice(FS_PREFIX.length)
if (isWin) url = url.replace(/^[A-Z]:/i, '')
req.url = decodeURI(url)
serveFromRoot(req, res, next)
} else {
next()
Expand Down

0 comments on commit 4d34a73

Please sign in to comment.