From 7be6c0c90a6224d3525af9c44b29ba48ef63ebf2 Mon Sep 17 00:00:00 2001 From: Yunfei He Date: Thu, 26 Aug 2021 03:11:41 +0800 Subject: [PATCH] fix: unexpected file request with custom publicDir, fix #4629 (#4631) --- packages/vite/src/node/server/middlewares/transform.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index a1ae24b2c32327..204c9c8c360c4a 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -114,13 +114,17 @@ export function transformMiddleware( } } - // warn explicit /public/ paths - if (url.startsWith('/public/')) { + const publicPath = + normalizePath(server.config.publicDir).slice( + server.config.root.length + ) + '/' + // warn explicit public paths + if (url.startsWith(publicPath)) { logger.warn( chalk.yellow( `files in the public directory are served at the root path.\n` + `Instead of ${chalk.cyan(url)}, use ${chalk.cyan( - url.replace(/^\/public\//, '/') + url.replace(publicPath, '/') )}.` ) )