@@ -84,6 +84,7 @@ export interface Options {
84
84
}
85
85
86
86
export interface Response {
87
+ cache ?: boolean ;
87
88
code ?: number ;
88
89
content ?: string | Buffer ;
89
90
filePath ?: string ;
@@ -207,12 +208,13 @@ export abstract class Server {
207
208
208
209
private onRequest = async ( request : http . IncomingMessage , response : http . ServerResponse ) : Promise < void > => {
209
210
try {
210
- const payload = await this . preHandleRequest ( request ) ;
211
+ const parsedUrl = request . url ? url . parse ( request . url , true ) : { query : { } } ;
212
+ const payload = await this . preHandleRequest ( request , parsedUrl ) ;
211
213
response . writeHead ( payload . redirect ? HttpCode . Redirect : payload . code || HttpCode . Ok , {
212
- // "Cache-Control": "public, max-age=31536000",
213
214
"Content-Type" : getMediaMime ( payload . filePath ) ,
214
215
...( payload . redirect ? { Location : this . withBase ( request , payload . redirect ) } : { } ) ,
215
216
...( request . headers [ "service-worker" ] ? { "Service-Worker-Allowed" : this . options . basePath || "/" } : { } ) ,
217
+ ...( payload . cache ? { "Cache-Control" : "public, max-age=31536000" } : { } ) ,
216
218
...payload . headers ,
217
219
} ) ;
218
220
response . end ( payload . content ) ;
@@ -225,13 +227,12 @@ export abstract class Server {
225
227
}
226
228
}
227
229
228
- private async preHandleRequest ( request : http . IncomingMessage ) : Promise < Response > {
230
+ private async preHandleRequest ( request : http . IncomingMessage , parsedUrl : url . UrlWithParsedQuery ) : Promise < Response > {
229
231
const secure = ( request . connection as tls . TLSSocket ) . encrypted ;
230
232
if ( this . options . cert && ! secure ) {
231
233
return { redirect : request . url } ;
232
234
}
233
235
234
- const parsedUrl = request . url ? url . parse ( request . url , true ) : { query : { } } ;
235
236
const fullPath = decodeURIComponent ( parsedUrl . pathname || "/" ) ;
236
237
const match = fullPath . match ( / ^ ( \/ ? [ ^ / ] * ) ( .* ) $ / ) ;
237
238
let [ /* ignore */ , base , requestPath ] = match
@@ -250,19 +251,32 @@ export abstract class Server {
250
251
this . ensureGet ( request ) ;
251
252
}
252
253
254
+ // Allow for a versioned static endpoint. This lets us cache every static
255
+ // resource underneath the path based on the version without any work and
256
+ // without adding query parameters which have their own issues.
257
+ // REVIEW: Discuss whether this is the best option; this is sort of a quick
258
+ // hack almost to get caching in the meantime but it does work pretty well.
259
+ if ( / s t a t i c - .+ / . test ( base ) ) {
260
+ base = "/static" ;
261
+ }
262
+
253
263
switch ( base ) {
254
264
case "/" :
255
265
switch ( requestPath ) {
256
266
case "/favicon.ico" :
257
267
case "/manifest.json" :
258
- return this . getResource ( this . serverRoot , "media" , requestPath ) ;
268
+ const response = await this . getResource ( this . serverRoot , "media" , requestPath ) ;
269
+ response . cache = true ;
270
+ return response ;
259
271
}
260
272
if ( ! this . authenticate ( request ) ) {
261
273
return { redirect : "/login" } ;
262
274
}
263
275
break ;
264
276
case "/static" :
265
- return this . getResource ( this . rootPath , requestPath ) ;
277
+ const response = await this . getResource ( this . rootPath , requestPath ) ;
278
+ response . cache = true ;
279
+ return response ;
266
280
case "/login" :
267
281
if ( ! this . options . auth || requestPath !== "/index.html" ) {
268
282
throw new HttpError ( "Not found" , HttpCode . NotFound ) ;
@@ -514,10 +528,10 @@ export class MainServer extends Server {
514
528
NLS_CONFIGURATION : await getNlsConfiguration ( locale , environment . userDataPath ) ,
515
529
} ;
516
530
531
+ content = content . replace ( / \/ s t a t i c \/ / g, `/static${ product . commit ? `-${ product . commit } ` : "" } /` ) . replace ( "{{WEBVIEW_ENDPOINT}}" , "" ) ;
517
532
for ( const key in options ) {
518
533
content = content . replace ( `"{{${ key } }}"` , `'${ JSON . stringify ( options [ key as keyof Options ] ) } '` ) ;
519
534
}
520
- content = content . replace ( "{{WEBVIEW_ENDPOINT}}" , "" ) ;
521
535
522
536
return { content, filePath } ;
523
537
}
0 commit comments