@@ -448,6 +448,7 @@ export async function setupServer(
448
448
css : {
449
449
devSourcemap : true ,
450
450
} ,
451
+ // Vite will normalize the `base` option by adding a leading and trailing forward slash.
451
452
base : serverOptions . servePath ,
452
453
resolve : {
453
454
mainFields : [ 'es2020' , 'browser' , 'module' , 'main' ] ,
@@ -583,15 +584,16 @@ export async function setupServer(
583
584
584
585
// Parse the incoming request.
585
586
// The base of the URL is unused but required to parse the URL.
586
- const pathname = pathnameWithoutServePath ( req . url , serverOptions ) ;
587
+ const pathname = pathnameWithoutBasePath ( req . url , server . config . base ) ;
587
588
const extension = extname ( pathname ) ;
588
589
589
590
// Rewrite all build assets to a vite raw fs URL
590
591
const assetSourcePath = assets . get ( pathname ) ;
592
+ console . warn ( { s : server . config . base } ) ;
591
593
if ( assetSourcePath !== undefined ) {
592
594
// The encoding needs to match what happens in the vite static middleware.
593
595
// ref: https://github.com/vitejs/vite/blob/d4f13bd81468961c8c926438e815ab6b1c82735e/packages/vite/src/node/server/middlewares/static.ts#L163
594
- req . url = `/ @fs/${ encodeURI ( assetSourcePath ) } ` ;
596
+ req . url = `${ server . config . base } @fs/${ encodeURI ( assetSourcePath ) } ` ;
595
597
next ( ) ;
596
598
597
599
return ;
@@ -689,7 +691,7 @@ export async function setupServer(
689
691
690
692
// Parse the incoming request.
691
693
// The base of the URL is unused but required to parse the URL.
692
- const pathname = pathnameWithoutServePath ( req . url , serverOptions ) ;
694
+ const pathname = pathnameWithoutBasePath ( req . url , server . config . base ) ;
693
695
694
696
if ( pathname === '/' || pathname === `/index.html` ) {
695
697
const rawHtml = outputFiles . get ( '/index.html' ) ?. contents ;
@@ -801,17 +803,12 @@ async function loadViteClientCode(file: string) {
801
803
return contents ;
802
804
}
803
805
804
- function pathnameWithoutServePath ( url : string , serverOptions : NormalizedDevServerOptions ) : string {
806
+ function pathnameWithoutBasePath ( url : string , basePath : string ) : string {
805
807
const parsedUrl = new URL ( url , 'http://localhost' ) ;
806
- let pathname = decodeURIComponent ( parsedUrl . pathname ) ;
807
- if ( serverOptions . servePath && pathname . startsWith ( serverOptions . servePath ) ) {
808
- pathname = pathname . slice ( serverOptions . servePath . length ) ;
809
- if ( pathname [ 0 ] !== '/' ) {
810
- pathname = '/' + pathname ;
811
- }
812
- }
808
+ const pathname = decodeURIComponent ( parsedUrl . pathname ) ;
813
809
814
- return pathname ;
810
+ // slice(basePath.length - 1) to retain the trailing slash
811
+ return basePath && pathname . startsWith ( basePath ) ? pathname . slice ( basePath . length - 1 ) : pathname ;
815
812
}
816
813
817
814
type ViteEsBuildPlugin = NonNullable <
0 commit comments