-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
serve_jsx.ts
48 lines (46 loc) 路 1.42 KB
/
serve_jsx.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React from "./vendor/https/dev.jspm.io/react/index.js";
import ReactDOMServer from "./vendor/https/dev.jspm.io/react-dom/server.js";
import { HttpHandler } from "./router.ts";
import { resolveIndexPath } from "./router_util.ts";
export type DFC<P = {}> = React.FC<P> & {
getInitialProps?: () => Promise<P>;
};
/** Serve jsx/tsx by dynamic import */
export function serveJsx(
dir: string,
parentComponent: any = React.Fragment
): HttpHandler {
return async function serveJsx(req) {
const { pathname } = new URL(req.url, "http://dummy");
const p = await resolveIndexPath(dir, pathname, [".tsx", ".jsx"]);
if (p) {
const jsx = await import(p);
const el = jsx.default as DFC;
if (!el) {
throw new Error(
"jsx: jsx/tsx files served by serveJsx must has default export!"
);
}
if (typeof el !== "function") {
throw new Error("jsx: default export must be React component!");
}
let props = {};
if (typeof el.getInitialProps === "function") {
props = await el.getInitialProps();
}
await req.respond({
status: 200,
headers: new Headers({
"content-type": "text/html; charset=UTF-8"
}),
body: ReactDOMServer.renderToString(
React.createElement(
parentComponent,
{},
React.createElement(el, props, [])
)
)
});
}
};
}