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

Commit 9af658d

Browse files
author
Je
committed
refactor: preimport renderer
1 parent 67f8b35 commit 9af658d

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

project.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ interface Module {
2727
hash: string
2828
}
2929

30+
interface Renderer {
31+
renderPage: Function
32+
renderHead: Function
33+
E501Page: Function
34+
}
35+
3036
interface RenderResult {
3137
url: RouterURL
3238
status: number
@@ -45,6 +51,7 @@ export class Project {
4551
#routing: Routing = new Routing()
4652
#apiRouting: Routing = new Routing()
4753
#fsWatchListeners: Array<EventEmitter> = []
54+
#renderer: Renderer = { renderPage: () => void 0, renderHead: () => void 0, E501Page: () => null }
4855

4956
constructor(dir: string, mode: 'development' | 'production') {
5057
this.mode = mode
@@ -376,9 +383,9 @@ export class Project {
376383

377384
const { deps, modules, styles } = moduleState
378385
log.info(colors.bold(' Modules'))
379-
log.info(' ', colors.bold(deps.count.toString()), 'deps', colors.dim(`• ${util.bytesString(deps.bytes)} (mini, uncompress)`))
380-
log.info(' ', colors.bold(modules.count.toString()), 'modules', colors.dim(`• ${util.bytesString(modules.bytes)} (mini, uncompress)`))
381-
log.info(' ', colors.bold(styles.count.toString()), 'styles', colors.dim(`• ${util.bytesString(styles.bytes)} (mini, uncompress)`))
386+
log.info(' {}', colors.bold(deps.count.toString()), 'deps', colors.dim(`• ${util.bytesString(deps.bytes)} (mini, uncompress)`))
387+
log.info(' {}', colors.bold(modules.count.toString()), 'modules', colors.dim(`• ${util.bytesString(modules.bytes)} (mini, uncompress)`))
388+
log.info(' {}', colors.bold(styles.count.toString()), 'styles', colors.dim(`• ${util.bytesString(styles.bytes)} (mini, uncompress)`))
382389

383390
log.info(`Done in ${Math.round(performance.now() - start)}ms`)
384391
}
@@ -530,6 +537,11 @@ export class Project {
530537
}
531538
await this._createMainModule()
532539

540+
// ensure react in deno is same with browser one
541+
const { renderPage, renderHead } = await import('file://' + this.#modules.get('//deno.land/x/aleph/renderer.js')!.jsFile)
542+
const { E501Page } = await import('file://' + this.#modules.get('//deno.land/x/aleph/error.js')!.jsFile)
543+
this.#renderer = { renderPage, renderHead, E501Page }
544+
533545
log.info(colors.bold('Aleph.js'))
534546
log.info(colors.bold(' Config'))
535547
if (this.#modules.has('/data.js')) {
@@ -1181,11 +1193,15 @@ export class Project {
11811193
try {
11821194
const appModule = this.#modules.get('/app.js')
11831195
const e404Module = this.#modules.get('/404.js')
1184-
const { E501Page } = await import('file://' + this.#modules.get('//deno.land/x/aleph/error.js')!.jsFile)
1185-
const { renderPage, renderHead } = await import('file://' + this.#modules.get('//deno.land/x/aleph/renderer.js')!.jsFile)
1186-
const { default: App } = appModule && url.pagePath != '' ? await import('file://' + appModule.jsFile) : {} as any
1187-
const { default: E404 } = e404Module ? await import('file://' + e404Module.jsFile) : {} as any
1188-
const staticData = await this.getStaticData()
1196+
const [
1197+
{ default: App }, // todo: cache, re-import when hash changed
1198+
{ default: E404 }, // todo: cache, re-import when hash changed
1199+
staticData // todo: real static
1200+
] = await Promise.all([
1201+
appModule && url.pagePath != '' ? await import('file://' + appModule.jsFile) : Promise.resolve({}),
1202+
e404Module ? await import('file://' + e404Module.jsFile) : Promise.resolve({}),
1203+
await this.getStaticData()
1204+
])
11891205
const pageComponentTree: { id: string, Component?: any }[] = pageModuleTree.map(({ id }) => ({ id }))
11901206
const imports = pageModuleTree.map(async ({ id }) => {
11911207
const mod = this.#modules.get(id)!
@@ -1195,13 +1211,13 @@ export class Project {
11951211
if (util.isLikelyReactComponent(C)) {
11961212
pc.Component = C
11971213
} else {
1198-
pc.Component = E501Page
1214+
pc.Component = this.#renderer.E501Page
11991215
}
12001216
}
12011217
})
12021218
await Promise.all(imports)
1203-
const html = renderPage(url, staticData, App, E404, pageComponentTree)
1204-
const head = await renderHead([
1219+
const html = this.#renderer.renderPage(url, staticData, App, E404, pageComponentTree)
1220+
const head = await this.#renderer.renderHead([
12051221
appModule ? this._lookupStyleDeps(appModule.id) : [],
12061222
...pageModuleTree.map(({ id }) => this._lookupStyleDeps(id))
12071223
].flat())

renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function renderPage(
1515
pageComponentTree: { id: string, Component?: any }[]
1616
) {
1717
const pageProps: PageProps = {
18-
Page: pageComponentTree.length > 0 ? pageComponentTree[0].Component || (() => null) : E404 || E404Page,
18+
Page: pageComponentTree.length > 0 ? (pageComponentTree[0].Component || (() => null)) : (E404 || E404Page),
1919
pageProps: {}
2020
}
2121
if (pageComponentTree.length > 1) {

0 commit comments

Comments
 (0)