@@ -84,6 +84,7 @@ export interface Options {
8484}
8585
8686export interface Response {
87+ cache ?: boolean ;
8788 code ?: number ;
8889 content ?: string | Buffer ;
8990 filePath ?: string ;
@@ -207,12 +208,13 @@ export abstract class Server {
207208
208209 private onRequest = async ( request : http . IncomingMessage , response : http . ServerResponse ) : Promise < void > => {
209210 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 ) ;
211213 response . writeHead ( payload . redirect ? HttpCode . Redirect : payload . code || HttpCode . Ok , {
212- // "Cache-Control": "public, max-age=31536000",
213214 "Content-Type" : getMediaMime ( payload . filePath ) ,
214215 ...( payload . redirect ? { Location : this . withBase ( request , payload . redirect ) } : { } ) ,
215216 ...( request . headers [ "service-worker" ] ? { "Service-Worker-Allowed" : this . options . basePath || "/" } : { } ) ,
217+ ...( payload . cache ? { "Cache-Control" : "public, max-age=31536000" } : { } ) ,
216218 ...payload . headers ,
217219 } ) ;
218220 response . end ( payload . content ) ;
@@ -225,13 +227,12 @@ export abstract class Server {
225227 }
226228 }
227229
228- private async preHandleRequest ( request : http . IncomingMessage ) : Promise < Response > {
230+ private async preHandleRequest ( request : http . IncomingMessage , parsedUrl : url . UrlWithParsedQuery ) : Promise < Response > {
229231 const secure = ( request . connection as tls . TLSSocket ) . encrypted ;
230232 if ( this . options . cert && ! secure ) {
231233 return { redirect : request . url } ;
232234 }
233235
234- const parsedUrl = request . url ? url . parse ( request . url , true ) : { query : { } } ;
235236 const fullPath = decodeURIComponent ( parsedUrl . pathname || "/" ) ;
236237 const match = fullPath . match ( / ^ ( \/ ? [ ^ / ] * ) ( .* ) $ / ) ;
237238 let [ /* ignore */ , base , requestPath ] = match
@@ -250,19 +251,32 @@ export abstract class Server {
250251 this . ensureGet ( request ) ;
251252 }
252253
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+
253263 switch ( base ) {
254264 case "/" :
255265 switch ( requestPath ) {
256266 case "/favicon.ico" :
257267 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 ;
259271 }
260272 if ( ! this . authenticate ( request ) ) {
261273 return { redirect : "/login" } ;
262274 }
263275 break ;
264276 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 ;
266280 case "/login" :
267281 if ( ! this . options . auth || requestPath !== "/index.html" ) {
268282 throw new HttpError ( "Not found" , HttpCode . NotFound ) ;
@@ -514,10 +528,10 @@ export class MainServer extends Server {
514528 NLS_CONFIGURATION : await getNlsConfiguration ( locale , environment . userDataPath ) ,
515529 } ;
516530
531+ content = content . replace ( / \/ s t a t i c \/ / g, `/static${ product . commit ? `-${ product . commit } ` : "" } /` ) . replace ( "{{WEBVIEW_ENDPOINT}}" , "" ) ;
517532 for ( const key in options ) {
518533 content = content . replace ( `"{{${ key } }}"` , `'${ JSON . stringify ( options [ key as keyof Options ] ) } '` ) ;
519534 }
520- content = content . replace ( "{{WEBVIEW_ENDPOINT}}" , "" ) ;
521535
522536 return { content, filePath } ;
523537 }
0 commit comments