@@ -25,6 +25,7 @@ const DEV_MODE = config.isDevMode();
25
25
const DEFAULT_PREFIX_KEY = 'defaultPrefix' ;
26
26
const API_ENDPOINTS = config . getApiEndPoints ( ) ;
27
27
const isHMREnabled = config . isHMREnabled ( ) ;
28
+ const servingStaticIndex = config . isServingStaticIndex ( ) ;
28
29
29
30
function initAppCommon ( ) {
30
31
const app = new Koa ( ) ;
@@ -48,28 +49,42 @@ function initApp(app) {
48
49
app . use ( favicon ( path . join ( __dirname , 'build/app/favicon.ico' ) ) ) ;
49
50
50
51
// =====serve static=====
51
- let staticPrefix = path . join (
52
- config . getAppPrefix ( ) ,
53
- config . getStaticPrefix ( ) || '/'
54
- ) ;
52
+ const ROOT_PATH = '/' ;
53
+ let staticPrefixConfig = config . getStaticPrefix ( ) ;
54
+ let staticPrefix = path . join ( config . getAppPrefix ( ) , staticPrefixConfig ) ;
55
+ if ( ! staticPrefix . startsWith ( ROOT_PATH ) ) {
56
+ staticPrefix = ROOT_PATH ;
57
+ }
55
58
56
59
if ( sysUtils . isWindows ( ) ) {
57
60
staticPrefix = sysUtils . replaceBackwardSlash ( staticPrefix ) ;
58
61
}
59
- // app.use(mount(staticPrefix, conditional()));
60
- // app.use(mount(staticPrefix, etag()));
62
+
63
+ const staticOptions = {
64
+ // one month cache for prod
65
+ maxage : DEV_MODE ? 0 : 2592000000 ,
66
+ gzip : false ,
67
+ setHeaders ( res , path ) {
68
+ if ( path . endsWith ( '.html' ) ) {
69
+ res . setHeader ( 'Cache-Control' , 'no-cache' ) ;
70
+ }
71
+ } ,
72
+ } ;
73
+ const isStaticAssetsInRoot =
74
+ ! staticPrefixConfig || staticPrefixConfig == ROOT_PATH ;
75
+ if ( isStaticAssetsInRoot && ! servingStaticIndex ) {
76
+ //workaround: use a random index to pass through the static middleware
77
+ staticOptions . index = `${ Math . random ( ) . toString ( ) } .html` ;
78
+ }
79
+
61
80
app . use (
62
81
mount (
63
82
staticPrefix ,
64
- serveStatic ( path . join ( __dirname , 'build/app' ) , {
65
- // one month cache for prod
66
- maxage : DEV_MODE ? 0 : 2592000000 ,
67
- gzip : false ,
68
- } )
83
+ serveStatic ( path . join ( __dirname , 'build/app' ) , staticOptions )
69
84
)
70
85
) ;
71
86
// handle static not found, do not pass further down
72
- if ( staticPrefix && staticPrefix != '/' ) {
87
+ if ( ! isStaticAssetsInRoot ) {
73
88
app . use (
74
89
mount ( staticPrefix , ctx => {
75
90
ctx . status = 404 ;
0 commit comments