mirror of
https://github.com/outline/outline.git
synced 2026-12-01 17:32:49 +03:00
Lazy components now render synchronously once their chunk is loaded and share a preload promise, so the editor no longer commits a fallback and waits out the 300ms retry throttle. Editor chunks are preloaded from the document route, the sidebar hover, and the data loader. A lint rule blocks direct imports of the editor chunk.
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import * as React from "react";
|
|
import type { LazyComponent as LazyComponentType } from "~/utils/lazyWithRetry";
|
|
import lazyWithRetry from "~/utils/lazyWithRetry";
|
|
|
|
// oxlint-disable no-explicit-any -- ComponentType<any> is the standard React pattern for generic component constraints
|
|
export interface LazyComponent<T extends React.ComponentType<any>> {
|
|
Component: LazyComponentType<T>;
|
|
preload: () => Promise<{ default: T }>;
|
|
}
|
|
|
|
interface LazyLoadOptions {
|
|
retries?: number;
|
|
interval?: number;
|
|
/** If provided, picks this named export from the module instead of `default`. */
|
|
exportName?: string;
|
|
}
|
|
|
|
/**
|
|
* Creates a lazy-loaded component with preloading capability and automatic retries on failure.
|
|
* Supports both default and named exports.
|
|
*
|
|
* @param factory A function that returns a promise of a module.
|
|
* @param options Optional configuration for retry behavior and export name.
|
|
* @returns An object containing the lazy Component and a preload function.
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* // Default export
|
|
* const MyComponent = createLazyComponent(() => import('./MyComponent'));
|
|
*
|
|
* // Named export
|
|
* const MyComponent = createLazyComponent(() => import('./MyComponent'), {
|
|
* exportName: 'MyComponent',
|
|
* });
|
|
* ```
|
|
*/
|
|
export function createLazyComponent<T extends React.ComponentType<any>>(
|
|
factory: () => Promise<Record<string, T>>,
|
|
options: LazyLoadOptions = {}
|
|
): LazyComponent<T> {
|
|
const { retries, interval, exportName } = options;
|
|
|
|
const wrappedFactory = exportName
|
|
? () =>
|
|
factory().then((m) => ({
|
|
default: m[exportName],
|
|
}))
|
|
: (factory as () => Promise<{ default: T }>);
|
|
|
|
const Component = lazyWithRetry(wrappedFactory, retries, interval);
|
|
|
|
return { Component, preload: Component.preload };
|
|
}
|