Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 1209fb9

Browse files
committed
Improve index.html loading
1 parent f7ab5a3 commit 1209fb9

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

server/html.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ import { getAlephPkgUri } from "./config.ts";
88

99
await initLolHtml(decodeLolHtmlWasm());
1010

11+
type LoadOptions = {
12+
isDev: boolean;
13+
ssr?: { suspense?: boolean };
14+
hmrWebSocketUrl?: string;
15+
};
16+
1117
// laod the `index.html`
1218
// - fix relative url to absolute url of `src` and `href`
1319
// - add `./framework/core/hmr.ts` when in `development` mode
1420
// - add `./framework/core/nomodule.ts`
1521
// - check the `<head>` and `<body>` elements
1622
// - check the `<ssr-body>` element if the ssr is enabled
1723
// - add `data-suspense` attribute to `<body>` if using suspense ssr
18-
export async function loadAndFixIndexHtml(isDev: boolean, ssr?: { suspense?: boolean }): Promise<Uint8Array> {
24+
export async function loadAndFixIndexHtml(options: LoadOptions): Promise<Uint8Array> {
1925
const { html, hasSSRBody } = await loadIndexHtml();
20-
return fixIndexHtml(html, { isDev, ssr, hasSSRBody });
26+
return fixIndexHtml(html, hasSSRBody, options);
2127
}
2228

2329
async function loadIndexHtml(): Promise<{ html: Uint8Array; hasSSRBody: boolean }> {
@@ -84,11 +90,8 @@ async function loadIndexHtml(): Promise<{ html: Uint8Array; hasSSRBody: boolean
8490
}
8591
}
8692

87-
function fixIndexHtml(
88-
html: Uint8Array,
89-
options: { isDev: boolean; ssr?: { suspense?: boolean }; hasSSRBody: boolean },
90-
): Uint8Array {
91-
const { isDev, ssr, hasSSRBody } = options;
93+
function fixIndexHtml(html: Uint8Array, hasSSRBody: boolean, options: LoadOptions): Uint8Array {
94+
const { isDev, ssr, hmrWebSocketUrl } = options;
9295
const alephPkgUri = getAlephPkgUri();
9396
const chunks: Uint8Array[] = [];
9497
const rewriter = new HTMLRewriter("utf8", (chunk: Uint8Array) => chunks.push(chunk));
@@ -157,6 +160,15 @@ function fixIndexHtml(
157160
},
158161
});
159162
}
163+
if (isDev && hmrWebSocketUrl) {
164+
rewriter.on("head", {
165+
element(el: Element) {
166+
el.append(`<script>window.__hmrWebSocketUrl=${JSON.stringify(hmrWebSocketUrl)};</script>`, {
167+
html: true,
168+
});
169+
},
170+
});
171+
}
160172

161173
try {
162174
rewriter.write(html);

server/mod.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,11 @@ export const serve = (options: ServerOptions = {}) => {
267267
let indexHtml: Uint8Array | null | undefined = Reflect.get(globalThis, "__ALEPH_INDEX_HTML");
268268
if (indexHtml === undefined) {
269269
try {
270-
indexHtml = await loadAndFixIndexHtml(isDev, typeof ssr === "function" ? {} : ssr);
270+
indexHtml = await loadAndFixIndexHtml({
271+
isDev,
272+
ssr: typeof ssr === "function" ? {} : ssr,
273+
hmrWebSocketUrl: options.hmrWebSocketUrl,
274+
});
271275
} catch (err) {
272276
if (err instanceof Deno.errors.NotFound) {
273277
indexHtml = null;
@@ -285,16 +289,6 @@ export const serve = (options: ServerOptions = {}) => {
285289
return new Response("Not Found", { status: 404 });
286290
}
287291

288-
if (isDev && options.hmrWebSocketUrl) {
289-
customHTMLRewriter.set("head", {
290-
element(el) {
291-
el.append(`<script>window.__hmrWebSocketUrl=${JSON.stringify(options.hmrWebSocketUrl)};</script>`, {
292-
html: true,
293-
});
294-
},
295-
});
296-
}
297-
298292
// render html
299293
return renderer.fetch(req, ctx, {
300294
indexHtml,

0 commit comments

Comments
 (0)