UnbreakMyApp / Fix guides

White screen after a new deploy: "Failed to fetch dynamically imported module"

The site worked, you published an update, and people who already had it open get a white screen. Vite (Lovable, Bolt) is asking for a JavaScript file the new deploy deleted.

Building a new app instead? Try Bolt (20% off with our link) (affiliate link).

Why it happens

Vite splits routes into their own files and puts a content hash in the name, such as Settings-Cab1Twx9.js. The index.html a visitor already loaded points at those exact names. Your next deploy deletes the previous files. The next click calls import() for a URL that is gone, and the browser throws TypeError: Failed to fetch dynamically imported module.

Vite documents this case: the assets running on the device are outdated, and the code tries to import the old chunk, which was deleted. The same screen appears when a single-page rewrite returns index.html (200, content-type text/html) for that missing file, because the browser expected JavaScript.

A fresh private window that still throws is a different bug: a wrong Vite base path, or a host that never uploaded the assets folder. An offline user or a blocked request fails the same way, so a reload loop is worse than the error.

How to fix it, step by step

  1. In DevTools → Network, find the red .js request. A 404, or a 200 whose response is HTML, after a deploy you just shipped, is a stale chunk. A brand-new tab with the same failure means the new build's files are not actually being served.
  2. In the app entry (main.tsx or main.jsx), before the app renders, listen for vite:preloadError. Call event.preventDefault() so Vite does not rethrow, then reload the page once. Vite's docs describe this event and say to set Cache-Control: no-cache on the HTML file, otherwise browsers keep referencing the old assets.
  3. Guard the reload with sessionStorage and a short window (about 10 seconds). If it already reloaded, let the error surface. An unconditional reload loops when the user is offline or the file is really missing.
  4. On the host, do not cache index.html for a year. Hashed files under /assets/ can be cached for a long time; the HTML cannot. If the HTML is on Vercel or Netlify, set that header for the document only.
  5. Keep the single-page rewrite for real pages such as /dashboard. Vercel checks the filesystem before applying rewrites, so a missing file under /assets/ can fall through and come back as index.html. A missing script should 404, not return your homepage.
  6. For a Next.js app on Vercel, the matching idea is Skew Protection: it is on Pro and Enterprise, and supported frameworks (including Next.js) pin asset requests to the deployment that served the page. It does not cover a full browser refresh, and it is not automatic for a plain Vite app. The reload handler is still the fix for Lovable and Bolt.
  7. Deploy, leave an old tab open, then publish again and click a route you have not opened yet. It should reload once and land on the new version.

Paste-ready prompt for Lovable

After a new deploy, users who already have the app open get "Failed to fetch dynamically imported module" (or a ChunkLoadError) and a white screen. Please:
1. In the Vite entry file, before createRoot / render, add:
   window.addEventListener('vite:preloadError', (event) => {
     const key = 'chunk-reload-at';
     const last = Number(sessionStorage.getItem(key) || '0');
     if (Date.now() - last < 10000) return;
     event.preventDefault();
     sessionStorage.setItem(key, String(Date.now()));
     window.location.reload();
   });
   Wrap sessionStorage in try/catch and do nothing if it throws.
2. Tell me how to set Cache-Control: no-cache on index.html only, for my host (Vercel or Netlify). Leave hashed files cacheable.
3. If a rewrite sends every path to index.html, keep it for page routes, and do not serve index.html for missing files under /assets/.
4. Do not change routes or features. If this project is Next.js rather than Vite, say so and do not add the Vite event; describe the one-time reload in the existing error boundary instead.

FAQ

Why does a refresh fix it?

A full load downloads the new index.html, which points at the new hashed files. The old tab was still holding the previous HTML.

Will this loop forever?

Only if you reload on every failure with no guard. Skipping a second reload within a few seconds leaves a real outage visible instead of flashing the page.

Still broken?

Get a free diagnosis by email within 24 hours. Fixed or refunded.

Get a free diagnosis

Or get the weekly fix digest:

📬 This Week in Broken Apps: the most common Lovable/Supabase breaks and how to fix them. Free, weekly, unsubscribe anytime.

Free: the Vibe-Code Fix Checklist.

Sources