From 7b35cb07d4a9fd29b1445839a40b1f6a9ffc5ad3 Mon Sep 17 00:00:00 2001
From: ayame113 <40050810+ayame113@users.noreply.github.com>
Date: Thu, 1 Jun 2023 10:18:28 +0900
Subject: [PATCH] feat: preloading JavaScript files (#1189)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
close #1187
## points
- use `` tag and `Link: rel="modulepreload"`
header to preload dependencies
- No preloading at all for pages that don't use JavaScript at all.
- Information about preloading is delivered only when the JS file has
already been built. If the JS file hasn't finished building, it will
respond without preload information instead of waiting for the build and
then returning the HTML.
## performance comparison
I've deployed this branch to https://fresh-module-preload.deno.dev/ so
you can try it out and see how it behaves.
When I accessed [fresh.deno.dev](https://fresh.deno.dev/) and
[fresh-module-preload.deno.dev](https://fresh-module-preload.deno.dev/)
20 times each and measured the DOMContentLoaded time, it was 24% faster
(157.3ms -> 118.95ms) in my environment (Nagano City, Japan).
Actual measured data
(Don't forget to check `Disable Cache` in devtools)
| afeter (ms) | before (ms)
-- | -- | --
0 | 180 | 152
1 | 82 | 144
2 | 78 | 186
3 | 156 | 196
4 | 88 | 211
5 | 85 | 123
6 | 162 | 209
7 | 154 | 110
8 | 86 | 175
9 | 135 | 109
10 | 183 | 117
11 | 115 | 159
12 | 99 | 173
13 | 83 | 131
14 | 82 | 142
15 | 135 | 160
16 | 120 | 125
17 | 148 | 152
18 | 118 | 229
19 | 90 | 143
average | 118.95 | 157.3
## screen shots


---------
Co-authored-by: Luca Casonato
---
src/server/bundle.ts | 21 +++++++++------------
src/server/context.ts | 8 ++------
src/server/render.ts | 11 +++++++++--
tests/main_test.ts | 1 +
4 files changed, 21 insertions(+), 20 deletions(-)
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, ` {