perf: add loading optimization with modulepreload (#15)

This commit aims to speed up loading of chunked modules using
the link rel=modulepreload directive.
This commit is contained in:
Luca Casonato
2021-06-02 12:55:30 +02:00
committed by GitHub
parent 26189cfdf3
commit 41307a5ac8
3 changed files with 54 additions and 10 deletions
+26 -5
View File
@@ -1,12 +1,33 @@
import { renderToString } from "./deps.ts";
import * as rt from "../runtime/deps.ts";
import { Page } from "./routes.ts";
import { BUILD_ID, INTERNAL_PREFIX, JS_PREFIX } from "./constants.ts";
export function render(page: Page, params: Record<string, string>): string {
export function render(
page: Page,
imports: string[],
preloads: string[],
params: Record<string, string>,
): string {
const props = { params };
const propsStr = JSON.stringify(props);
const body = renderToString(rt.h(page.component, props));
return `<!DOCTYPE html><html><head><script src="${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/${page.name}.js" type="module"></script></head><body>${body}<script id="__FRSH_PROPS" type="application/json">${
JSON.stringify(props)
}</script></body></html>`;
return `<!DOCTYPE html>
<html>
<head>
${
preloads.map((src) => `<link rel="modulepreload" href="${src}">`).join(
"\n ",
)
}
${
imports.map((src) => `<script src="${src}" type="module"></script>`).join(
"\n ",
)
}
</head>
<body>
${body}
<script id="__FRSH_PROPS" type="application/json">${propsStr}</script>
</body>
</html>`;
}