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

Commit 3dd1b78

Browse files
committed
refactor: add preload scripts for dev mode
1 parent 5109391 commit 3dd1b78

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

server/app.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,12 +560,40 @@ export class Application implements ServerApplication {
560560

561561
/** get ssr html scripts */
562562
getSSRHTMLScripts(pagePath?: string) {
563-
const { baseUrl } = this.config
563+
const { framework } = this.config
564+
const baseUrl = util.trimSuffix(this.config.baseUrl, '/')
565+
const alephPkgPath = getAlephPkgUri().replace('https://', '').replace('http://localhost:', 'http_localhost_')
566+
const fullAlephPkgPath = `${baseUrl}/_aleph/-/${alephPkgPath}`
564567

565568
if (this.isDev) {
569+
const preload: string[] = [
570+
`${fullAlephPkgPath}/framework/core/module.js`,
571+
`${fullAlephPkgPath}/framework/core/events.js`,
572+
`${fullAlephPkgPath}/framework/core/routing.js`,
573+
`${fullAlephPkgPath}/framework/core/hmr.js`,
574+
`${fullAlephPkgPath}/framework/${framework}/bootstrap.js`,
575+
`${fullAlephPkgPath}/shared/util.js`,
576+
]
577+
578+
Array.from(this.#modules.keys()).forEach(url => {
579+
switch (trimModuleExt(url)) {
580+
case '/app':
581+
preload.push(`${baseUrl}/_aleph/app.js`)
582+
break
583+
case '/404':
584+
preload.push(`${baseUrl}/_aleph/404.js`)
585+
break
586+
}
587+
})
588+
589+
if (pagePath) {
590+
preload.push(`${baseUrl}/_aleph/pages/${pagePath.replace(/\/$/, '/index')}.js`)
591+
}
592+
566593
return [
567-
{ src: util.cleanPath(`${baseUrl}/_aleph/main.js`), type: 'module' },
568-
{ src: util.cleanPath(`${baseUrl}/_aleph/-/deno.land/x/aleph/nomodule.js`), nomodule: true },
594+
...preload.map(src => ({ src, type: 'module', preload: true })),
595+
{ src: `${baseUrl}/_aleph/main.js`, type: 'module' },
596+
{ src: `${fullAlephPkgPath}/nomodule.js`, nomodule: true },
569597
]
570598
}
571599

@@ -574,7 +602,7 @@ export class Application implements ServerApplication {
574602
...['polyfill', 'deps', 'shared', 'main', pagePath ? '/pages' + pagePath.replace(/\/$/, '/index') : '']
575603
.filter(name => name !== "" && this.#bundler.getBundledFile(name) !== null)
576604
.map(name => ({
577-
src: util.cleanPath(`${baseUrl}/_aleph/${this.#bundler.getBundledFile(name)}`)
605+
src: `${baseUrl}/_aleph/${this.#bundler.getBundledFile(name)}`
578606
}))
579607
]
580608
}

server/ssr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function createHtml({
175175
lang?: string,
176176
head?: string[],
177177
className?: string,
178-
scripts?: (string | { id?: string, type?: string, src?: string, innerText?: string, nomodule?: boolean, async?: boolean })[],
178+
scripts?: (string | { id?: string, type?: string, src?: string, innerText?: string, async?: boolean, preload?: boolean, nomodule?: boolean })[],
179179
minify?: boolean
180180
}) {
181181
const eol = minify ? '' : '\n'
@@ -196,7 +196,7 @@ function createHtml({
196196
} else if (util.isNEString(v.innerText)) {
197197
const { innerText, ...rest } = v
198198
return `<script${formatAttrs(rest)}>${eol}${innerText}${eol}${indent}</script>`
199-
} else if (util.isNEString(v.src)) {
199+
} else if (util.isNEString(v.src) && !v.preload) {
200200
return `<script${formatAttrs({ ...v, src: util.cleanPath(v.src) })}></script>`
201201
} else {
202202
return ''

0 commit comments

Comments
 (0)