# On Demand and When Injection

In addition to the code package download, code injection is also a major time-consuming process during Mini Program startup. The size of the injected code is positively related to the memory footprint and the injection time.

utilizeInjection on demandandTime injectionCan optimize the time and memory footprint of the code injection link.

# Injection on demand

Base library 2.11.1 And above version support, 2.11.1 The following are compatible but not optimized. Tool debugging please use the 1.05.2111300 And above version, basic library selection 2.20.1 And above versions.

Typically, at Mini Program startup, all of the code packages that the launch page depends on (master package, subcontract, plugin package, extension library, etc.) JS The code is injected into all of them, including other pages that are not visited and custom components that are not used. JS The code will be executed immediately. This causes a lot of unused code to be injected into the Mini Program runtime environment, affecting injection time and memory footprint.

Self-base library version 2.11.1 Since then, Mini Programs support the selective injection of necessary code through configuration to reduce the startup time and runtime memory of Mini Programs.

{
  "lazyCodeLoading": "requiredComponents"
}

# Note

  • When on-demand injection is enabled, the Mini Program injects only the custom components and page code needed to currently access the page. Unvisited pages, custom components not declared on the current page will not be loaded and initialized, and the corresponding code files will not be executed.Please be sure to confirm the normal performance of the Mini Program after modifying the configuration
  • With on-demand injection enabled, the page JSON All components defined in the configuration and app.json in usingComponents Configured global custom components are treated as dependencies of the page and injected and loaded. Suggest that the developerTimely removal JSON A declaration that custom components are not used in, and try toAvoid declaring low-usage custom components globally, otherwise it may affect the effect of on-demand injection.
  • Plug-in packages and extension libraries do not currently support on-demand injection. If you need to implement plug-in on-demand loading, consider placing the plug-in under a subcontract and passing theSubcontract asynchronyThe form ofAsynchronous introduction

# Time injection

Base library 2.11.2 And above version support, 2.11.2 The following and unconfigured have the same effect. Tool debugging please use the 1.05.2111300 And above version, basic library selection 2.20.1 And above versions.

In openingInjection on demandCharacteristics of the premise,Time injectionYou can specify that some custom components are not injected at Mini Program startup, but when they are actually rendered.

After you have specified lazyCodeLoading for requiredComponents To configure the custom component in the case of Placeholder component, the component is automatically treated as an injection component:

  1. Within each page, the component is not injected until it is first rendered
  2. Within each page, when the component is rendered for the first time, the component is rendered to its corresponding placeholder component, and injection begins at the end of the rendering process.
  3. After injection, the placeholder component is replaced back into the corresponding component.