refactor: centralize server into ServerContext (#26)

This commit centralizes all server related logic into a central
ServerContext class.
This commit is contained in:
Luca Casonato
2021-06-10 20:46:10 +02:00
committed by GitHub
parent 78c144641c
commit 572b8123c4
9 changed files with 236 additions and 224 deletions
+19 -15
View File
@@ -1,28 +1,32 @@
import { renderToString } from "./deps.ts";
import * as rt from "../runtime/deps.ts";
import { Page } from "./routes.ts";
import { Page } from "./types.ts";
export function render(
page: Page,
imports: string[],
preloads: string[],
params: Record<string, string>,
): string {
const props = { params };
export interface RenderOptions {
page: Page;
imports: string[];
preloads: string[];
params: Record<string, string>;
}
export function render(options: RenderOptions): string {
const props = { params: options.params };
const propsStr = JSON.stringify(props);
const body = renderToString(rt.h(page.component, props));
const body = renderToString(rt.h(options.page.component, props));
return `<!DOCTYPE html>
<html>
<head>
${
preloads.map((src) => `<link rel="modulepreload" href="${src}">`).join(
"\n ",
)
options.preloads.map((src) => `<link rel="modulepreload" href="${src}">`)
.join(
"\n ",
)
}
${
imports.map((src) => `<script src="${src}" type="module"></script>`).join(
"\n ",
)
options.imports.map((src) => `<script src="${src}" type="module"></script>`)
.join(
"\n ",
)
}
</head>
<body>