Does Vite's config caching cause stale asset references with dynamic imports?
0 reputation · 18 Jul 2022, 08:30 UTC
0 reputation · 18 Jul 2022, 08:30 UTC
Vite utilizes an esbuild-based pre-bundling process and internal caching to optimize development startup times. When using glob patterns within the configuration to manage assets or dynamic imports, there is a potential for the cache to persist outdated references even after the underlying file structure changes.
In environments where dynamic imports are frequently modified or generated, it is unclear if Vite's internal watcher consistently invalidates these specific cached entries without a full manual cache clearance.
vite clear-cache is the only reliable method to resolve 404 errors following dynamic import updates.What is the specific trigger for cache invalidation when glob patterns are used in the config? Does Vite provide a programmatic way to refresh these references without restarting the entire dev server?
35275 reputation · 18 Jul 2022, 10:59 UTC
No. Vite's config cache alone does not cause stale asset references for dynamic imports in Vite 5.x. Stale 404s after changing files that are referenced via glob patterns in config are a watcher invalidation gap, not a bug in dynamic import handling. Dynamic imports are tracked by the module graph and HMR. Config-derived assets are tracked separately and are only rebuilt when the config file itself changes.
vite clear-cache exists and removes the persistent .vite cache. It is a full, all-or-nothing operation and terminates in-memory dev server state. There is no granular API to invalidate only config-derived assets.When a glob pattern lives in vite.config.ts, e.g. for manualChunks, assets inclusion, or plugin options, Vite computes the matched set at config resolve time. The cache key incorporates file system metadata for the config, not for every file the glob can match. Therefore:
This is a config-derived asset graph staleness problem, not a dynamic import cache problem.
import.meta.glob instead of a config-time glob. Source-level globs are watched by the module graph.vite clear-cache then restart the dev server. Expect loss of HMR state.One missing diagnostic detail that changes the recommendation: Is the glob pattern used inside vite.config.ts for build/asset options, or is it used in application code via import.meta.glob / dynamic import expressions? Config-time globs require config reload; source-time globs are HMR-aware.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.