UNPKG

vite-preload

Version:

Speed up your Vite application by preloading server rendered lazy modules and stylesheets as early as possible

35 lines (32 loc) 1.02 kB
import { lazyWithPreload } from 'react-lazy-with-preload'; let preloads = []; /** * Drop in replacement for React.lazy that also supports preloading. * * Must be used to be able to call `preloadAll()` on the server. * * @example const LazyComponent = lazyWithPreload(() => import('./Component')); */ function lazy(factory) { const z = lazyWithPreload(factory); preloads.push(z.preload); return z; } /** * Preload all detected lazy() components. * * Can be used on the server to resolve all lazy imports before rendering to avoid the Suspense loading state to be triggered on the first render. */ async function preloadAll() { // Preload all lazy components up to a depth of 3 for (let i = 0; i < 3 && preloads.length > 0; i++) { const _preloads = preloads; preloads = []; if (_preloads.length === 0) { return; } await Promise.all(_preloads.map((preload) => preload())); } } export { lazy, preloadAll }; //# sourceMappingURL=lazy.js.map