Skip to content

Commit 9e06b81

Browse files
authored
fix: avoid wrong warning of explicit public paths (#4927)
1 parent e687d98 commit 9e06b81

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

packages/vite/src/node/server/middlewares/transform.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,22 @@ export function transformMiddleware(
114114
}
115115
}
116116

117-
const publicPath =
118-
normalizePath(server.config.publicDir).slice(
119-
server.config.root.length
120-
) + '/'
121-
// warn explicit public paths
122-
if (url.startsWith(publicPath)) {
123-
logger.warn(
124-
chalk.yellow(
125-
`files in the public directory are served at the root path.\n` +
126-
`Instead of ${chalk.cyan(url)}, use ${chalk.cyan(
127-
url.replace(publicPath, '/')
128-
)}.`
117+
// check if public dir is inside root dir
118+
const publicDir = normalizePath(server.config.publicDir)
119+
const rootDir = normalizePath(server.config.root)
120+
if (publicDir.startsWith(rootDir)) {
121+
const publicPath = `${publicDir.slice(rootDir.length)}/`
122+
// warn explicit public paths
123+
if (url.startsWith(publicPath)) {
124+
logger.warn(
125+
chalk.yellow(
126+
`files in the public directory are served at the root path.\n` +
127+
`Instead of ${chalk.cyan(url)}, use ${chalk.cyan(
128+
url.replace(publicPath, '/')
129+
)}.`
130+
)
129131
)
130-
)
132+
}
131133
}
132134

133135
if (

0 commit comments

Comments
 (0)