refactor: centralize server into ServerContext (#26)
This commit centralizes all server related logic into a central ServerContext class.
This commit is contained in:
+19
-15
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user