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

Commit c662de1

Browse files
author
X
authored
feat: add preload scripts
Preload All Scripts
2 parents bd5d0d9 + c7ce5b3 commit c662de1

File tree

2 files changed

+74
-39
lines changed

2 files changed

+74
-39
lines changed

html.ts

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,86 @@
1-
import util from './util.ts'
1+
import util from "./util.ts";
22

33
export function createHtml({
4-
lang = 'en',
4+
lang = "en",
55
head = [],
66
scripts = [],
77
body,
8-
minify = false
8+
minify = false,
99
}: {
10-
lang?: string,
11-
head?: string[],
12-
scripts?: (string | { id?: string, type?: string, src?: string, innerText?: string, nomodule?: boolean, async?: boolean, preload?: boolean })[],
13-
body: string,
14-
minify?: boolean
10+
lang?: string;
11+
head?: string[];
12+
scripts?:
13+
(string | {
14+
id?: string;
15+
type?: string;
16+
src?: string;
17+
innerText?: string;
18+
nomodule?: boolean;
19+
async?: boolean;
20+
preload?: boolean;
21+
})[];
22+
body: string;
23+
minify?: boolean;
1524
}) {
16-
const eol = minify ? '' : '\n'
17-
const indent = minify ? '' : ' '.repeat(4)
18-
const headTags = head.map(tag => tag.trim())
19-
.concat(scripts.map(v => {
25+
const eol = minify ? "" : "\n";
26+
const indent = minify ? "" : " ".repeat(4);
27+
const headTags = head.map((tag) => tag.trim())
28+
.concat(scripts.map((v) => {
2029
if (!util.isString(v) && util.isNEString(v.src)) {
21-
if (v.type === 'module') {
22-
return `<link rel="modulepreload" href=${JSON.stringify(v.src)} />`
23-
} else if (v.async === true) {
24-
return `<link rel="preload" href=${JSON.stringify(v.src)} as="script" />`
30+
if (v.type === "module") {
31+
return `<link rel="modulepreload" href=${JSON.stringify(v.src)} />`;
2532
}
33+
return `<link rel="preload" href=${JSON.stringify(v.src)
34+
} as="script" />`;
2635
}
27-
return ''
28-
})).filter(Boolean)
29-
const scriptTags = scripts.map(v => {
36+
return "";
37+
}))
38+
/*.concat(
39+
[
40+
"/_aleph/-/deno.land/x/aleph/error.js",
41+
"/_aleph/-/deno.land/x/aleph/aleph.js",
42+
"/_aleph/-/deno.land/x/aleph/context.js",
43+
"/_aleph/-/deno.land/x/aleph/events.js"
44+
].map((v) =>
45+
`<link rel="modulepreload" href=${JSON.stringify(v)} as="script" />`
46+
),
47+
)*/
48+
.filter(Boolean);
49+
const scriptTags = scripts.map((v) => {
3050
if (util.isString(v)) {
31-
return `<script>${v}</script>`
51+
return `<script>${v}</script>`;
3252
} else if (util.isNEString(v.innerText)) {
33-
const { innerText, ...rest } = v
34-
return `<script${attrString(rest)}>${eol}${innerText}${eol}${indent}</script>`
53+
const { innerText, ...rest } = v;
54+
return `<script${attrString(rest)
55+
}>${eol}${innerText}${eol}${indent}</script>`;
3556
} else if (util.isNEString(v.src) && !v.preload) {
36-
return `<script${attrString(v)}></script>`
57+
return `<script${attrString(v)}></script>`;
3758
} else {
38-
return ''
59+
return "";
3960
}
40-
}).filter(Boolean)
61+
}).filter(Boolean);
4162

4263
return [
43-
'<!DOCTYPE html>',
64+
"<!DOCTYPE html>",
4465
`<html lang="${lang}">`,
45-
'<head>',
66+
"<head>",
4667
indent + '<meta charSet="utf-8" />',
47-
...headTags.map(tag => indent + tag),
48-
'</head>',
49-
'<body>',
68+
...headTags.map((tag) => indent + tag),
69+
"</head>",
70+
"<body>",
5071
indent + body,
51-
...scriptTags.map(tag => indent + tag),
52-
'</body>',
53-
'</html>'
54-
].join(eol)
72+
...scriptTags.map((tag) => indent + tag),
73+
"</body>",
74+
"</html>",
75+
].join(eol);
5576
}
5677

5778
function attrString(v: any): string {
58-
return Object.keys(v).map(k => {
79+
return Object.keys(v).map((k) => {
5980
if (v[k] === true) {
60-
return ` ${k}`
81+
return ` ${k}`;
6182
} else {
62-
return ` ${k}=${JSON.stringify(String(v[k]))}`
83+
return ` ${k}=${JSON.stringify(String(v[k]))}`;
6384
}
64-
}).join('')
85+
}).join("");
6586
}

project.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ export class Project {
260260
return [status, data]
261261
}
262262

263+
getPreloadScripts(baseUrl: string) {
264+
const scripts = [
265+
'-/deno.land/x/aleph/aleph.js',
266+
'-/deno.land/x/aleph/context.js',
267+
'-/deno.land/x/aleph/error.js',
268+
'-/deno.land/x/aleph/events.js',
269+
'-/deno.land/x/aleph/routing.js,',
270+
'-/deno.land/x/aleph/util.js'
271+
]
272+
return scripts.map(src => ({ src: `${baseUrl}${src}`, type: 'module', preload: true }))
273+
}
274+
263275
async getPageHtml(loc: { pathname: string, search?: string }): Promise<[number, string, Record<string, string> | null]> {
264276
if (!this.isSSRable(loc.pathname)) {
265277
const [url] = this.#routing.createRouter(loc)
@@ -275,7 +287,7 @@ export class Project {
275287
scripts: [
276288
data ? { type: 'application/json', innerText: JSON.stringify(data), id: 'ssr-data' } : '',
277289
{ src: util.cleanPath(`${baseUrl}/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' },
278-
{ src: util.cleanPath(`${baseUrl}/_aleph/-/deno.land/x/aleph/nomodule.js${this.isDev ? '?dev' : ''}`), nomodule: true },
290+
...this.getPreloadScripts(baseUrl),
279291
...scripts
280292
],
281293
body,
@@ -293,6 +305,7 @@ export class Project {
293305
scripts: [
294306
{ src: util.cleanPath(`${baseUrl}/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' },
295307
{ src: util.cleanPath(`${baseUrl}/_aleph/-/deno.land/x/aleph/nomodule.js${this.isDev ? '?dev' : ''}`), nomodule: true },
308+
...this.getPreloadScripts(baseUrl)
296309
],
297310
head: customLoading?.head || [],
298311
body: `<main>${customLoading?.body || ''}</main>`,
@@ -379,6 +392,7 @@ export class Project {
379392
data ? { type: 'application/json', innerText: JSON.stringify(data), id: 'ssr-data' } : '',
380393
{ src: util.cleanPath(`${baseUrl}/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' },
381394
{ src: util.cleanPath(`${baseUrl}/_aleph/-/deno.land/x/aleph/nomodule.js${this.isDev ? '?dev' : ''}`), nomodule: true },
395+
...this.getPreloadScripts(baseUrl),
382396
...scripts
383397
],
384398
body,

0 commit comments

Comments
 (0)