Skip to content
FunnelGuard

Fix guide by FunnelsGuard

ChunkLoadError on a lazy-loaded route: how to find and fix it

PRF-123High impactDetectedBy FunnelGuardPublished Updated
On this page

What this issue is

Modern sites built with React, Next.js, Vue, Angular or similar tools split their JavaScript into chunks. A chunk is a small file that loads only when a visitor needs a certain page or component, for example the checkout or a booking form. This is called lazy loading.

A ChunkLoadError is thrown by webpack when that file cannot be loaded. Typical messages are ChunkLoadError: Loading chunk 123 failed and Loading CSS chunk failed. Vite and native ES modules show a similar message: Failed to fetch dynamically imported module. The cause is different from a normal script bug: the code is fine, but the file is missing, blocked or wrong.

Why it costs you leads or sales

The failing chunk is often the one behind the page that matters: the lead form, the cart, the checkout or a booking widget. The visitor clicks a link, nothing renders and they leave. Your ads and analytics usually still count the earlier page view, so traffic looks healthy while conversions drop.

It can also hit only some visitors, such as people who had a tab open during a deploy, people behind a certain network or those served by one CDN location. That makes it easy to miss in manual testing.

Where a ChunkLoadError breaks the funnel
  1. Ad clickVisitor lands on the site
  2. Landing pageMain bundle loads fine
  3. Click to checkoutRoute chunk request fails (404 or blocked)
  4. Form or checkoutNever renders
  5. ConversionNo lead, no sale, no tracking hit

The landing page works, but the lazy route's chunk fails, so every later step is lost.

Conversions before and after a broken chunkExample numbers
Conversions before and after a broken chunk (example)Before deploy: 40 leads per day; Chunk broken: 9 leads per day; After fix: 40 leads per day0153140940Before deployChunk brokenAfter fixleads per day

Example: a site that normally gets 40 leads a day drops sharply after a deploy leaves a route chunk unreachable.

How to check if your site has it

Check for a ChunkLoadError
  1. Open the page and the browser console

    In Chrome press F12, open the Console tab and reload. Then click through to the lazy route (checkout, form, booking page). Look for ChunkLoadError or Failed to fetch dynamically imported module.

  2. Check the Network tab

    Filter by JS. Find the red request for the chunk file. Note the status (404, 403, 5xx, blocked) and the full URL it tried to load.

  3. Compare the URL with your live files

    Open the failing URL directly. A 404 means the file no longer exists. An HTML page returned instead of JavaScript means a rewrite rule or CDN is serving the wrong thing.

  4. Test with a cold cache

    Use a private window, then test again on mobile data. Try a tab that was opened before your last deploy, since that is the classic trigger.

What the failing request tells you
What you seeLikely cause
404 on a hashed file like 123.a1b2c3.jsOld chunk deleted by a deploy while cached HTML still points to it
200 but content is HTMLRewrite or single-page-app fallback serving index.html for a missing JS file
Request goes to the wrong domain or pathWrong publicPath, assetPrefix or base setting
Blocked by CORS or CSPCDN on another domain without the right headers or policy
Timeout or 5xxCDN or origin problem, or a flaky network

How to fix it

The right fix depends on the cause you found. Work through these steps in order.

Fix a ChunkLoadError
  1. Keep old chunks available after a deploy

    Do not delete the previous build's static files straight away. Keep them for at least a few days so open tabs and cached pages can still load them. Many hosts do this with immutable, versioned asset folders.

  2. Fix the public path

    Make sure the asset base matches where files really live: output.publicPath in webpack, assetPrefix in Next.js, base in Vite. A missing trailing slash or a wrong CDN domain is a common mistake.

  3. Set correct caching

    Serve HTML with a short cache or no-cache so it always points to current chunks. Serve hashed JS and CSS files with a long immutable cache. Purge the CDN after each deploy.

  4. Stop the SPA fallback hijacking missing files

    Make sure requests for .js and .css files return a real 404 when missing, not index.html. A missing file is then clear instead of causing a confusing syntax error.

  5. Add a retry and a safe fallback

    Retry the import once or twice, then reload the page once to pick up the new build. Show a clear message with a link if it still fails. See the snippet below.

Retry a lazy import, then reload oncejavascript
import { lazy } from 'react';

function lazyWithRetry(factory) {
  return lazy(async () => {
    const key = 'chunk-reloaded';
    try {
      const mod = await factory();
      sessionStorage.removeItem(key);
      return mod;
    } catch (err) {
      if (!sessionStorage.getItem(key)) {
        sessionStorage.setItem(key, '1');
        window.location.reload();
        return new Promise(() => {});
      }
      throw err;
    }
  });
}

const Checkout = lazyWithRetry(() => import('./Checkout'));
Where to change it on common setups
  1. Set the asset path

    webpack: output.publicPath. Next.js: assetPrefix in next.config.js. Vite: base in vite.config.js. Rebuild and redeploy.

  2. Keep old builds

    Change your deploy script so it adds new files without removing the previous build's hashed assets immediately.

  3. Fix cache headers

    Set Cache-Control: no-cache on HTML and public, max-age=31536000, immutable on hashed assets.

How to confirm it's fixed

Confirmation checks
  • The lazy route renders in a private window with an empty cache
  • No ChunkLoadError or failed dynamic import in the console
  • Every JS and CSS request returns 200 with the correct content type
  • A tab opened before the deploy still loads the route or reloads cleanly once
  • The form or checkout can be completed and the tracking hits fire

How to prevent it happening again

How a deploy causes a ChunkLoadError
  1. 10:00

    A visitor loads the site. The HTML points to chunk 123.a1b2.js.

  2. 10:05

    You deploy a new build. Old files are deleted.

  3. 10:06

    The visitor clicks to checkout. The browser requests the deleted chunk and gets a 404.

  4. 10:07

    ChunkLoadError. The page stays blank and the visitor leaves.

  5. After fix

    Old chunks are kept and a one-time reload fallback recovers open tabs.

Prevention checklist
  • Keep previous build assets for several days after each deploy
  • Use no-cache on HTML and immutable caching on hashed files
  • Purge the CDN as part of every deploy
  • Wrap lazy imports with a retry and a one-time reload
  • Run a test of the key journeys right after every release

How FunnelGuard handles this

FunnelGuard detects this today. A robot visitor runs your real journeys on a schedule, such as a lead form or checkout, and watches the browser console. If a ChunkLoadError appears, it is flagged as a console error on that step. A failure is re-checked from a second region before one alert goes out, with a screenshot and a likely cause.

This helps you catch the problem soon after a deploy rather than from lost leads. FunnelGuard reports what it sees on the journey it runs. It cannot see errors that only affect a visitor on a particular network or browser it did not test. You can start with the Free plan (1 site, 2 journeys, daily checks, no card), or try a paid plan with a 3-day free trial.

Questions people ask

Why is my website showing ChunkLoadError: Loading chunk failed?

The browser could not download a JavaScript file that the page loads on demand. The most common reasons are a recent deploy that removed old files, a wrong asset path, or a cache or CDN serving a missing or wrong file. Open the Network tab to see the failing URL and its status code.

How do I fix ChunkLoadError after a deploy?

Keep the previous build's hashed files on the server for a few days, serve HTML with no-cache, and purge your CDN on each deploy. Also add a retry and a one-time page reload to lazy imports so open tabs recover on their own.

Why does ChunkLoadError only happen to some visitors?

It often affects people who opened the site before a deploy, or those served by a stale CDN or browser cache. Network blocks, ad blockers or flaky connections can also stop a chunk loading. That is why it can be missed in your own testing.

What is the difference between ChunkLoadError and Failed to fetch dynamically imported module?

They are the same kind of problem reported by different tools. ChunkLoadError comes from webpack, while Failed to fetch dynamically imported module is the browser message for native ES module imports used by Vite and others. Both mean a lazily loaded file could not be fetched.

Can ChunkLoadError lose me leads or sales?

Yes. If the failing chunk powers a form, cart or checkout, the visitor sees a blank or stuck page and cannot convert. Early page views still get counted, so traffic can look normal while conversions fall.

Should I just reload the page when a chunk fails to load?

Reloading once is a good fallback, because it fetches the new HTML that points to current chunks. Limit it to one reload per session so a file that is truly missing does not cause an endless loop. Show a clear message if it still fails.

How can I be alerted when ChunkLoadError happens on my site?

You need something that runs your key journeys regularly and watches the console. FunnelGuard does this with a robot visitor and flags a ChunkLoadError as a console error on the step, then sends one alert after re-checking from a second region.

Does FunnelGuard catch this?

Detected

FunnelGuard checks for this today.

How: console error.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.