@@ -439,6 +439,7 @@ export async function setupServer(
439
439
css : {
440
440
devSourcemap : true ,
441
441
} ,
442
+ // Vite will normalize the `base` option by adding a leading and trailing forward slash.
442
443
base : serverOptions . servePath ,
443
444
resolve : {
444
445
mainFields : [ 'es2020' , 'browser' , 'module' , 'main' ] ,
@@ -568,15 +569,15 @@ export async function setupServer(
568
569
569
570
// Parse the incoming request.
570
571
// The base of the URL is unused but required to parse the URL.
571
- const pathname = pathnameWithoutServePath ( req . url , serverOptions ) ;
572
+ const pathname = pathnameWithoutBasePath ( req . url , server . config . base ) ;
572
573
const extension = extname ( pathname ) ;
573
574
574
575
// Rewrite all build assets to a vite raw fs URL
575
576
const assetSourcePath = assets . get ( pathname ) ;
576
577
if ( assetSourcePath !== undefined ) {
577
578
// The encoding needs to match what happens in the vite static middleware.
578
579
// ref: https://github.com/vitejs/vite/blob/d4f13bd81468961c8c926438e815ab6b1c82735e/packages/vite/src/node/server/middlewares/static.ts#L163
579
- req . url = `/ @fs/${ encodeURI ( assetSourcePath ) } ` ;
580
+ req . url = `${ server . config . base } @fs/${ encodeURI ( assetSourcePath ) } ` ;
580
581
next ( ) ;
581
582
582
583
return ;
@@ -674,7 +675,7 @@ export async function setupServer(
674
675
675
676
// Parse the incoming request.
676
677
// The base of the URL is unused but required to parse the URL.
677
- const pathname = pathnameWithoutServePath ( req . url , serverOptions ) ;
678
+ const pathname = pathnameWithoutBasePath ( req . url , server . config . base ) ;
678
679
679
680
if ( pathname === '/' || pathname === `/index.html` ) {
680
681
const rawHtml = outputFiles . get ( '/index.html' ) ?. contents ;
@@ -785,17 +786,14 @@ async function loadViteClientCode(file: string) {
785
786
return contents ;
786
787
}
787
788
788
- function pathnameWithoutServePath ( url : string , serverOptions : NormalizedDevServerOptions ) : string {
789
+ function pathnameWithoutBasePath ( url : string , basePath : string ) : string {
789
790
const parsedUrl = new URL ( url , 'http://localhost' ) ;
790
- let pathname = decodeURIComponent ( parsedUrl . pathname ) ;
791
- if ( serverOptions . servePath && pathname . startsWith ( serverOptions . servePath ) ) {
792
- pathname = pathname . slice ( serverOptions . servePath . length ) ;
793
- if ( pathname [ 0 ] !== '/' ) {
794
- pathname = '/' + pathname ;
795
- }
796
- }
791
+ const pathname = decodeURIComponent ( parsedUrl . pathname ) ;
797
792
798
- return pathname ;
793
+ // slice(basePath.length - 1) to retain the trailing slash
794
+ return basePath !== '/' && pathname . startsWith ( basePath )
795
+ ? pathname . slice ( basePath . length - 1 )
796
+ : pathname ;
799
797
}
800
798
801
799
type ViteEsBuildPlugin = NonNullable <
0 commit comments