/* Copyright 2026 Marimo. All rights reserved. */ import React from "react"; interface LazyComponentWithPreload { /** * Eagerly trigger the dynamic import. Returns the import promise so callers * can await it or attach error handling; safe to call multiple times (the * import is memoized). */ preload: () => Promise<{ default: React.ComponentType }>; Component: React.LazyExoticComponent>; } export const reactLazyWithPreload = ( factory: () => Promise<{ default: React.ComponentType }>, ): LazyComponentWithPreload => { let component: Promise<{ default: React.ComponentType }> | null = null; const preload = async () => { if (!component) { component = factory(); } return component; }; const LazyComponent = React.lazy(() => { return preload(); }); return { preload, Component: LazyComponent, }; };