Articles

How to fix Vite’s “Failed to fetch dynamically imported module” after a deploy

Diagnose a missing lazy JavaScript chunk after a Vite deployment, retain the original hashed assets, and check both old and fresh browser tabs.

Updated

If Vite reports “Failed to fetch dynamically imported module” after a deploy, inspect the failed request before changing your cache settings. If an old tab is asking for a hashed JavaScript file that the new release no longer serves, keep the file available at its original URL. Serve HTML with Cache-Control: no-cache so new navigations revalidate it. Revalidating HTML alone does not update a page that is already open.

Download the runnable Vite example

1. Check which request actually failed

Open the browser’s Network panel and click the route that fails. Compare the failed asset with the JavaScript named by the current build. Match the remedy to what the response shows:

What you seeWhat to check
Only tabs opened before the deploy request a missing old hashed fileRetain the previous build’s immutable assets.
Fresh tabs request a missing file tooCheck that HTML and assets were published together, the configured Vite base matches the URL, and the served artifact includes the file.
The chunk returns 200 with text/htmlThe SPA fallback may be answering missing asset paths with index.html. Serve JavaScript with the right type and return a real 404 for an absent asset.
The request never reaches the originCheck the browser’s CSP, CORS errors if the asset is on another origin, and the network. Do not assume a deploy mismatch from the error alone.

Vite documents the old-tab case and its vite:preloadError event. A full-page reload can recover when HTML revalidates, but it can lose unsaved work. If you offer a reload, give users a clear prompt or limit automatic recovery to one attempt. Retention avoids the reload for this failure.

2. Keep the old hashed URLs alive

Publish the new build, but do not remove the previous hashed files at cutover. Copy them into the candidate’s asset directory or keep them in storage at the same public URLs. If a name clashes, confirm the bytes match; never put new code under the old immutable path. Retain assets long enough for the old sessions you expect, then clean them up. This is independent of keeping old API clients compatible.

A useful baseline is:

  • HTML: Cache-Control: no-cache. New navigations revalidate the shell.
  • Content-hashed JavaScript and CSS: Cache-Control: public, max-age=31536000, immutable on files whose content never changes under that URL.
  • Missing asset: return 404, not a successful HTML fallback. Do not cache the missing-file response in the demo.

3. Check an old tab before and after promotion

From the downloaded directory, run npm install and bash build-demo.sh. It builds A, B, and B with A’s hashed assets retained as B-keep. Use an authenticated Gregale CLI and a new disposable app.

  1. Deploy demo-out/A once. Open the stable app URL in two separate browser profiles and wait for Shell A. Don’t click Open pricing yet.
  2. Stage demo-out/B using gregale deploy --path demo-out/B --name "$APP" --source=worktree --no-traffic. Use the exact deployment's response check for a first check, then promote the candidate in this disposable demo. Verify B receives 100% of traffic. Click Open pricing in the first old tab and look for the old asset’s 404.
  3. Stage and promote demo-out/B-keep the same way. Click Open pricing in the untouched A tab. It should load A’s original pricing chunk. A fresh tab should show Shell B and Pricing from B.

A staged HTTP check is useful, but a 200 for the HTML does not prove the browser can render it or navigate to a lazy route. Keep one browser test with a fresh tab and another with a tab from the release you are replacing. On a real site, a gradual rollout gives you time to spot regressions. Session affinity during a split does not guarantee old assets will remain available after full promotion.

Hosted test results

We opened two tabs on build A in Chromium, then advanced a private Gregale Scale app through two staged deployments. The browser harness fetched authenticated responses from the stable app URL. The candidate started at 0% each time, and we checked it was at 100% before clicking.

Browser and releaseWhat happened
Old A tab, B serving without old assetsThe original pricing chunk returned 404. Vite emitted vite:preloadError.
Untouched A tab, B serving with old assets retainedThe same URL returned 200 with the original JavaScript bytes. The tab showed Pricing from A without reloading.
Fresh tab, B serving with old assets retainedThe tab loaded Shell B and showed Pricing from B.

We reproduced the same outcomes with the included local Chromium test. This proves one lazy JavaScript import on the fixture, not anonymous ingress, a production CDN, or every in-flight user session.

Questions

Why does Vite fail to fetch a dynamically imported module after a deploy?

One common cause is that a previously opened tab imports a hashed chunk from the old build after that file has been removed. Check the failed network request before ruling out wrong paths, MIME, CSP or a network problem.

Does Cache-Control: no-cache on index.html fix old tabs?

It makes new navigations revalidate HTML. It does not replace HTML and JavaScript in a tab that is already open. Retain the old hashed assets or provide a bounded recovery flow.

Does session affinity keep old Vite assets after the rollout?

No. Affinity can keep related requests in a rollout cohort, but it does not guarantee the previous release still serves assets after the new version takes all traffic.