feat: support async layout functions (#1597)
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
co-authored by
Asher Gomez
parent
11077227b5
commit
8325225a0d
+111
-33
@@ -11,10 +11,12 @@ import {
|
||||
} from "preact";
|
||||
import {
|
||||
AppModule,
|
||||
AsyncLayout,
|
||||
AsyncRoute,
|
||||
ErrorPage,
|
||||
Island,
|
||||
LayoutModule,
|
||||
LayoutProps,
|
||||
Plugin,
|
||||
PluginRenderFunctionResult,
|
||||
PluginRenderResult,
|
||||
@@ -137,6 +139,13 @@ function defaultCsp() {
|
||||
};
|
||||
}
|
||||
|
||||
function checkAsyncComponent(
|
||||
component: unknown,
|
||||
): component is AsyncRoute | AsyncLayout {
|
||||
return typeof component === "function" &&
|
||||
component.constructor.name === "AsyncFunction";
|
||||
}
|
||||
|
||||
/**
|
||||
* This function renders out a page. Rendering is synchronous and non streaming.
|
||||
* Suspense boundaries are not supported.
|
||||
@@ -145,8 +154,8 @@ export async function render<Data>(
|
||||
opts: RenderOptions<Data>,
|
||||
): Promise<[string, ContentSecurityPolicy | undefined] | Response> {
|
||||
const component = opts.route.component;
|
||||
const isAsyncComponent = typeof component === "function" &&
|
||||
component.constructor.name === "AsyncFunction";
|
||||
const layouts = opts.layouts;
|
||||
const isAsyncComponent = checkAsyncComponent(component);
|
||||
|
||||
const props: Record<string, unknown> = {
|
||||
params: opts.params,
|
||||
@@ -192,16 +201,37 @@ export async function render<Data>(
|
||||
|
||||
let bodyHtml: string | null = null;
|
||||
|
||||
function renderInner(vnode: ComponentChildren): string {
|
||||
const syncPlugins = opts.plugins.filter((p) => p.render);
|
||||
|
||||
function buildRoot(appComponent: VNode) {
|
||||
return h(CSP_CONTEXT.Provider, {
|
||||
value: csp,
|
||||
children: h(HEAD_CONTEXT.Provider, {
|
||||
value: headComponents,
|
||||
children: h(opts.app.default, {
|
||||
params: opts.params as Record<string, string>,
|
||||
url: opts.url,
|
||||
route: opts.route.pattern,
|
||||
data: opts.data,
|
||||
state: opts.state!,
|
||||
Component() {
|
||||
return appComponent;
|
||||
},
|
||||
}),
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
function renderInnerSync(vnode: ComponentChildren): string {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
let finalAppComp: VNode<any> = vnode as any;
|
||||
|
||||
let i = opts.layouts.length;
|
||||
while (i--) {
|
||||
const layout = opts.layouts[i];
|
||||
|
||||
const layout = layouts[i];
|
||||
const curComp = finalAppComp;
|
||||
finalAppComp = h(layout.default, {
|
||||
|
||||
finalAppComp = h(layout.default as ComponentType<LayoutProps>, {
|
||||
params: opts.params as Record<string, string>,
|
||||
url: opts.url,
|
||||
route: opts.route.pattern,
|
||||
@@ -213,22 +243,7 @@ export async function render<Data>(
|
||||
});
|
||||
}
|
||||
|
||||
const root = h(CSP_CONTEXT.Provider, {
|
||||
value: csp,
|
||||
children: h(HEAD_CONTEXT.Provider, {
|
||||
value: headComponents,
|
||||
children: h(opts.app.default, {
|
||||
params: opts.params as Record<string, string>,
|
||||
url: opts.url,
|
||||
route: opts.route.pattern,
|
||||
data: opts.data,
|
||||
state: opts.state!,
|
||||
Component() {
|
||||
return finalAppComp;
|
||||
},
|
||||
}),
|
||||
}),
|
||||
});
|
||||
const root = buildRoot(finalAppComp);
|
||||
bodyHtml = renderToString(root);
|
||||
|
||||
for (const [id, children] of SLOTS_TRACKER.entries()) {
|
||||
@@ -241,7 +256,6 @@ export async function render<Data>(
|
||||
}
|
||||
|
||||
const renderResults: [Plugin, PluginRenderResult][] = [];
|
||||
const syncPlugins = opts.plugins.filter((p) => p.render);
|
||||
if (isAsyncComponent && syncPlugins.length > 0) {
|
||||
throw new Error(
|
||||
`Async server components cannot be rendered synchronously. The following plugins use a synchronous render method: "${
|
||||
@@ -261,7 +275,7 @@ export async function render<Data>(
|
||||
}
|
||||
renderResults.push([plugin, res]);
|
||||
} else {
|
||||
renderInner(h(component as ComponentType, props));
|
||||
renderInnerSync(h(component as ComponentType, props));
|
||||
}
|
||||
if (bodyHtml === null) {
|
||||
throw new Error(
|
||||
@@ -293,15 +307,17 @@ export async function render<Data>(
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (isAsyncComponent) {
|
||||
if (
|
||||
isAsyncComponent ||
|
||||
opts.layouts.some((layout) => checkAsyncComponent(layout.default))
|
||||
) {
|
||||
if (opts.renderFn !== DEFAULT_RENDER_FN) {
|
||||
throw new Error(
|
||||
`Async server components are not supported with custom render functions.`,
|
||||
);
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const res = await (component as AsyncRoute<any>)(opts.request, {
|
||||
const context = {
|
||||
localAddr: opts.context.localAddr,
|
||||
remoteAddr: opts.context.remoteAddr,
|
||||
renderNotFound: opts.context.renderNotFound,
|
||||
@@ -309,12 +325,74 @@ export async function render<Data>(
|
||||
route: opts.route.pattern,
|
||||
params: opts.params as Record<string, string>,
|
||||
state: opts.state ?? {},
|
||||
});
|
||||
if (res instanceof Response) {
|
||||
asyncRenderResponse = res;
|
||||
bodyHtml = "";
|
||||
} else {
|
||||
renderInner(res);
|
||||
};
|
||||
|
||||
const renderStack = [
|
||||
...layouts.map((layout) => layout.default),
|
||||
component,
|
||||
];
|
||||
|
||||
const componentStack: (VNode | null)[] = new Array(renderStack.length)
|
||||
.fill(null);
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const preparedLayouts: ComponentType<any>[] = [];
|
||||
for (let i = 0; i < renderStack.length; i++) {
|
||||
const layout = renderStack[i];
|
||||
if (!layout) continue;
|
||||
|
||||
if (checkAsyncComponent(layout)) {
|
||||
const res = await layout(opts.request, context, {
|
||||
params: opts.params as Record<string, string>,
|
||||
url: opts.url,
|
||||
route: opts.route.pattern,
|
||||
data: opts.data,
|
||||
state: opts.state!,
|
||||
Component() {
|
||||
return componentStack[i + 1];
|
||||
},
|
||||
});
|
||||
if (res instanceof Response) {
|
||||
asyncRenderResponse = res;
|
||||
bodyHtml = "";
|
||||
break;
|
||||
}
|
||||
// deno-lint-ignore no-explicit-any
|
||||
preparedLayouts.push(() => res as any);
|
||||
} else {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
preparedLayouts.push(layout as any);
|
||||
}
|
||||
}
|
||||
|
||||
// Only continue rendering layouts if we didn't abort
|
||||
// earlier by returning a response object.
|
||||
if (!asyncRenderResponse) {
|
||||
const routeComponent = preparedLayouts[preparedLayouts.length - 1];
|
||||
let finalAppComp = h(routeComponent, props);
|
||||
componentStack.push(finalAppComp);
|
||||
|
||||
// Skip page component
|
||||
let i = preparedLayouts.length - 1;
|
||||
while (i--) {
|
||||
const layout = preparedLayouts[i];
|
||||
const curComp = finalAppComp;
|
||||
componentStack[i + 1] = curComp;
|
||||
|
||||
finalAppComp = h(layout, {
|
||||
params: opts.params as Record<string, string>,
|
||||
url: opts.url,
|
||||
route: opts.route.pattern,
|
||||
data: opts.data,
|
||||
state: opts.state!,
|
||||
Component() {
|
||||
return curComp;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const root = buildRoot(finalAppComp);
|
||||
bodyHtml = renderToString(root);
|
||||
}
|
||||
} else {
|
||||
await opts.renderFn(ctx, () => renderSync().htmlText);
|
||||
|
||||
+7
-1
@@ -176,9 +176,15 @@ export interface LayoutProps<T = any, S = Record<string, unknown>>
|
||||
extends PageProps<T, S> {
|
||||
Component: ComponentType<Record<never, never>>;
|
||||
}
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export type AsyncLayout<T = any, S = Record<string, unknown>> = (
|
||||
req: Request,
|
||||
ctx: RouteContext<T, S>,
|
||||
props: LayoutProps,
|
||||
) => Promise<ComponentChildren | Response>;
|
||||
|
||||
export interface LayoutModule {
|
||||
default: ComponentType<LayoutProps>;
|
||||
default: ComponentType<LayoutProps> | AsyncLayout;
|
||||
}
|
||||
|
||||
export interface LayoutRoute {
|
||||
|
||||
Reference in New Issue
Block a user