From 41307a5ac86179a79f302cbcc4f692e5f9fe16e2 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 2 Jun 2021 12:55:30 +0200 Subject: [PATCH] perf: add loading optimization with modulepreload (#15) This commit aims to speed up loading of chunked modules using the link rel=modulepreload directive. --- src/server/bundle.ts | 16 ++++++++++++++++ src/server/render.ts | 31 ++++++++++++++++++++++++++----- src/server/router.ts | 17 ++++++++++++----- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/server/bundle.ts b/src/server/bundle.ts index 4b65ef02..1ecda520 100644 --- a/src/server/bundle.ts +++ b/src/server/bundle.ts @@ -18,6 +18,7 @@ async function ensureEsbuildInialized() { export class Bundler { #pages: Page[]; #cache: Map | Promise | undefined = undefined; + #preloads = new Map(); 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(); 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 { diff --git a/src/server/render.ts b/src/server/render.ts index d2d83063..f2834e89 100644 --- a/src/server/render.ts +++ b/src/server/render.ts @@ -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 { +export function render( + page: Page, + imports: string[], + preloads: string[], + params: Record, +): string { const props = { params }; + const propsStr = JSON.stringify(props); const body = renderToString(rt.h(page.component, props)); - return `${body}`; + return ` + + + ${ + preloads.map((src) => ``).join( + "\n ", + ) + } + ${ + imports.map((src) => ``).join( + "\n ", + ) + } + + + ${body} + + +`; } diff --git a/src/server/router.ts b/src/server/router.ts index 65c160ac..822efcb2 100644 --- a/src/server/router.ts +++ b/src/server/router.ts @@ -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>(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) => {