feat: async plugin rendering (#745)
An async render hook is added to plugins that enables injecting stylesheets and scripts based on the rendered HTML text.
This commit is contained in:
+20
-2
@@ -269,11 +269,27 @@ export interface Plugin {
|
||||
* inject CSS into the page, or load additional JS files on the client.
|
||||
*/
|
||||
render?(ctx: PluginRenderContext): PluginRenderResult;
|
||||
|
||||
/** The asynchronous render hook is called on the server every time some
|
||||
* JSX needs to be turned into HTML, wrapped around all synchronous render
|
||||
* hooks. The render hook needs to call the `ctx.renderAsync` function
|
||||
* exactly once, and await the result.
|
||||
*
|
||||
* This is useful for when plugins are generating styles and scripts with
|
||||
* asynchronous dependencies. Unlike the synchronous render hook, async render
|
||||
* hooks for multiple pages can be running at the same time. This means that
|
||||
* unlike the synchronous render hook, you can not use global variables to
|
||||
* propagate state between the render hook and the renderer.
|
||||
*/
|
||||
renderAsync?(ctx: PluginAsyncRenderContext): Promise<PluginRenderResult>;
|
||||
}
|
||||
|
||||
export interface PluginRenderContext {
|
||||
render: PluginRenderFunction;
|
||||
}
|
||||
export interface PluginAsyncRenderContext {
|
||||
renderAsync: PluginAsyncRenderFunction;
|
||||
}
|
||||
|
||||
export interface PluginRenderResult {
|
||||
/** CSS styles to be injected into the page. */
|
||||
@@ -303,12 +319,14 @@ export interface PluginRenderScripts {
|
||||
}
|
||||
|
||||
export type PluginRenderFunction = () => PluginRenderFunctionResult;
|
||||
export type PluginAsyncRenderFunction = () =>
|
||||
| PluginRenderFunctionResult
|
||||
| Promise<PluginRenderFunctionResult>;
|
||||
|
||||
export interface PluginRenderFunctionResult {
|
||||
/** The HTML text that was rendered. */
|
||||
htmlText: string;
|
||||
/** If the renderer encountered any islands that require hydration on the
|
||||
* client.
|
||||
*/
|
||||
* client. */
|
||||
requiresHydration: boolean;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user