On-demand Injection and On-use Injection

View Markdown

Last edited on 2026-08-21

During the Mini Program launch process, code injection is a primary time-consuming component, in addition to code package download. The volume of injected code is positively correlated with both memory usage and injection duration.

Leveraging the features of on-demand injection and on-use injection can optimize the time and memory consumption of the code injection phase.

On-Demand Injection

Supported in Base Library version 2.11.1 and above. Versions below 2.11.1 are compatible but offer no optimization benefits. For tool-based debugging, please use DevTools version 1.05.2111300 or later, with Base Library version set to 2.20.1 or above.

Typically, when a Mini Program starts, all JS code from the code packages required by the launch page (main package, subpackages, plugin packages, extension libraries, etc.) is merged and injected entirely. This includes JS code for unvisited pages and unused custom components, all of which are executed immediately. Consequently, a significant amount of unused code is injected and executed in the Mini Program runtime environment, impacting both injection latency and memory usage.

Starting from Base Library version 2.11.1, Mini Programs support selective injection of necessary code via configuration, thereby reducing Mini Program startup time and runtime memory footprint.

{
 "lazyCodeLoading": "requiredComponents"
}

Notes

  • After enabling on-demand injection, Mini Program will only inject the custom components and page code required for the currently accessed page. Unaccessed pages and custom components not declared in the current page will not be loaded or initialized, and their corresponding code files will not be executed. Developers must verify that Mini Program behaves correctly after modifying the configuration.
  • After enabling on-demand injection, all components defined in the page JSON configuration and global custom components configured via usingComponents in app.json will be treated as page dependencies and injected and loaded. It is recommended that developers promptly remove declarations of unused custom components in the JSON and avoid declaring custom components with low usage rates globally, otherwise the effectiveness of on-demand injection may be affected.
  • Plugin packages and extension libraries currently do not support on-demand injection. If you need to implement on-demand loading for plugins, consider placing the plugin in a subpackage and asynchronously importing it via "Subpackage Asynchronization".

On-demand Injection

Supported in Base Library 2.11.2 and later. Versions below 2.11.2 or those without configuration exhibit the same behavior. For debugging with the tool, please use version 1.05.2111300 or later, and select Base Library version 2.20.1 or later.

Prerequisite: The On-demand Injection feature must be enabled. In this case, On-demand Injection allows you to specify that certain custom components are not injected when Mini Program starts, but are injected only when they are actually rendered.

If lazyCodeLoading has already been set to requiredComponents, configuring a Placeholder Component for a custom component will automatically treat that component as an on-demand injection component:

  1. Within each page, the component will not be injected before it is rendered for the first time;
  2. Within each page, when the component is rendered for the first time, it is rendered as its corresponding placeholder component. The injection process begins after the rendering flow is complete;
  3. Once the injection is finished, the placeholder component is replaced by the corresponding component.