Skip to content

fix(optimizer): serve optimized deps on hash mismatch instead of 504 (fix #22303) - #23322

Closed
santhiprakash wants to merge 1 commit into
vitejs:mainfrom
santhiprakash:fix/optimizer-outdated-504-lazy-route-22303
Closed

fix(optimizer): serve optimized deps on hash mismatch instead of 504 (fix #22303)#23322
santhiprakash wants to merge 1 commit into
vitejs:mainfrom
santhiprakash:fix/optimizer-outdated-504-lazy-route-22303

Conversation

@santhiprakash

Copy link
Copy Markdown
Contributor

Summary

Fixes vitejs/vite#22303. Lazy-route navigation that reaches an optimized-dep subpath not pre-bundled at server start no longer fails with 504 Outdated Optimize Dep for the in-flight dynamic import.

Root cause

When a lazy module is transformed before its newly-discovered dep is fully optimized, the wrapper embeds a per-subpath ?v= based on the discovered browserHash. As soon as the optimizer commits, that hash is replaced with the optimized browserHash (a different formula). The browser's dynamic-import request — already in flight with the wrapper's hash — hit the throwOutdatedRequest path in optimizedDepsPlugin.load and surfaced as Uncaught TypeError: Failed to fetch dynamically imported module.

There were two checks firing in this scenario:

  1. info.browserHash !== browserHash before await info.processing
  2. info.browserHash !== currentInfo?.browserHash after await info.processing

Both are wrong for an in-flight dynamic import: the browser asked for the dep, give it the dep. The optimizer still triggers a full reload when its output actually changes, so the subsequent reload re-issues the request with the new hash.

Change

packages/vite/src/node/plugins/optimizedDeps.ts: remove the two throwOutdatedRequest checks in the load hook. Keep the catch-branch throw for the genuine file-missing case (the file genuinely didn't get written because the optimizer run was cancelled).

optimizeDeps.ignoreOutdatedRequests is now redundant for this scenario — the new behavior matches what users previously had to opt into. It still affects callers that rely on the throw elsewhere, which we did not change.

Repro

https://github.com/patricklafrance/vite-rac-tsr-504-repro — 5/5 headless-Chrome 504s on a fresh clone before this patch. Verified that lazy /counter navigation completes without Failed to fetch dynamically imported module after the patch (re-tested against the upstream optimizer flow).

Test plan

  • Verified by hand against the upstream repro pattern (lazy route + dep excluded via crawlFrameworkPkgs).
  • No new unit tests; the load hook is not currently covered by the packages/vite/src/node/__tests__ suite, and the e2e harness would need a new fixture that excludes a subpath through a workspace that the scanner can't crawl. Happy to add a fixture under playground/optimize-deps/ if maintainers prefer one.

The 504 throw in optimizedDepsPlugin.load fired whenever the
browserHash embedded in a lazy-route wrapper no longer matched
the current dep browserHash. For dynamic imports the browser had
already initiated the request with the wrapper's hash, so the
504 surfaced as 'Failed to fetch dynamically imported module'
(issue vitejs#22303).

The optimizer still triggers a full reload when its output
actually changes, so serving the current chunk is safe: the
subsequent reload re-issues the request with the new hash. We
keep the catch-branch throw for the case where the file is
genuinely missing on disk.
@santhiprakash
santhiprakash force-pushed the fix/optimizer-outdated-504-lazy-route-22303 branch from 63c4e85 to 78eb59a Compare August 21, 2026 10:32
@bluwy bluwy added the bot: likely Likely a bot, LLM, or agent. Automatically comments and closes the issue or PR label Aug 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The PR has been manually labeled as likely to be created by a bot, LLM, or agent, and will be automatically closed. This may be because the PR contains LLM-generated descriptions, does not follow the PR template, is overly verbose, or does not contain any fruitful interaction, which harms the review process. Please read our AI policy for more information.

If you believe this is a mistake, please reply to this comment and we will review it.

@github-actions github-actions Bot closed this Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot: likely Likely a bot, LLM, or agent. Automatically comments and closes the issue or PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] 504 Outdated Optimize Dep on lazy-route navigation — dep optimizer bumps per-subpath ?v= after transform is served

2 participants