Fix "Failed to fetch dynamically imported module" after a deploy

TypeError: Failed to fetch dynamically imported module: https://your-app.com/assets/Dashboard-abc123.js, often together with: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html".

TypeError: Failed to fetch dynamically imported module right after you publish an update means a browser tab that was open before the deploy asked for a JavaScript chunk that your new build deleted. The person sees a blank page or a crash when they click to another page. A refresh fixes it for them, so you can’t reproduce it.

Quick fix

Add a vite:preloadError listener in src/main.tsx that reloads the page once (code below), and make sure your host doesn’t cache index.html. The reload picks up the new file names, so the old tab heals itself instead of showing a blank page.

window.addEventListener('vite:preloadError', (event) => {
  const last = Number(sessionStorage.getItem('chunk-reload-at') || 0);
  if (Date.now() - last > 10_000) {
    event.preventDefault(); // don't throw, we're reloading
    sessionStorage.setItem('chunk-reload-at', String(Date.now()));
    window.location.reload();
  }
});

What you see

You publish an update to your Lovable, Bolt or Cursor app. Most people are fine, but a few report a blank page or a crash when they click to another page, and your error tracker fills up with:

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

In Chrome’s console it often comes with a second line:

Failed to load module script: Expected a JavaScript module script but the
server responded with a MIME type of "text/html". Strict MIME type checking
is enforced for module scripts per HTML spec.

A hard refresh fixes it for that person, and you can’t reproduce it in a fresh tab. Both lines are quoted by people hitting it in production in Vite issue #11804.

Why it happens

Vite-based apps (what Lovable and Bolt generate) split each lazily loaded page into its own file, and the file name includes a hash of its content, like Dashboard-abc123.js. When you change the code and deploy again, the hash changes and the new deploy only contains Dashboard-9f8e7d.js.

Vite’s build docs describe exactly this: when a new deployment occurs, the hosting service may delete the assets from previous deployments, so a user who opened your site before the deploy is still running the old JavaScript, which tries to import the old chunk, and the import fails. The original issue reporter in #11804 saw it only “if we deploy while they have an active session”.

That’s why it clusters right after deploys, and why it’s rare for users who just arrived. The sections below cover the usual causes one at a time.

Cause 1: an old tab asks for a chunk the new deploy removed

This is the common case, and the fix is the reload above. Vite fires a vite:preloadError event when it fails to load a dynamic import, and its docs show reloading the page as the example handler. Put the listener at the top of your entry file (src/main.tsx or src/main.ts), before the app renders.

The reload fetches the new index.html, which points at the new chunk names. The 10-second guard follows the sessionStorage approach shared in #11804: if the page fails again right after reloading, let the error through instead of looping.

You can paste this into your AI builder with a prompt like “Add a vite:preloadError listener in main.tsx that reloads the page at most once every 10 seconds, using sessionStorage as a guard.”

Cause 2: index.html is cached, so the reload gets the old file names

Vite’s docs add one condition: set Cache-Control: no-cache on the HTML file, otherwise the old assets will still be referenced after the reload. The defaults already cover this on the two common hosts:

  • Netlify sends Cache-Control: public, max-age=0, must-revalidate for static files by default and invalidates its CDN cache on every deploy, according to its caching overview.
  • Vercel uses the same default, public, max-age=0, must-revalidate, which its Cache-Control docs say tells both the CDN and the browser not to cache.

So the usual culprit is something you added:

  1. A /* rule in _headers or vercel.json with a long max-age. Long caching belongs on /assets/* (the hashed files), not on the HTML.
  2. A service worker or PWA plugin that caches index.html.

To check, open DevTools > Network, reload, click the document request and read its cache-control header.

Cause 3: “Expected a JavaScript module script” (the host returns HTML for the missing file)

The text/html MIME line is the same problem seen from the server side. If your host rewrites every unknown path to index.html (the SPA rewrite you added so refreshes work), the request for the deleted .js file gets your HTML page back, and the browser refuses to run HTML as a module.

If it only happens after deploys, the fix is the same as cause 1. If it happens on every visit, even in a fresh tab, the files aren’t missing because of a deploy: the page is asking for them at the wrong path. That’s usually Vite’s base option not matching where the site is served, covered in Vite base path: blank page and assets 404.

Cause 4: no error boundary, so a failed chunk becomes a blank page

Even with the reload, show something useful when a lazy page can’t load. In React, wrap your lazy routes in an error boundary that checks the error message for Failed to fetch dynamically imported module or the shorter error loading dynamically imported module (both wordings are matched in code shared in the issue) and renders “A new version is available” with a Reload button. Commenters in #11804 shared this pattern as their workaround.

What about Vercel’s Skew Protection?

Vercel’s Skew Protection routes old clients to the deployment that served them, so their old chunks keep working. It’s available on Pro and Enterprise plans, works through supported frameworks (Next.js, Astro, SvelteKit, Nuxt, Qwik, React Router), and can’t reach deployments that were deleted. A plain Vite single-page app isn’t in that list, so the reload above is still the fix that works on any host and plan.

Check it worked

  1. Open your live app in a tab and leave it there.
  2. Change some text on a lazy-loaded page and deploy.
  3. In the old tab, navigate to that page. It should reload once and show the new version, with no blank screen.

If you don’t have error tracking yet, this is the kind of error you only hear about from annoyed users. If your Lovable app is blank on the published URL for every visitor, not only after a deploy, start with Lovable works in preview but is broken on the published site. For a person to look at deploys, caching, monitoring and the rest of your launch path, get a written pre-launch audit.

FAQ

Why does "Failed to fetch dynamically imported module" only happen after I deploy?

Vite gives every lazily loaded chunk a file name with a content hash. A new deploy ships new names, and the host may delete the old files. A tab opened before the deploy still runs the old JavaScript and asks for the old file name, which no longer exists.

Why does a hard refresh fix it?

A refresh downloads the new index.html, which points at the new chunk names. That is also why you usually cannot reproduce it in a fresh tab, and why reloading once on the vite:preloadError event is the fix Vite documents.

What does "Expected a JavaScript module script but the server responded with a MIME type of text/html" mean here?

Your host answered the request for the missing .js file with an HTML page, usually index.html from an SPA rewrite rule. The browser refuses to run HTML as a module. It is the same missing-chunk problem, and the same fix applies.

Will reloading on vite:preloadError cause an endless reload loop?

Not with a guard. Store the time of the last reload in sessionStorage and only reload if the previous one was more than about 10 seconds ago. If the chunk is still missing after one reload, the error goes through to your error boundary instead.

Does Vercel Skew Protection fix it?

Only on Pro and Enterprise plans and for the frameworks Vercel lists (Next.js, Astro, SvelteKit, Nuxt, Qwik, React Router). A plain Vite single-page app is not in that list, so the reload fix is still the one that works on any host.