{"slug": "how-to-fix-vites-failed-to-fetch-dynamically-imported-module-after-a-deploy", "title": "How to fix Vite’s “Failed to fetch dynamically imported module” after a deploy", "summary": "A developer writing on gregale.dev documented a reproducible fix for Vite's \"Failed to fetch dynamically imported module\" error after a deploy, showing that old browser tabs requesting hashed JavaScript files removed by a new release receive 404s and trigger Vite's vite:preloadError event. The recommended remedy is to retain the previous build's immutable hashed assets at their original URLs, serve HTML with Cache-Control: no-cache so new navigations revalidate, and reserve long-lived immutable caching for content-addressed files. The author reproduced the failure in Chromium across two staged Gregale deployments, confirming that revalidating HTML alone does not update an already-open page.", "body_md": "*Originally published on [gregale.dev](https://gregale.dev/articles/vite-failed-to-fetch-dynamically-imported-module-after-deploy). I build Gregale, so read this as a vendor's page. This post was written with AI assistance.*\n\nIf 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.\n\n[Download the runnable Vite example](https://gregale.dev/examples/vite-old-tab-deployment.zip)\n\nOpen 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:\n\n| What you see | What to check | \n|---|---|\n| Only tabs opened before the deploy request a missing old hashed file | Retain the previous build’s immutable assets. | \n| Fresh tabs request a missing file too | Check that HTML and assets were published together, the configured Vite `base` matches the URL, and the served artifact includes the file. | \n| The chunk returns 200 with `text/html` | The 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. | \n| The request never reaches the origin | Check 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. | \n\nVite documents the old-tab case and its [`vite:preloadError` event](https://vite.dev/guide/build#load-error-handling). 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.\n\nPublish 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.\n\nA useful baseline is:\n\n`Cache-Control: no-cache`. New navigations revalidate the shell.` Cache-Control: public, max-age=31536000, immutable` on files whose content never changes under that URL.\nFrom 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](https://gregale.dev/docs/install) and a new disposable app.\n\n`demo-out/A` once. Open the stable app URL in two separate browser profiles and wait for `demo-out/B` using `gregale deploy --path demo-out/B --name \"$APP\" --source=worktree --no-traffic`. Use `demo-out/B-keep` the same way. Click 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](https://gregale.dev/docs/rollouts) gives you time to spot regressions. Session affinity during a split does not guarantee old assets will remain available after full promotion.\n\nWe 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.\n\n| Browser and release | What happened | \n|---|---|\n| Old A tab, B serving without old assets | The original pricing chunk returned 404. Vite emitted `vite:preloadError` . | \n| Untouched A tab, B serving with old assets retained | The same URL returned 200 with the original JavaScript bytes. The tab showed Pricing from A without reloading. | \n| Fresh tab, B serving with old assets retained | The tab loaded Shell B and showed Pricing from B. | \n\nWe 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.\n\n**Why does Vite fail to fetch a dynamically imported module after a deploy?**\n\nOne 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.\n\n**Does Cache-Control: no-cache on index.html fix old tabs?**\n\nIt 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.\n\n**Does session affinity keep old Vite assets after the rollout?**\n\nNo. 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.", "url": "https://wpnews.pro/news/how-to-fix-vites-failed-to-fetch-dynamically-imported-module-after-a-deploy", "canonical_source": "https://dev.to/hpoyrazk/how-to-fix-vites-failed-to-fetch-dynamically-imported-module-after-a-deploy-5hbb", "published_at": "2026-12-04 13:00:00+00:00", "updated_at": "2026-12-04 13:14:03.953276+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Vite", "Gregale", "Chromium"], "also_reported_by": [], "takeaway": "Retaining the previous build's hashed assets at their original URLs prevents old tabs from hitting 404s on lazy chunks, while Cache-Control: no-cache on HTML lets new navigations revalidate the shell.", "recommendation": {"advice": "Keep the prior build's immutable hashed assets at their original URLs through cutover and serve HTML with Cache-Control: no-cache, since revalidating HTML alone won't update already-open tabs.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/how-to-fix-vites-failed-to-fetch-dynamically-imported-module-after-a-deploy", "markdown": "https://wpnews.pro/news/how-to-fix-vites-failed-to-fetch-dynamically-imported-module-after-a-deploy.md", "text": "https://wpnews.pro/news/how-to-fix-vites-failed-to-fetch-dynamically-imported-module-after-a-deploy.txt", "jsonld": "https://wpnews.pro/news/how-to-fix-vites-failed-to-fetch-dynamically-imported-module-after-a-deploy.jsonld"}}