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:
@@ -18,6 +18,7 @@ async function ensureEsbuildInialized() {
|
||||
export class Bundler {
|
||||
#pages: Page[];
|
||||
#cache: Map<string, Uint8Array> | Promise<void> | undefined = undefined;
|
||||
#preloads = new Map<string, string[]>();
|
||||
|
||||
constructor(pages: Page[]) {
|
||||
this.#pages = pages;
|
||||
@@ -42,10 +43,21 @@ export class Bundler {
|
||||
outfile: "",
|
||||
jsxFactory: "h",
|
||||
jsxFragment: "Fragment",
|
||||
metafile: true,
|
||||
outdir: `/`,
|
||||
entryPoints,
|
||||
});
|
||||
|
||||
const metafileOutputs = bundle.metafile!.outputs;
|
||||
|
||||
for (const path in metafileOutputs) {
|
||||
const meta = metafileOutputs[path];
|
||||
const imports = meta.imports
|
||||
.filter(({ kind }) => kind === "import-statement")
|
||||
.map(({ path }) => `/${path}`);
|
||||
this.#preloads.set(`/${path}`, imports);
|
||||
}
|
||||
|
||||
const cache = new Map<string, Uint8Array>();
|
||||
for (const file of bundle.outputFiles) {
|
||||
cache.set(file.path, file.contents);
|
||||
@@ -69,6 +81,10 @@ export class Bundler {
|
||||
const cache = await this.cache();
|
||||
return cache.get(path) ?? null;
|
||||
}
|
||||
|
||||
getPreloads(path: string): string[] {
|
||||
return this.#preloads.get(path) ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
function freshPlugin(pageList: Page[]): esbuild.Plugin {
|
||||
|
||||
+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>`;
|
||||
}
|
||||
|
||||
+12
-5
@@ -10,11 +10,16 @@ export function installRoutes(
|
||||
pages: Page[],
|
||||
apiRoutes: ApiRoute[],
|
||||
) {
|
||||
const bundler = new Bundler(pages);
|
||||
|
||||
for (const page of pages) {
|
||||
const bundlePath = `/${page.name}.js`;
|
||||
const imports = [bundleAssetUrl(bundlePath)];
|
||||
router.get<Record<string, string>>(page.route, (ctx) => {
|
||||
ctx.response.status = 200;
|
||||
ctx.response.type = "html";
|
||||
ctx.response.body = render(page, ctx.params);
|
||||
const preloads = bundler.getPreloads(bundlePath).map(bundleAssetUrl);
|
||||
ctx.response.body = render(page, imports, preloads, ctx.params);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -22,7 +27,7 @@ export function installRoutes(
|
||||
router.all(apiRoute.route, apiRoute.handler);
|
||||
}
|
||||
|
||||
const internal = internalRouter(pages);
|
||||
const internal = internalRouter(bundler);
|
||||
|
||||
router.use(
|
||||
INTERNAL_PREFIX,
|
||||
@@ -31,13 +36,15 @@ export function installRoutes(
|
||||
);
|
||||
}
|
||||
|
||||
function bundleAssetUrl(path: string) {
|
||||
return `${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}${path}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a router that contains all fresh routes. Should be mounted at
|
||||
* constants.INTERNAL_PREFIX
|
||||
*/
|
||||
function internalRouter(pages: Page[]): oak.Router {
|
||||
const bundler = new Bundler(pages);
|
||||
|
||||
function internalRouter(bundler: Bundler): oak.Router {
|
||||
const router = new oak.Router();
|
||||
|
||||
router.get(`${JS_PREFIX}/${BUILD_ID}/:path*`, async (ctx) => {
|
||||
|
||||
Reference in New Issue
Block a user