UNPKG

vite-preload

Version:

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

38 lines (34 loc) 1.09 kB
'use strict'; var reactLazyWithPreload = require('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 = reactLazyWithPreload.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())); } } exports.lazy = lazy; exports.preloadAll = preloadAll; //# sourceMappingURL=lazy.cjs.map