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:
+26
-5
@@ -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>`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user