-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
perf: avoid computing paths on each request #15318
Conversation
Run & review this pull request in StackBlitz Codeflow. |
|
||
// check if public dir is inside root dir | ||
const publicDir = normalizePath(server.config.publicDir) | ||
const rootDir = normalizePath(server.config.root) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vite/packages/vite/src/node/config.ts
Lines 490 to 492 in ea6a7a6
const resolvedRoot = normalizePath( | |
config.root ? path.resolve(config.root) : process.cwd(), | |
) |
vite/packages/vite/src/node/config.ts
Line 738 in ea6a7a6
root: resolvedRoot, |
config.root
is already normalized, so we don't need to normalize it here.I think we can normalize the publicDir when we resolve the config as well.
vite/packages/vite/src/node/config.ts
Lines 650 to 656 in ea6a7a6
const resolvedPublicDir = | |
publicDir !== false && publicDir !== '' | |
? path.resolve( | |
resolvedRoot, | |
typeof publicDir === 'string' ? publicDir : 'public', | |
) | |
: '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done with config.root
. About publicDir
, we got hit by it not being normalized here #15317, so I think it is a good idea. But I was thinking we should do this in the next minor or major because some plugins may expect it to not be normalized. Hopefully there aren't many instances like that but better to play safe.
I think that all the directory paths in the resolved config should be normalized and without a trailing slash: root
, publicDir
, outDir
. We should check what we are doing with base
. There are a lot of places were we are doing a path.posix.join
or similar when we could be directly concatenating the strings if we trust the resolved config.
Description
We should optimize responding from the server as much as possible. For large projects, the time it takes the server to serve already transformed requests will greatly affect reloading time.
This PR avoids normalize paths and path checks that were done on each request. It is safe to cache this in the middleware constructor after #15166
I also moved the warning to another function because it isn't important for the main logic of the handler.
What is the purpose of this pull request?