TypeError: Failed to fetch dynamically imported module, after a Vite deploy

By AI App Medic · Updated · Vite · Lovable · Bolt · Deploy

The app has been in production and working. You publish a new build from Lovable, Bolt, Cursor or a Vite pipeline. A user who already had the tab open clicks a route, or you do, and the console shows:

TypeError: Failed to fetch dynamically imported module: https://your-app.com/assets/Dashboard-Ab12Cd.js

A hard refresh often brings the page back. A few days later, after the next deploy, the same users report it again. This is almost always version skew in a Vite SPA, not a typo in the import path.

Why a new deploy breaks open tabs

Vite (and the React Router or React.lazy setup most of these apps use) splits routes into chunks whose names include a content hash: Dashboard-Ab12Cd.js. index.html points at the current hashes. When you deploy, the host serves a new HTML file and a new set of hashed files. Many hosts also delete the previous build, so the old Dashboard-Ab12Cd.js is gone.

Anyone still running the previous HTML, or anyone whose browser cached that HTML, then lazy-loads a route and requests a file that no longer exists. The dynamic import fails, the route never renders, and in some cases the screen goes blank. Vite documents this under load error handling and emits a vite:preloadError event when it happens.

Step 1: prove it is a missing chunk

  1. Open DevTools, go to Network, leave the tab on the live site, and trigger the navigation that breaks.
  2. Look for a JavaScript request under /assets/ (or your configured assets directory) that returns 404 or failed. The file name will contain a hash.
  3. View source on the page and compare the script tags in the HTML with the files your current deploy actually contains. If the HTML mentions a hash that is not on the server, you have version skew.

If the failed URL is a Supabase function, an OpenAI call or some other API, stop here and debug that request instead. If every asset 404s on a brand-new private window, the problem is the Vite base or the host's publish directory, which is covered in blank page after deploy, not this page.

Step 2: reload once when Vite reports a preload error

Add this in the entry file (src/main.tsx or src/main.jsx), before you mount the app, so it is registered before any lazy route loads:

window.addEventListener("vite:preloadError", (event) => {
  event.preventDefault();
  const key = "vite-preload-reload-at";
  const last = Number(sessionStorage.getItem(key) || "0");
  if (Date.now() - last < 10_000) return;
  sessionStorage.setItem(key, String(Date.now()));
  window.location.reload();
});

event.preventDefault() stops Vite from rethrowing the error. The reload fetches a fresh index.html, which points at the new chunks. The sessionStorage guard is required: without it, a truly missing file (wrong base, a blocked path) would reload in a loop. Ten seconds is enough to let one recovery through and to refuse a second.

If you prefer, you can show a small "A new version is available" banner and reload on a button click instead of doing it immediately. The event still has to be handled; ignoring it leaves the failed route dead until the user refreshes by hand.

Step 3: do not let the browser cache index.html

Vite's own guidance is to send Cache-Control: no-cache on the HTML file. Hashed assets can be cached for a long time because their names change when their contents change. If index.html is also cached, users keep requesting chunks you have already deleted, and the handler in step 2 will fire on every deploy.

On Netlify, add a public/_headers file (Vite copies public/ into dist/):

/*
  Cache-Control: public, max-age=0, must-revalidate

/assets/*
  Cache-Control: public, max-age=31536000, immutable

On Vercel, the same idea in vercel.json:

{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        { "key": "Cache-Control", "value": "public, max-age=0, must-revalidate" }
      ]
    },
    {
      "source": "/assets/(.*)",
      "headers": [
        { "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }
      ]
    }
  ]
}

The catch-all HTML rule is listed first, then the longer cache on /assets/ overrides it for hashed files. If you use a service worker or Workbox, make sure it does not cache index.html as a long-lived document; that is a frequent way this bug survives after you set the headers.

On Lovable's own host you may not get a headers file. The listener in step 2 is still the right fix, because it recovers the tab even when the previous HTML was already in memory. If you later move the app to Netlify, Vercel or Cloudflare, add the headers there. The migration steps are in migrate a Lovable app off Lovable hosting.

Other causes that look the same

Prevention

Keep the preload handler in the entry file of every Vite app you ship. Send no-cache (or max-age=0, must-revalidate) on HTML, and a long immutable cache on hashed assets. If your host can retain the previous build's /assets files for a day or two, that gives open tabs a grace period, but you should not rely on it alone. After each deploy, leave an old tab open, click a lazy route, and confirm it recovers without a manual refresh.

FAQ

Why does a hard refresh fix it, and why does it come back after the next deploy?

A hard refresh downloads the new index.html, which points at the new hashed files. The next deploy deletes those files and replaces them again, so any tab that still has the previous HTML hits the same 404. The preload handler and a no-cache policy on HTML are what make it stay fixed.

Is this the same as Failed to fetch on a Supabase function?

No. This TypeError names a JavaScript module URL from your own app (usually /assets/…). A Failed to fetch on /functions/v1/… is a network or CORS problem with the API, covered in the Edge Function CORS guide.

Will this fire on the user's very first visit?

Not for version skew: there is no old HTML yet. A first-visit failure is more often a wrong Vite base, a host that is not serving the assets directory, or an extension blocking the file. Check the status code and the path before you add a reload.

Related guides