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

Error "504 (Outdated Optimize Dep)" when force re-optimization on the server start by using vite --force #14284

Closed
7 tasks done
anyesu opened this issue Sep 4, 2023 · 1 comment
Labels
feat: deps optimizer Esbuild Dependencies Optimization p4-important Violate documented behavior or significantly improves performance (priority)

Comments

@anyesu
Copy link
Contributor

anyesu commented Sep 4, 2023

Describe the bug

中文说明

GET http://localhost:5173/node_modules/.vite/deps/element-ui_lib_button.js?v=bc3e4ba5 504 (Outdated Optimize Dep)

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:

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.

vite-bug

pnpm i
pnpm run dev

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:

  Forced re-optimization of dependencies
    vite:deps scanning for dependencies... +0ms

    VITE v4.4.9  ready in 986 ms

    ➜  Local:   http://localhost:5173/
    ➜  Network: use --host to expose
    ➜  press h to show help
+ call delayDepsOptimizerUntil('/not_root') 0ms after the last
+ call markIdAsDone('/not_root') 0ms after the last
    vite:deps Crawling dependencies using entries:
    vite:deps   /home/projects/vitejs-vite-imqoo8/index.html +0ms
    vite:deps ✨ static imports crawl ended +871ms
+ metadata.discovered ( size: 1 ) : element-ui/packages/theme-chalk/src/index.scss
    vite:deps Scan completed in 927.22ms: no dependencies found +95ms
+ metadata.discovered ( size: 1 ) : element-ui/packages/theme-chalk/src/index.scss
+ call delayDepsOptimizerUntil('main.js') 177ms after the last
+ call delayDepsOptimizerUntil('node_modules/.vite/deps/element-ui_lib_button.js?v=1d7c7005') 10ms after the last
    vite:deps Dependencies bundled in 1216.69ms +0ms
+ metadata.discovered ( size: 2 ) : element-ui/packages/theme-chalk/src/index.scss, element-ui/lib/button
    vite:deps ✨ using post-scan optimizer result, the scanner found every used dependency +1s
    vite:deps ✨ dependencies optimized +1ms
+ call delayDepsOptimizerUntil('node_modules/.pnpm/vite@4.4.9_sass@1.66.1/node_modules/vite/dist/client/client.mjs') 1175ms after the last
+ call delayDepsOptimizerUntil('node_modules/.pnpm/vite@4.4.9_sass@1.66.1/node_modules/vite/dist/client/env.mjs') 3ms after the last
+ call delayDepsOptimizerUntil('node_modules/.vite/deps/element-ui_lib_button.js?v=1d7c7005') 405ms after the last

If you run the project multiple times, you will find that the order of the logs may be different.

According to the log we can find two things:

  • The delayDepsOptimizerUntil('main.js') was called 177ms after the delayDepsOptimizerUntil('/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 as scanDeps , so it goes to else ( using post-scan optimizer result... ) .

    const crawlDeps = Object.keys(metadata.discovered)
    // Await for the scan+optimize step running in the background
    // It normally should be over by the time crawling of user code ended
    await depsOptimizer.scanProcessing
    if (!isBuild && optimizationResult && !config.optimizeDeps.noDiscovery) {
    const result = await optimizationResult.result
    optimizationResult = undefined
    currentlyProcessing = false
    const scanDeps = Object.keys(result.metadata.optimized)
    if (scanDeps.length === 0 && crawlDeps.length === 0) {

Then we can change the content of the vite.config.js file:

- process.env.NO_SLOW || slowTransformIndexHtml(),
+ // process.env.NO_SLOW || slowTransformIndexHtml(),

vite will restart automatically, and then we will look at the logs afterwards:

  [vite] vite.config.js changed, restarting server...
  Forced re-optimization of dependencies
    vite:deps scanning for dependencies... +25m
  [vite] server restarted.
+ call delayDepsOptimizerUntil('/not_root') 0ms after the last
+ call markIdAsDone('/not_root') 0ms after the last
    vite:deps Crawling dependencies using entries:
    vite:deps   /home/projects/vitejs-vite-imqoo8/index.html +25m
+ call delayDepsOptimizerUntil('main.js') 823ms after the last
    vite:deps Scan completed in 928.80ms: no dependencies found +99ms
+ call delayDepsOptimizerUntil('node_modules/.vite/deps/element-ui_lib_button.js?v=e80a6e76') 100ms after the last
+ call markIdAsDone('/home/projects/vitejs-vite-imqoo8/main.js') 922ms after the last
    vite:deps ✨ static imports crawl ended +988ms
+ metadata.discovered ( size: 2 ) : element-ui/packages/theme-chalk/src/index.scss, element-ui/lib/button
+ metadata.discovered ( size: 2 ) : element-ui/packages/theme-chalk/src/index.scss, element-ui/lib/button
    vite:deps Dependencies bundled in 1137.07ms +25m
+ metadata.discovered ( size: 2 ) : element-ui/packages/theme-chalk/src/index.scss, element-ui/lib/button
    vite:deps ✨ new dependencies were found while crawling that weren't detected by the scanner +1s
    vite:deps ✨ re-running optimizer +0ms
    vite:deps new dependencies found: element-ui/packages/theme-chalk/src/index.scss, element-ui/lib/button +6ms
    vite:deps Dependencies bundled in 1032.49ms +1s
    vite:deps ✨ dependencies optimized +1s
+ call delayDepsOptimizerUntil('node_modules/.vite/deps/chunk-76J2PTFD.js?v=b0e72c20') 2196ms after the last
+ call delayDepsOptimizerUntil('node_modules/.pnpm/vite@4.4.9_sass@1.66.1/node_modules/vite/dist/client/client.mjs') 4394ms after the last
+ call delayDepsOptimizerUntil('node_modules/.pnpm/vite@4.4.9_sass@1.66.1/node_modules/vite/dist/client/env.mjs') 2ms after the last

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

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11700 @ 2.50GHz
    Memory: 26.00 GB / 47.81 GB
  Binaries:
    Node: 16.20.2 - ~\AppData\Local\pnpm\node.EXE
    Yarn: 1.22.18 - D:\software\nodejs\node_global\yarn.CMD
    npm: 8.19.4 - ~\AppData\Local\pnpm\npm.CMD
    pnpm: 8.6.12 - ~\AppData\Local\pnpm\pnpm.EXE
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (116.0.1938.69)
    Internet Explorer: 11.0.19041.1566

Used Package Manager

pnpm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Sep 4, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

anyesu added a commit to anyesu/vite that referenced this issue Sep 4, 2023
@sapphi-red sapphi-red added p4-important Violate documented behavior or significantly improves performance (priority) feat: deps optimizer Esbuild Dependencies Optimization labels Sep 6, 2023
Gaubee pushed a commit to Gaubee/vite that referenced this issue Sep 13, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Oct 1, 2023
bluwy pushed a commit that referenced this issue Oct 3, 2023
bluwy pushed a commit that referenced this issue Oct 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: deps optimizer Esbuild Dependencies Optimization p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
Development

No branches or pull requests

2 participants