@@ -17,6 +17,7 @@ import { AppModule } from './app.module'
17
17
import { ConfigService } from './core/config/config.service'
18
18
import { getStartupConfig } from './core/config/config.startup'
19
19
import { Logger } from './core/logger/logger.service'
20
+ import { SpaHtmlService } from './core/spa/spa-html.service'
20
21
import { SpaFilter } from './core/spa/spa.filter'
21
22
22
23
import './self-check'
@@ -88,8 +89,21 @@ async function bootstrap(): Promise<NestFastifyApplication> {
88
89
const configService : ConfigService = app . get ( ConfigService )
89
90
const logger : Logger = app . get ( Logger )
90
91
91
- // Serve index.html without a cache
92
- app . getHttpAdapter ( ) . get ( '/' , async ( req : FastifyRequest , res : FastifyReply ) => {
92
+ // (5) Sort out the webroot - update index.html and set env var for spa filter
93
+ let realWebroot = startupConfig . webroot || ''
94
+ try {
95
+ await SpaHtmlService . updateIndexHtml ( startupConfig . webroot )
96
+ process . env . UIX_ORIGINAL_WEBROOT = startupConfig . webroot
97
+ configService . setOriginalWebroot ( startupConfig . webroot )
98
+ } catch ( error ) {
99
+ logger . warn ( `Could not update index.html with webroot ${ startupConfig . webroot } : ${ error . message } ` )
100
+ realWebroot = ''
101
+ process . env . UIX_ORIGINAL_WEBROOT = globalThis . webroot . errorCode
102
+ configService . setOriginalWebroot ( globalThis . webroot . errorCode )
103
+ }
104
+
105
+ // (6) Serve index.html without a cache
106
+ app . getHttpAdapter ( ) . get ( realWebroot || '/' , async ( req : FastifyRequest , res : FastifyReply ) => {
93
107
res . type ( 'text/html' )
94
108
res . header ( 'Cache-Control' , 'no-cache, no-store, must-revalidate' )
95
109
res . header ( 'Pragma' , 'no-cache' )
@@ -103,10 +117,11 @@ async function bootstrap(): Promise<NestFastifyApplication> {
103
117
setHeaders ( res ) {
104
118
res . setHeader ( 'Cache-Control' , 'public,max-age=31536000,immutable' )
105
119
} ,
120
+ ...realWebroot ? { prefix : realWebroot } : { } ,
106
121
} )
107
122
108
- // Set prefix
109
- app . setGlobalPrefix ( ' /api' )
123
+ // (8) Set api prefix (including webroot)
124
+ app . setGlobalPrefix ( ` ${ realWebroot || '' } /api` )
110
125
111
126
// (9) Set up cors
112
127
app . enableCors ( {
@@ -136,7 +151,7 @@ async function bootstrap(): Promise<NestFastifyApplication> {
136
151
} )
137
152
. build ( )
138
153
const document = SwaggerModule . createDocument ( app , options )
139
- SwaggerModule . setup ( ' swagger' , app , document )
154
+ SwaggerModule . setup ( ` ${ realWebroot } / swagger` . replace ( / ^ \/ / , '' ) , app , document )
140
155
141
156
// (12) Use the spa filter to serve index.html for any non-api routes
142
157
app . useGlobalFilters ( new SpaFilter ( ) )
0 commit comments