@@ -2,6 +2,7 @@ import path from 'path'
22import url from 'url'
33import fs from 'fs'
44import express from 'express'
5+ import send from 'send'
56import { EventEmitter } from 'node:events'
67
78import lvimport from 'live-elements-core/lvimport.mjs'
@@ -60,6 +61,7 @@ class WebServerConfiguration{
6061 this . _scripstUrl = '/scripts'
6162 this . _stylesUrl = '/styles'
6263 this . _entryScriptName = 'main.bundle.js'
64+ this . _staticFileHeaders = config . staticFileHeaders ? config . staticFileHeaders : { etag : true , lastModified : true , maxAge : 86400 * 1000 }
6365 }
6466
6567 get runMode ( ) { return this . _runMode }
@@ -82,6 +84,8 @@ class WebServerConfiguration{
8284 return `ws://${ host } :${ this . port } `
8385 }
8486
87+ get staticFileHeaders ( ) { return this . _staticFileHeaders }
88+
8589 static renderModeFromString ( mode ) {
8690 if ( mode === 'development' )
8791 return WebServer . RenderMode . Development
@@ -235,6 +239,16 @@ export default class WebServer extends EventEmitter{
235239 return webServer
236240 }
237241
242+ sendDynamicFile ( res , type , content ) {
243+ const seconds = Math . floor ( maxAgeMs / 1000 )
244+ res . setHeader ( 'Cache-Control' , `public, max-age=${ seconds } ` )
245+ res . type ( type ) . send ( content )
246+ }
247+
248+ sendFile ( req , res , file ) {
249+ return send ( req , file , this . config . staticFileHeaders ) . pipe ( res )
250+ }
251+
238252 viewLoaderData ( view , placement ) {
239253 const placements = placement ? placement . map ( p => {
240254 const placementPath = ComponentRegistry . findComponentPath ( p , this . bundleLookupPath )
@@ -699,9 +713,16 @@ export default class WebServer extends EventEmitter{
699713 async run ( ) {
700714 const distPath = this . establishedDistPath ( )
701715
702- this . _app . use ( '/scripts' , express . static ( path . join ( distPath , 'scripts' ) ) )
703- this . _app . use ( '/styles' , express . static ( path . join ( distPath , 'styles' ) ) )
704- this . _app . use ( '/' , express . static ( path . join ( distPath , 'assets' ) , { index : false } ) )
716+ this . _app . use ( '/scripts' , express . static ( path . join ( distPath , 'scripts' ) , {
717+ maxAge : this . config . staticFileHeaders . maxAge
718+ } ) )
719+ this . _app . use ( '/styles' , express . static ( path . join ( distPath , 'styles' ) , {
720+ maxAge : this . config . staticFileHeaders . maxAge
721+ } ) )
722+ this . _app . use ( '/' , express . static ( path . join ( distPath , 'assets' ) , {
723+ maxAge : this . config . staticFileHeaders . maxAge ,
724+ index : false
725+ } ) )
705726
706727 /// Handle routes
707728
0 commit comments