diff --git a/src/server/bundle.ts b/src/server/bundle.ts index aba02f62..e0beaaed 100644 --- a/src/server/bundle.ts +++ b/src/server/bundle.ts @@ -52,6 +52,7 @@ export class Bundler { #plugins: Plugin[]; #cache: Map | Promise | undefined = undefined; #dev: boolean; + dependencyMap = new Map(); constructor( islands: Island[], @@ -124,15 +125,15 @@ export class Bundler { jsx: JSX_RUNTIME_MODE[this.#jsxConfig.jsx], jsxImportSource: this.#jsxConfig.jsxImportSource, }); + const metafileOutputs = bundle.metafile!.outputs; - // 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); - // } + for (const path in metafileOutputs) { + const meta = metafileOutputs[path]; + const imports = meta.imports + .filter(({ kind }) => kind === "import-statement") + .map(({ path }) => `/${path}`); + this.dependencyMap.set(`/${path}`, imports); + } const cache = new Map(); const absDirUrlLength = toFileUrl(absWorkingDir).href.length; @@ -161,10 +162,6 @@ export class Bundler { const cache = await this.cache(); return cache.get(path) ?? null; } - - // getPreloads(path: string): string[] { - // return this.#preloads.get(path) ?? []; - // } } function buildIdPlugin(buildId: string): esbuildTypes.Plugin { diff --git a/src/server/context.ts b/src/server/context.ts index 9f59d05c..ff15666e 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -503,14 +503,13 @@ export class ServerContext { ); } - const preloads: string[] = []; const resp = await internalRender({ route, islands: this.#islands, plugins: this.#plugins, app: this.#app, imports, - preloads, + dependencyMap: this.#bundler.dependencyMap, renderFn: this.#renderFn, url: new URL(req.url), params, @@ -541,10 +540,7 @@ export class ServerContext { status: options?.status ?? status, statusText: options?.statusText, headers: options?.headers - ? { - ...headers, - ...options.headers, - } + ? { ...headers, ...options.headers } : headers, }); }; diff --git a/src/server/render.ts b/src/server/render.ts index 41e89a5a..20d054c0 100644 --- a/src/server/render.ts +++ b/src/server/render.ts @@ -26,7 +26,7 @@ export interface RenderOptions { plugins: Plugin[]; app: AppModule; imports: string[]; - preloads: string[]; + dependencyMap: Map; url: URL; params: Record; renderFn: RenderFunction; @@ -209,6 +209,7 @@ export async function render( bodyHtml = bodyHtml as string; const imports: [string, string][] = []; + const preloadSet = new Set(); function addImport(path: string): string { const randomNonce = crypto.randomUUID().replace(/-/g, ""); if (csp) { @@ -219,6 +220,11 @@ export async function render( } const url = bundleAssetUrl(path); imports.push([url, randomNonce]); + preloadSet.add(url); + for (const depPath of opts.dependencyMap.get(path) ?? []) { + const url = bundleAssetUrl(depPath); + preloadSet.add(url); + } return url; } @@ -332,11 +338,12 @@ export async function render( headComponents.splice(0, 0, node); } + const preloads = [...preloadSet]; const html = template({ bodyHtml, headComponents, imports, - preloads: opts.preloads, + preloads, lang: ctx.lang, }); diff --git a/tests/main_test.ts b/tests/main_test.ts index e823997f..054d7fcd 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -44,6 +44,7 @@ Deno.test("/ page prerender", async () => { body, ``, ); + assertStringIncludes(body, ` {