Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A lazy-loaded chunk will not load in a Vite app

ErrorTypeError: Failed to fetch dynamically imported module

Short answer

The browser asked for a JavaScript chunk and did not get it. After a new deploy the old page still points at chunk names that no longer exist. Other causes are an ad blocker that dislikes the file name, a flaky network, or a dynamic import path Vite cannot resolve. Open the Network tab, find the failed chunk and read its status.

  • Applies to Vite 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser DevTools Network tab, reload, trigger the lazy route and look at the failed .js request. A 404 or an HTML body points to a stale deploy. A request marked blocked or cancelled points to an extension or the network.

    npx vite build && npx vite preview
  2. 2Match what you see, then jump to its fix

    1. 1The error shows up for users who kept the tab open across a release; the chunk URL returns 404A new deploy removed the chunk an open tab still wantsFix
    2. 2The request in DevTools is shown as blocked by client, and it works in a private window with extensions offAn ad blocker or extension blocks the chunk by its nameFix
    3. 3It happens on mobile data or a weak connection, and a plain reload fixes itThe network dropped the requestFix
    4. 4Only imports with a template string or a variable fail, while imports with a plain string workThe import() path is built from a variable in a way Vite cannot followFix

What does the error look like?

  • TypeError: Failed to fetch dynamically imported module

Cause 1 of 4

A new deploy removed the chunk an open tab still wants

1Check

Vite names chunks with a content hash. When you publish a new build and delete the old files, a page loaded earlier still holds the old names. Compare the chunk name in the failing request with the files now on the server.

ls dist/assets | head

2Fix

Reload the page when a dynamic import fails, using the vite:preloadError event, and serve index.html with Cache-Control no-cache so returning users always get the current entry. Keeping the previous release's chunks on the server for a while also helps.

// main.js
window.addEventListener('vite:preloadError', (event) => {
  window.location.reload()
})

3Verify

Deploy twice with a tab left open on the first version, then click the lazy route. The page should reload and show the route.

curl -I https://example.com/index.html

Confirmed by vite.dev

Did this fix it?

Cause 2 of 4

An ad blocker or extension blocks the chunk by its name

1Check

Blocking is done by file name, so a chunk whose name matches a block list can be caught. Open the app in a profile with no extensions.

ls dist/assets

2Fix

Change the chunk naming pattern so names no longer look like blocked words.

// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
  build: {
    rolldownOptions: {
      output: { chunkFileNames: 'assets/chunk-[hash].js' },
    },
  },
})

3Verify

Rebuild, then load the lazy route with the extension switched on.

npx vite build

Confirmed by vite.dev

Did this fix it?

Cause 3 of 4

The network dropped the request

1Check

Modern browsers can retry a module import that failed on the network, older ones need a page reload. Check whether the failed request is a timeout or a reset rather than a 404.

curl -I https://example.com/assets/chunk-abc123.js

2Fix

Use the same vite:preloadError handler as above so the page reloads once instead of staying broken.

window.addEventListener('vite:preloadError', () => {
  window.location.reload()
})

3Verify

Throttle the network to Offline in DevTools, trigger the route, restore the network and confirm the page recovers.

# DevTools > Network > throttling: Offline, then No throttling

Confirmed by vite.dev

Did this fix it?

Cause 4 of 4

The import() path is built from a variable in a way Vite cannot follow

1Check

Vite accepts a variable in import() only when the path starts with ./, ../ or #, ends with a file extension, and names a folder one level deep. Look at the failing import().

grep -rn 'import(' src

2Fix

Write the path in the allowed form, or list the files with import.meta.glob and pick one from the map.

// allowed
const page = await import(`./pages/${name}.js`)

// or, for more complex cases
const modules = import.meta.glob('./pages/*.js')
const page2 = await modules[`./pages/${name}.js`]()

3Verify

Run the build and open each route that uses the dynamic import.

npx vite build && npx vite preview

Confirmed by vite.dev

Did this fix it?

How do I stop it coming back?

  • Send Cache-Control no-cache for index.html so the entry file is always fresh.
  • Keep the previous release's assets on the server for some time after each deploy.
  • Register the vite:preloadError handler once at the app entry.
  • Use import.meta.glob instead of building import paths from several variables.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.