Error "504 (Outdated Optimize Dep)" when force re-optimization on the server start by using vite --force #14284
Closed
7 tasks done
Labels
feat: deps optimizer
Esbuild Dependencies Optimization
p4-important
Violate documented behavior or significantly improves performance (priority)
Describe the bug
中文说明
On my project built with vue-element-admin , this error sometimes occurs when using the
vite --force
to start the project, so the page displays a white screen. However, the error disappears when restart the project again. Although this way can solve the problem, it is quite annoying to have this error from time to time.The reason for this problem is that the id will be changed through tryOptimizedResolve in vite:resolve , but the optimizer does not actually pre-build the corresponding file because there is no outdatedResult .
The timing of this problem is random ( maybe related to the complexity of the project files ) , and it is difficult to reproduce it stably. However, after repeated trials, I finally found a specific situation that can definitely cause this problem:
Any url except
/
and/favicon.ico
which can trigger additional transformRequest in transformMiddleware , .e.g/xxx
. This is very important!vite/packages/vite/src/node/server/middlewares/transform.ts
Line 44 in 898fee7
vite/packages/vite/src/node/server/middlewares/transform.ts
Lines 56 to 58 in 898fee7
vite/packages/vite/src/node/server/middlewares/transform.ts
Lines 203 to 205 in 898fee7
Import statements dynamically added by plugins as what unplugin-auto-import does, they cannot be optimized by crawl of static imports and will trigger addMissingDep during transform .
vite/packages/vite/src/node/optimizer/optimizer.ts
Line 541 in 898fee7
The slow transformIndexHtml which is used to increase the time interval between the first request
/xxx
and preTransformRequest .vite/packages/vite/src/node/server/middlewares/indexHtml.ts
Lines 348 to 365 in 898fee7
Pre-build a large scss file ( .e.g element-ui ) which may cost a few seconds to make the optimize step slower.
Sorry for my poor Chinglish, but I think you can understand what I mean after running the demo below.
Reproduction
https://stackblitz.com/edit/vitejs-vite-imqoo8?file=vite.config.js
Steps to reproduce
In order to better observe the problem, I added a patch file created with patch-package to show more logs.
Open the browser and visit the page, you can see nothing and will find
504 (Outdated Optimize Dep)
error in devtools.Then you can see the following log in terminal:
According to the log we can find two things:
The
delayDepsOptimizerUntil('main.js')
was called 177ms after thedelayDepsOptimizerUntil('/not_root')
be called.This time difference exceeds callCrawlEndIfIdleAfterMs so the onCrawlEnd was called earlier than expected.
The length of
metadata.discovered
was changed from 1 to 2.When this line of code was executed, the actual length of
crawlDeps
is 1, which is the same asscanDeps
, so it goes to else (using post-scan optimizer result...
) .vite/packages/vite/src/node/optimizer/optimizer.ts
Lines 622 to 635 in 898fee7
Then we can change the content of the vite.config.js file:
vite will restart automatically, and then we will look at the logs afterwards:
The length of
metadata.discovered
was no longer changed, and the re-running optimizer step was executed as expected.Open the browser and visit the page, you can see
Hello,World!
and no error in devtools.Another strange thing here is that the
delayDepsOptimizerUntil('main.js')
was called 823ms after the previous, which looks like pre-build of scss was run before.System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: