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

Commit 9952e18

Browse files
committed
refactor(server): improve dist serve
1 parent cbc0dec commit 9952e18

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

server/server.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,38 +103,36 @@ export class Server {
103103
}
104104
return
105105
} else {
106-
const reqMap = pathname.endsWith('.js.map')
107-
const fixedPath = util.trimPrefix(reqMap ? pathname.slice(0, -4) : pathname, '/_aleph/')
108-
const metaFile = path.join(app.buildDir, util.trimSuffix(fixedPath.replace(reHashJs, ''), '.js') + '.meta.json')
109-
if (existsFileSync(metaFile)) {
110-
const { url } = JSON.parse(await Deno.readTextFile(metaFile))
111-
const mod = app.getModule(url)
112-
if (mod) {
113-
const etag = req.headers.get('If-None-Match')
114-
if (etag && etag === mod.hash) {
115-
req.status(304).send('')
116-
return
117-
}
118-
let body = ''
119-
if (reqMap) {
120-
if (existsFileSync(mod.jsFile + '.map')) {
121-
body = await Deno.readTextFile(mod.jsFile + '.map')
122-
} else {
123-
req.status(404).send('file not found')
124-
return
125-
}
126-
} else {
127-
body = await Deno.readTextFile(mod.jsFile)
128-
if (app.isHMRable(mod.url)) {
129-
body = app.injectHMRCode(mod, body)
130-
}
131-
}
132-
req.setHeader('ETag', mod.hash)
133-
req.send(body, `application/${reqMap ? 'json' : 'javascript'}; charset=utf-8`)
106+
const filePath = path.join(app.buildDir, util.trimPrefix(pathname, '/_aleph/'))
107+
if (existsFileSync(filePath)) {
108+
const info = Deno.lstatSync(filePath)
109+
const lastModified = info.mtime?.toUTCString() ?? new Date().toUTCString()
110+
if (lastModified === r.headers.get('If-Modified-Since')) {
111+
req.status(304).send('')
134112
return
135113
}
114+
115+
let content = await Deno.readTextFile(filePath)
116+
117+
if (reHashJs.test(filePath)) {
118+
const metaFile = filePath.replace(reHashJs, '') + '.meta.json'
119+
if (existsFileSync(metaFile)) {
120+
try {
121+
const { url } = JSON.parse(await Deno.readTextFile(metaFile))
122+
const mod = app.getModule(url)
123+
if (mod && app.isHMRable(mod.url)) {
124+
content = app.injectHMRCode(mod, content)
125+
}
126+
} catch (e) { }
127+
}
128+
}
129+
130+
req.setHeader('Last-Modified', lastModified)
131+
req.send(content, getContentType(filePath))
132+
return
136133
}
137134
}
135+
138136
req.status(404).send('file not found')
139137
return
140138
}
@@ -155,6 +153,7 @@ export class Server {
155153
/** start a standard aleph server. */
156154
export async function serve(hostname: string, port: number, app: Appliaction) {
157155
const server = new Server(app)
156+
await app.ready
158157
while (true) {
159158
try {
160159
const s = stdServe({ hostname, port })

0 commit comments

Comments
 (0)