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:
Tom
2023-06-06 12:36:04 +02:00
committed by GitHub
parent cd760b90d4
commit dd4e584a3e
6 changed files with 113 additions and 13 deletions
+36 -10
View File
@@ -168,18 +168,18 @@ export async function render<Data>(
let bodyHtml: string | null = null;
function realRender(): string {
function renderInner(): string {
bodyHtml = renderToString(vnode);
return bodyHtml;
}
const plugins = opts.plugins.filter((p) => p.render !== null);
const syncPlugins = opts.plugins.filter((p) => p.render);
const renderResults: [Plugin, PluginRenderResult][] = [];
function render(): PluginRenderFunctionResult {
const plugin = plugins.shift();
function renderSync(): PluginRenderFunctionResult {
const plugin = syncPlugins.shift();
if (plugin) {
const res = plugin.render!({ render });
const res = plugin.render!({ render: renderSync });
if (res === undefined) {
throw new Error(
`${plugin?.name}'s render hook did not return a PluginRenderResult object.`,
@@ -187,7 +187,7 @@ export async function render<Data>(
}
renderResults.push([plugin, res]);
} else {
realRender();
renderInner();
}
if (bodyHtml === null) {
throw new Error(
@@ -200,13 +200,39 @@ export async function render<Data>(
};
}
await opts.renderFn(ctx, () => render().htmlText);
const asyncPlugins = opts.plugins.filter((p) => p.renderAsync);
if (bodyHtml === null) {
throw new Error("The `render` function was not called by the renderer.");
async function renderAsync(): Promise<PluginRenderFunctionResult> {
const plugin = asyncPlugins.shift();
if (plugin) {
const res = await plugin.renderAsync!({ renderAsync });
if (res === undefined) {
throw new Error(
`${plugin?.name}'s async render hook did not return a PluginRenderResult object.`,
);
}
renderResults.push([plugin, res]);
if (bodyHtml === null) {
throw new Error(
`The 'renderAsync' function was not called by ${plugin?.name}'s async render hook.`,
);
}
} else {
await opts.renderFn(ctx, () => renderSync().htmlText);
if (bodyHtml === null) {
throw new Error(
`The 'render' function was not called by the legacy async render hook.`,
);
}
}
return {
htmlText: bodyHtml,
requiresHydration: ENCOUNTERED_ISLANDS.size > 0,
};
}
bodyHtml = bodyHtml as string;
await renderAsync();
bodyHtml = bodyHtml as unknown as string;
const moduleScripts: [string, string][] = [];
+20 -2
View File
@@ -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;
}