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

Commit c6b916f

Browse files
committed
refactor(server): clean up
1 parent 84b6229 commit c6b916f

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

server/app.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ export class Application implements ServerApplication {
128128
const { init } = await import(`../framework/${framework}/init.ts`)
129129
await init(this)
130130

131-
if (!this.isDev) {
132-
log.info('Building...')
133-
}
131+
log.info('Building...')
134132

135133
// pre-compile framework modules
136134
await this.compile(`${alephPkgUri}/framework/${framework}/bootstrap.ts`)
@@ -150,8 +148,8 @@ export class Application implements ServerApplication {
150148
// compile custom components
151149
for (const name of ['app', '404', 'loading']) {
152150
for (const ext of moduleExts) {
153-
if (existsFileSync(path.join(this.srcDir, name + '.' + ext))) {
154-
await this.compile('/' + name + '.' + ext)
151+
if (existsFileSync(path.join(this.srcDir, `${name}.${ext}`))) {
152+
await this.compile(`/${name}.${ext}`)
155153
break
156154
}
157155
}
@@ -338,14 +336,18 @@ export class Application implements ServerApplication {
338336
if (options.pathname) {
339337
Object.assign(routeMod, { url: util.cleanPath('/api/' + options.pathname) + '.tsx' })
340338
} else if (!routeMod.url.startsWith('/api/')) {
341-
throw new Error(`the api module url should start with '/api/': ${routeMod.url}`)
339+
log.warn(`the api module url should start with '/api/': ${routeMod.url}`)
340+
this.#modules.delete(mod.url)
341+
return
342342
}
343343
this.#apiRouting.update(routeMod)
344344
} else if (options.asPage) {
345345
if (options.pathname) {
346346
Object.assign(routeMod, { url: util.cleanPath('/pages/' + options.pathname) + '.tsx' })
347347
} else if (!routeMod.url.startsWith('/pages/')) {
348-
throw new Error(`the page module url should start with '/pages/': ${routeMod.url}`)
348+
log.warn(`the page module url should start with '/pages/': ${routeMod.url}`)
349+
this.#modules.delete(mod.url)
350+
return
349351
}
350352
this.#pageRouting.update(routeMod)
351353
}
@@ -466,9 +468,11 @@ export class Application implements ServerApplication {
466468
}
467469
for (const ext of moduleExts) {
468470
if (url.endsWith('.' + ext)) {
469-
return url.startsWith('/pages/') ||
471+
return (
472+
url.startsWith('/pages/') ||
470473
url.startsWith('/components/') ||
471-
['/app', '/404'].includes(trimModuleExt(url))
474+
['/app', '/404'].includes(util.trimSuffix(url, '.' + ext))
475+
)
472476
}
473477
}
474478
return this.config.plugins.some(p => p.type === 'loader' && p.test.test(url) && (p.allowPage || p.acceptHMR))

server/server.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,38 +89,39 @@ export class Server {
8989
req.status(status).send('')
9090
}
9191
return
92-
} else if (reHashJs.test(pathname) && ['main', 'main.bundle'].includes(util.trimPrefix(pathname, '/_aleph/').replace(reHashJs, ''))) {
93-
req.send(app.getMainJS(pathname.startsWith('/_aleph/main.bundle')), 'application/javascript; charset=utf-8')
92+
}
93+
94+
if (pathname.startsWith('/_aleph/main') && ['main', 'main.bundle'].includes(util.trimPrefix(pathname, '/_aleph/').replace(reHashJs, ''))) {
95+
req.send(app.getMainJS(pathname.includes('.bundle.')), 'application/javascript; charset=utf-8')
9496
return
95-
} else {
96-
const filePath = path.join(app.buildDir, util.trimPrefix(pathname, '/_aleph/'))
97-
if (existsFileSync(filePath)) {
98-
const info = Deno.lstatSync(filePath)
99-
const lastModified = info.mtime?.toUTCString() ?? new Date().toUTCString()
100-
if (lastModified === r.headers.get('If-Modified-Since')) {
101-
req.status(304).send('')
102-
return
103-
}
97+
}
10498

105-
let content = await Deno.readTextFile(filePath)
99+
const filePath = path.join(app.buildDir, util.trimPrefix(pathname, '/_aleph/'))
100+
if (existsFileSync(filePath)) {
101+
const info = Deno.lstatSync(filePath)
102+
const lastModified = info.mtime?.toUTCString() ?? new Date().toUTCString()
103+
if (lastModified === r.headers.get('If-Modified-Since')) {
104+
req.status(304).send('')
105+
return
106+
}
106107

107-
if (reHashJs.test(filePath)) {
108-
const metaFile = filePath.replace(reHashJs, '') + '.meta.json'
109-
if (existsFileSync(metaFile)) {
110-
try {
111-
const { url } = JSON.parse(await Deno.readTextFile(metaFile))
112-
const mod = app.getModule(url)
113-
if (mod && app.isHMRable(mod.url)) {
114-
content = app.injectHMRCode(mod, content)
115-
}
116-
} catch (e) { }
117-
}
108+
let content = await Deno.readTextFile(filePath)
109+
if (reHashJs.test(filePath)) {
110+
const metaFile = filePath.replace(reHashJs, '') + '.meta.json'
111+
if (existsFileSync(metaFile)) {
112+
try {
113+
const { url } = JSON.parse(await Deno.readTextFile(metaFile))
114+
const mod = app.getModule(url)
115+
if (mod && app.isHMRable(mod.url)) {
116+
content = app.injectHMRCode(mod, content)
117+
}
118+
} catch (e) { }
118119
}
119-
120-
req.setHeader('Last-Modified', lastModified)
121-
req.send(content, getContentType(filePath))
122-
return
123120
}
121+
122+
req.setHeader('Last-Modified', lastModified)
123+
req.send(content, getContentType(filePath))
124+
return
124125
}
125126

126127
req.status(404).send('file not found')
@@ -206,7 +207,7 @@ export async function serve({ app, port, hostname, certFile, keyFile }: ServeOpt
206207
} else {
207208
s = stdServe({ port, hostname })
208209
}
209-
log.info(`Aleph server ready on http://${hostname || 'localhost'}:${port}${app.config.baseUrl}`)
210+
log.info(`Server ready on http://${hostname || 'localhost'}:${port}${app.config.baseUrl}`)
210211
for await (const r of s) {
211212
server.handle(r)
212213
}

0 commit comments

Comments
 (0)