@@ -138,32 +138,31 @@ export default (harness: PluginHarness): Vite.Plugin => {
138138 } )
139139
140140 // serve index.html with api token
141+ // Mounted on `base` so Connect applies the same matching as vitestUiAuth
142+ // and the sirv handler below. Connect strips `base`, so the UI root is `/`.
141143 // oxlint-disable-next-line prefer-arrow-callback
142- server . middlewares . use ( function vitestUiHtmlMiddleware ( req , res , next ) {
143- if ( req . url ) {
144- const url = new URL ( req . url , 'http://localhost' )
145- if ( url . pathname === base ) {
146- // vitestUiAuth already validated the request and set the cookie;
147- // redirect to strip the token from the URL
148- if ( isValidApiRequest ( ctx . config , req ) ) {
149- res . statusCode = 302
150- res . setHeader ( 'Location' , base )
151- res . end ( )
152- return
153- }
154- const html = clientIndexHtml . replace (
155- '<!-- !LOAD_METADATA! -->' ,
156- `<script>window.VITEST_API_TOKEN = ${ JSON . stringify ( ctx . config . api . token ) } ;window.VITEST_UI_THEME = ${ JSON . stringify ( ctx . config . uiOptions . theme ) } </script>` ,
157- )
158- res . setHeader ( 'Cache-Control' , 'no-cache, max-age=0, must-revalidate' )
159- res . setHeader ( 'Referrer-Policy' , 'no-referrer' )
160- res . setHeader ( 'Content-Type' , 'text/html; charset=utf-8' )
161- res . write ( html )
162- res . end ( )
163- return
164- }
144+ server . middlewares . use ( base , function vitestUiHtmlMiddleware ( req , res , next ) {
145+ const { pathname } = new URL ( req . url ?? '/' , 'http://localhost' )
146+ if ( pathname !== '/' ) {
147+ return next ( )
165148 }
166- next ( )
149+ // vitestUiAuth already validated the request and set the cookie;
150+ // redirect to strip the token from the URL
151+ if ( isValidApiRequest ( ctx . config , req ) ) {
152+ res . statusCode = 302
153+ res . setHeader ( 'Location' , base )
154+ res . end ( )
155+ return
156+ }
157+ const html = clientIndexHtml . replace (
158+ '<!-- !LOAD_METADATA! -->' ,
159+ `<script>window.VITEST_API_TOKEN = ${ JSON . stringify ( ctx . config . api . token ) } ;window.VITEST_UI_THEME = ${ JSON . stringify ( ctx . config . uiOptions . theme ) } </script>` ,
160+ )
161+ res . setHeader ( 'Cache-Control' , 'no-cache, max-age=0, must-revalidate' )
162+ res . setHeader ( 'Referrer-Policy' , 'no-referrer' )
163+ res . setHeader ( 'Content-Type' , 'text/html; charset=utf-8' )
164+ res . write ( html )
165+ res . end ( )
167166 } )
168167
169168 server . middlewares . use (
0 commit comments