Vite plugin that preloads all JS chunks and CSS stylesheets generated by dynamic imports (e.g., React.lazy) into the HTML entry point. As of v0.7.0, it automatically injects <link rel="modulepreload"> and <link rel="stylesheet"> tags for every chunk produced by code splitting, improving load performance by prefetching before the user navigates. It differs from manual chunking plugins by targeting framework-level lazy imports rather than Rollup's manualChunks. Released at a moderate cadence, it supports Vite 5+, TypeScript types out of the box, and offers options to filter chunks, choose preload vs prefetch mode, and generate a manifest JSON.
npm install vite-plugin-preloadVerified import paths — ran on the pinned version, not inferred.
Demonstrates the minimum Vite config to enable preloading of all dynamically-loaded JS chunks and CSS.
Use dynamic imports for code splitting instead of manualChunks. Or consider using Vite's built-in manualChunks preloading.
If you need immediate preloading, set mode to 'preload' (default).
Set format to false or provide a PrettierOptions object to control formatting.
Implement a proper filter function that returns true for chunks you want to preload, e.g., (chunk) => chunk.fileName.endsWith('.js') && chunk.isDynamicEntry.Set `generatePreloadManifestJsonPath` to a file path to produce a JSON manifest of all preload links.
Install the package: npm install -D vite-plugin-preload. Ensure Vite >=5.0.0 is installed.
Use default import: import preload from 'vite-plugin-preload'. Or in CJS: const preload = require('vite-plugin-preload').Upgrade to v0.7.0 or later: npm install vite-plugin-preload@latest. Check docs for options per version.
Only use this plugin in a Vite project. It must be placed in the Vite config's plugins array.