Plugin optimization patterns for faster startup
Apply these patterns when you develop or migrate a front-end plugin so that heavy interface code loads on-demand instead of at startup.
Each pattern keeps the plugin entry point lightweight and reduces the synchronous chunk that Red Hat Developer Hub downloads before the plugin registers its extensions. Use the following patterns to keep the plugin entry point lightweight.
Defer extension interfaces behind blueprint loaders Copy linkLink copied!
- Lazy-load extension user interfaces through blueprint loaders
-
Return the component from an
import()call inside the blueprintloader, and do not statically import the component in the same file. The bundler then places the component in an asynchronous chunk.// Avoid: MyPage is statically imported, so it stays in the synchronous chunk. import { MyPage } from './components/MyPage'; loader: async () => <MyPage />, // Prefer: MyPage loads only when the extension is rendered. loader: async () => { const { MyPage } = await import('./components/MyPage'); return <MyPage />; }, - Avoid fake-asynchronous loaders
- An
asyncloader that references a statically imported component is not lazy-loaded. Make the dynamicimport()the only reference to the component from the entry or extension-definition file. - Do not use
Promise.resolveas a substitute forimport() -
Returning
Promise.resolve({ Content: AlreadyImportedComponent })looks asynchronous but does not create an asynchronous chunk, because the component is still statically imported. Acomponentscallback, such as the one onHomePageWidgetBlueprint, must callimport()to defer the component.// Prefer: the component loads only when the widget renders. components: async () => { const { MyHomepageCard } = await import('./components/MyHomepageCard'); return { Content: () => <MyHomepageCard />, }; }, - Use conditional
import()for mutually exclusive user interfaces -
When a loader renders one of several components based on configuration, call
import()inside each branch so that only the selected component loads.loader: async () => { // layouts is provided by the extension configuration or inputs. if (layouts.length > 0) { const { LayoutSwitcher } = await import('../components/LayoutSwitcher'); return <LayoutSwitcher layouts={layouts} />; } const { EntityContent } = await import('../components/EntityContent'); return <EntityContent />; }, - Prefer a blueprint loader over a synchronous component
- When a blueprint accepts both a
componentand aloaderparameter, useloader.When a blueprint supports data-driven parameters, such as an icon name, a title, and a link, use those instead of a custom React component.
Defer blueprints that take a synchronous element or factory Copy linkLink copied!
- Defer heavy dependencies behind API factories
-
ApiBlueprintregistration is synchronous. When an API implementation depends on a large library, load the library inside the factory function withimport()so that the dependency loads only when the API is first used. Cache the returned promise with the??=operator so that repeated calls reuse the same asynchronous chunk.this.contentPromise ??= import('./FormDecoratorContent'); - Use thin lazy shells when a blueprint requires a synchronous element
-
Some blueprints, such as
AppDrawerContentBlueprintand certain root element blueprints, take a synchronouselementorcomponentparameter rather than aloader. Wrap the heavy user interface in a small module that exports only aReact.lazyandSuspenseshell, and import that shell from the entry file instead of the real component tree.The
lazyDrawerUi.tsxfile exports the lazy shell:// lazyDrawerUi.tsx: the entry file imports only this shell. import { lazy, Suspense } from 'react'; const LazyDrawerContent = lazy(() => import('./DrawerContent').then(m => ({ default: m.DrawerContent })), ); export const DrawerContentElement = ( <Suspense fallback={null}> <LazyDrawerContent /> </Suspense> );The
alpha.tsxentry file imports the shell and passes it to the blueprint:// alpha.tsx import { DrawerContentElement } from './lazyDrawerUi'; const MY_DRAWER_ID = 'my-plugin.drawer-content'; params: { id: MY_DRAWER_ID, element: DrawerContentElement }