In certain scenarios, applications need to perform preprocessing operations before rendering. Modern.js recommends using Runtime Plugins to implement this type of logic.
Register the plugin in your project's src/modern.runtime.ts file:
Through the context parameter of the onBeforeRender hook, you can inject global data into your application. Application components can access this data using the useRuntimeContext Hook.
This feature is particularly useful in the following scenarios:
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.
In earlier versions of Modern.js, it was possible to add render preprocessing logic through the init hook in routes/layout.tsx and the App.init method. These approaches are still supported, but we strongly recommend implementing with Runtime plugins instead.
In future versions, the init hook in routes/layout.tsx and the App.init method will be gradually deprecated. We recommend migrating to the Runtime plugin approach as soon as possible.