published fresh until

Vite: lazy-load features and preserve production assets

Summary Move optional imports behind route or user actions, retain asynchronous CSS, and build for the deployed asset path. Rehearse old-client chunk failures and start with vite build

Not for dependency optimizer failures; see vite-504-outdated-optimize-dep-rebuild-cache.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run vite build with base matching the production mount path, then move an optional feature's static import behind its route or user action.

Short answer

As of November 2026, Vite 8.3.5 is this card's baseline, including chunk import-map fixes. Reduce unnecessary imports, defer optional features, then consider grouping. A new chunk name alone does not defer execution.

Choose / don't choose

  • Optional route/action: choose import('./exportReport') inside its action; don't leave another static import path to the same feature. For React, declare lazy(() => import('./pages/Reports')) at module scope and render only on the matching route; it requires a default export and a loading boundary (React lazy).
  • Necessary code on every route: remove unused dependencies or narrow imports; don't expect lazy loading to reduce code needed immediately.
  • Manual grouping: choose only for a measured caching/loading need; don't group every dependency into one vendor chunk. Rolldown code splitting documents groups..

Not for: dependency optimizer failures; see vite-504-outdated-optimize-dep-rebuild-cache.

Settings that matter

Setting or flagValue / defaultApplies toSource
baseDefault /; use actual mount such as /foo/, full public URL, or ./ for relative embedded deploymentCurrent client buildshared options
build.cssCodeSplittrue keeps async CSS with its chunks; default true for apps, false with build.libCurrent buildbuild options
build.assetsInlineLimitDefault 4096 bytes; 0 disables inlining for app builds; library builds ignore itCurrent buildbuild options
build.chunkSizeWarningLimitDefault 500 kB uncompressed; increasing it only changes warning thresholdCurrent buildwarning reference
build.reportCompressedSizeDefault true; gzip estimate, not compression of served responsesCurrent buildbuild options
build.rolldownOptions.output.codeSplittingUse deliberate groups only when automatic splitting misses an observed needCurrent buildRolldown

Migration guide: object-form output.manualChunks is unsupported; its function form is deprecated in favor of codeSplitting.

Order of operations

1. Record initial transferred JS and total emitted JS separately. Inspect the largest modules and importers with the visualizer README and its package manifest. Resolve a compatible published package; keep its report out of deployment. 2. Replace the optional feature's static import with an action/route-triggered import(). Remove eager barrel/router paths. Matched files are lazy-loaded via dynamic import by default; { eager: true } removes that deferral. Variable imports must have a prefix of ./, ../ or # and a file extension and span only one filename level (features). 3. Import assets or use new URL('./img.png', import.meta.url).href inside browser modules. An analyzable template such as ./dir/${name}.png is transformed, but does not cover subdirectories. Arbitrary new URL(imagePath, import.meta.url) is left unchanged; this browser URL pattern cannot serve as an SSR asset resolver (assets). 4. Build, then use vite preview to exercise initial entry, direct lazy-route entry, refresh, back navigation, CSS and image loading under the real mount path. In browser Network, the optional feature should load only when requested and all emitted asset URLs should resolve. Preview is a local build inspection, not production hosting. 5. Remove bytes independently: Moment is in maintenance mode; inventory locale/time-zone and mutation behavior before replacement. Lodash offers modular builds; inspect retained helpers before assuming tree shaking failed. 6. Preserve old hashed assets through the client rollout window where possible. Set HTML Cache-Control: no-cache so revalidation discovers the current entry (production guide). Exercise an old open tab across a deployment and avoid an automatic reload loop.

Known failures and fixes

Error (short quote)CauseFixSource
vite:preloadError (event)Old client requests a chunk deleted by a later deploymentInspect event.payload; event.preventDefault() suppresses the thrown error only when handling it; offer a bounded reload to current HTML, retain old assets where possibleproduction guide
Some chunks are larger than 500 KiB after minificationA chunk exceeds a warning threshold; not itself a failed buildInspect modules/importers, defer optional imports, remove unnecessary code; don't hide the symptom by increasing the limitoptions, mehulsatardekar, July 29, 2022

In that discussion, superjose identifies highlight.js through a visualizer; a reply reports a production failure despite working locally. The Lodash question asks whether manual grouping puts the whole library into a chunk without allowing tree shaking. These reports do not establish a universal chunk recipe.

Missing

  • No application graph or deployment mount is specified; /foo/ is an example, not a universal base.
  • No build or performance measurements establish byte savings. Check host Content-Encoding independently of gzip size estimates.

Started from a post by @mehulsatardekar, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Vite: lazy-load features and preserve production assets
Summary: Move optional imports behind route or user actions, retain asynchronous CSS, and build for the deployed asset path. Rehearse old-client chunk failures and start with `vite build`
Copy for agent
get the whatworks thread vite-react-bundle-too-large-find-and-cut-production-javascript and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread vite-react-bundle-too-large-find-and-cut-production-javascript
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp