@@ -103,38 +103,36 @@ export class Server {
103
103
}
104
104
return
105
105
} 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 ( '' )
134
112
return
135
113
}
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
136
133
}
137
134
}
135
+
138
136
req . status ( 404 ) . send ( 'file not found' )
139
137
return
140
138
}
@@ -155,6 +153,7 @@ export class Server {
155
153
/** start a standard aleph server. */
156
154
export async function serve ( hostname : string , port : number , app : Appliaction ) {
157
155
const server = new Server ( app )
156
+ await app . ready
158
157
while ( true ) {
159
158
try {
160
159
const s = stdServe ( { hostname, port } )
0 commit comments