Code Injection Optimization
Code injection is the critical phase after package download in the miniapp startup flow. The base library loads developer JS code into the JavaScript engine, completing parsing, compilation, and execution in sequence. The key characteristic of this phase is that all top-level code in loaded modules is executed immediately — including App() definition, global variable initialization, and App.onLaunch / App.onShow lifecycle callbacks.
The core strategy for optimizing code injection time is: minimize the amount of code that must run at startup, and defer non-urgent work until it's actually needed.
Minimize Top-Level Execution
Top-level module code (code outside function bodies) executes immediately at injection time. Performing complex computations, large data initialization, or synchronous reads at the top level adds directly to injection time.
Reduce Synchronous API Calls
Frequent TTT capability calls during startup may block the JS thread and slow code execution. Minimize or eliminate synchronous API calls (most synchronous APIs end with Sync).
Caching mechanism:
-
The base library caches the following APIs to avoid repeated calls:
getLaunchOptionsgetLaunchOptionsSync
-
The following APIs are called frequently but contain dynamic data that cannot be cached directly; handle caching yourself:
getSystemInfogetSystemInfoSync
For data related to
system,brand,model,platform, use the getDeviceInfo API, which has built-in caching.
- The following APIs contain dynamic data with no built-in caching; implement caching at the business layer:
getDeviceInfogetGroupInfogetCustomConfig
For non-persistent storage, prefer
globalDataover thegetStorageAPI.
Streamline App Initialization
In a Ray project, app.tsx executes immediately during injection. A common mistake is placing all initialization logic here:
Keep only logic that affects the first screen (such as login state checks). Defer everything else until after the home page component mounts: