feat: allow passing additional args to HandlerContext.render (#82)

Co-authored-by: Luca Casonato <lucacasonato@yahoo.com>
This commit is contained in:
Leo Kettmeir
2022-02-11 01:25:47 +01:00
committed by GitHub
co-authored by Luca Casonato
parent 6abf62c832
commit 788a85c431
7 changed files with 75 additions and 18 deletions
+2 -1
View File
@@ -203,7 +203,7 @@ export class ServerContext {
params: Record<string, string | string[]>,
) => {
if (page.component === undefined) return undefined;
const render = async () => {
const render = async (renderData?: Record<string, unknown>) => {
const preloads = page.runtimeJS
? this.#bundler.getPreloads(bundlePath).map(bundleAssetUrl)
: [];
@@ -214,6 +214,7 @@ export class ServerContext {
renderer: this.#renderer,
url: new URL(req.url),
params,
renderData,
});
const headers: Record<string, string> = {
"content-type": "text/html; charset=utf-8",
+7 -1
View File
@@ -18,6 +18,7 @@ export interface RenderOptions {
url: URL;
params: Record<string, string | string[]>;
renderer: Renderer;
renderData?: Record<string, unknown>;
}
export type RenderFn = () => void;
@@ -114,7 +115,12 @@ function defaultCsp() {
export async function* render(
opts: RenderOptions,
): AsyncIterable<string | [string, ContentSecurityPolicy | undefined]> {
const props = { params: opts.params, url: opts.url, route: opts.page.route };
const props = {
params: opts.params,
url: opts.url,
route: opts.page.route,
renderData: opts.renderData,
};
const csp: ContentSecurityPolicy | undefined = opts.page.csp
? defaultCsp()
+1 -1
View File
@@ -12,7 +12,7 @@ export interface PageModule {
export interface HandlerContext {
req: Request;
match: Record<string, string>;
render?: () => Promise<Response>;
render?: (args?: Record<string, unknown>) => Promise<Response>;
}
export type Handler = (ctx: HandlerContext) => Response | Promise<Response>;