fix(server): avoid chokidar throttling on startup #15347
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Vite dev server producing unexpected hmr updates on startup without changes. Chokidar is throttling directory reads on initial watch, producing add events, and vite responds with hmr updates to the add events. Deduping the array passed to
chokidar.watch
on server creation fixes the issue.Additional context
We noticed during the startup process of the vite dev server with our project that it would regularly and seemingly randomly produce hmr update logs, even though no changes were being made during the startup process. What was discovered was that
chokidar
was throttling directory reads during the initial watch on vite server creation. Throttled events fromchokidar
result inadd
events emitted even withignoreInitial
set totrue
. Vite listens for theseadd
events and triggers an hmr update for the associated file.The throttling is occurring, because the files/directories passed to chokidar.watch could and in our case did contain duplicate directories.
chokidar
does not appear to dedupe the array passed to it, sochokidar
ended up duplicatively reading the same directories via _handleRead. It appears the intention of the throttling within _handleRead is to catch changes that may have occurred during the initial watch process. So,chokidar
treated the duplicative reads as throttled changes (i.e.initialAdd
asfalse
and emits anadd
event), which informed vite, and vite responded with an hmr update.The default configuration of vite appears to encounter this, which is where we hit it. As our root option was
process.cwd()
and envDir was defaulting toroot
. So,chokidar.watch
receives theroot
directory twice via root and config.envDir.This PR uses a
Set
to remove any duplicate strings from the array of files/directories passed tochokidar.watch
to avoid this problem (i.e. remove the duplicate config.envDir entry).The workaround for this ahead of this change is to not have
root
andconfig.envDir
share the same value (i.e. setconfig.envDir
to a different directory).What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).