Render Preprocessing
In certain scenarios, applications need to perform preprocessing operations before rendering. Modern.js recommends using Runtime Plugins to implement this type of logic.
Defining a Runtime Plugin
Registering the Plugin
Register the plugin in your project's src/modern.runtime.ts file:
Use Case -- Global Data Injection
Through the context parameter of the onBeforeRender hook, you can inject global data into your application. Application components can access this data using the use(RuntimeContext) Hook.
This feature is particularly useful in the following scenarios:
- Applications requiring page-level preliminary data
- Custom data injection workflows
- Framework migration scenarios (e.g., migrating from Next.js)
Defining a Data Injection Plugin
Using Injected Data in Components
Using with SSR
In SSR scenarios, the browser can access data injected via onBeforeRender during server-side rendering. Developers can decide whether to re-fetch data on the browser side to override server data based on their requirements.