Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unexpected file request with custom publicDir, fix #4629 #4631

Merged
merged 1 commit into from Aug 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/vite/src/node/server/middlewares/transform.ts
Expand Up @@ -125,13 +125,17 @@ export function transformMiddleware(
}
}

// warn explicit /public/ paths
if (url.startsWith('/public/')) {
const publicPath =
normalizePath(server.config.publicDir).slice(
server.config.root.length
) + '/'
Comment on lines +128 to +131
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const publicDir = '/foo/public'
const root = '/foo/bar/root'

publicDir.slice(root.length) + '/'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@meteorlxy I guess if root.length is bigger than publicDir.length, we shouldn't throw a warning and just skip it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@meteorlxy Would you like to give a fix? Or I will fix it after home.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should not be simply determined by the length. It's better to check if publicDir is inside root or not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just submit a PR to fix that #4927

// 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, '/')
)}.`
)
)
Expand Down