504 (Outdated Optimize Dep)

⚡ Fastest fix

Vite's pre-bundled dependency cache went stale mid-session. It's a dev-server cache mismatch, not a real timeout. Reload the page first; if it sticks, wipe the optimize cache and restart:

# stop the dev server, then:
rm -rf node_modules/.vite
npm run dev -- --force        # rebuild the dependency pre-bundle from scratch

What you're seeing

# in the browser console / network tab
GET http://localhost:5173/node_modules/.vite/deps/lodash-es.js?v=8f2a1c3d
    504 (Outdated Optimize Dep)

# in the Vite terminal
[vite] ✨ new dependencies optimized: chart.js
[vite] ✨ optimized dependencies changed. reloading

The ?v= hash in the URL is the tell: the browser asked for one pre-bundle version and Vite has already moved to a new one. That "reloading" line in the terminal is Vite re-optimizing underneath you.

30-second triage

Fix 1 — Reload the page

When: a one-off 504 the first time a new dependency is hit.

# nothing to install — just refresh
# Vite finishes optimizing and serves the new pre-bundle
# (Cmd/Ctrl + R)

The first time Vite encounters a dependency it hasn't pre-bundled, it optimizes it and reloads. A single 504 during that hand-off is expected; one refresh clears it.

Fix 2 — Wipe the optimize cache and force a rebuild

When: the 504 won't go away, or the cache looks corrupted after a crash.

# stop the dev server first
rm -rf node_modules/.vite       # delete the pre-bundle cache
npm run dev -- --force          # or: vite --force

# nuke a stale browser copy too if needed: hard reload / disable cache

node_modules/.vite holds the esbuild pre-bundles. Deleting it plus --force makes Vite re-derive everything cleanly, which fixes a stale or half-written cache.

Fix 3 — Pre-declare deps so Vite stops re-optimizing

When: recurring 504s because Vite keeps discovering deps at runtime.

// vite.config.js
export default {
  optimizeDeps: {
    include: ['lodash-es', 'chart.js', 'date-fns'], // bundle up front
    // exclude: ['some-pkg-using-workers'],          // skip when needed
  },
};

If deps are only discovered when a route lazy-loads them, Vite re-optimizes mid-session and invalidates chunks — the classic repeating 504. Listing heavy or dynamically imported packages in optimizeDeps.include makes Vite pre-bundle them at startup, so the cache stays stable.

Fix 4 — Restart after any dependency change

When: it began right after npm install, an update, or npm link.

# after changing dependencies, restart the dev server
# (Ctrl-C, then)
npm run dev

Installing or updating a package changes the lockfile, so Vite re-runs optimization and old pre-bundle URLs go stale. A restart re-derives the cache; you rarely need --force for this case.

Why this happens

For fast dev startup, Vite pre-bundles your node_modules dependencies with esbuild into node_modules/.vite/deps and serves them with a version query (?v=hash). If Vite later decides the pre-bundle is out of date — it discovered a new dependency to include, the lockfile changed, or the config changed — it re-optimizes and assigns a new hash. Any browser request still pointing at the old hash can no longer be served, so Vite responds 504 (Outdated Optimize Dep) to tell the client "this pre-bundle is stale, reload." It reuses the HTTP 504 code but has nothing to do with a gateway timeout. Because it's purely the dev-server dependency cache, the fixes are all about clearing or stabilizing that cache — and it never affects vite build.

Quick reference

PatternCauseFix
One-off at startupfirst-time dep optimizationreload (Fix 1)
Persistentstale/corrupted cacheclear .vite + --force (Fix 2)
Recurring on navigationruntime-discovered depsoptimizeDeps.include (Fix 3)
After a dep changelockfile changedrestart dev server (Fix 4)
In productionN/Acan't happen — Rollup build

✓ Confirm it's fixed

  • Reload several times and navigate between routes — no 504 in the network tab.
  • The Vite terminal stops printing "optimized dependencies changed. reloading" on every visit.
  • vite build && vite preview works (confirms it was only a dev-cache issue).

Frequently Asked Questions

What does '504 (Outdated Optimize Dep)' mean in Vite?

Vite pre-bundles your dependencies into node_modules/.vite for speed. The 504 means the browser requested a pre-bundled dependency chunk that Vite has since invalidated — the optimize cache became outdated mid-session, so the old URL no longer resolves. It is a dev-server caching mismatch, not a real gateway timeout.

How do I fix Vite 504 Outdated Optimize Dep?

First just reload the page — Vite usually re-optimizes and recovers. If it persists, stop the dev server, delete node_modules/.vite (the optimize cache), and restart. Running vite --force also rebuilds the dependency cache from scratch, which clears a corrupted or stale pre-bundle.

Why does it keep happening on every reload?

A repeating 504 usually means Vite keeps discovering new dependencies to pre-bundle at runtime (so it re-optimizes and invalidates chunks), or a dependency is being added dynamically. List the heavy or dynamically imported packages in optimizeDeps.include so Vite bundles them up front and stops re-optimizing mid-session.

What is optimizeDeps in Vite?

optimizeDeps controls Vite's dependency pre-bundling. optimizeDeps.include forces packages to be pre-bundled up front (fixes repeated re-optimization); optimizeDeps.exclude skips packages that shouldn't be pre-bundled (e.g. ones using import.meta or workers). Tuning these stabilizes the cache so 504s stop.

Did it start after adding or updating a package?

Yes, that is a common trigger. Installing, updating, or linking a dependency changes the lockfile, so Vite re-runs optimization and old pre-bundle URLs go stale. Restart the dev server after any dependency change; a plain restart re-derives the cache and clears the 504.

Does the 504 affect my production build?

No. Dependency pre-bundling with esbuild is a dev-server-only optimization. vite build uses Rollup and does not use the .vite optimize cache, so "Outdated Optimize Dep" cannot occur in production — it is strictly a local dev issue.

More build & tooling errors

Browse the full reference for Vite, Node.js, and bundler errors — exact message, cause, and fix.

All Error References Can't resolve 'fs' process.env is undefined
About the author

Pasindu Ishan is a software developer based in Sri Lanka. He builds privacy-first developer tools at JSON Dev Tools.