@@ -90,6 +90,10 @@ export async function* serveWithVite(
90
90
serverOptions . servePath = browserOptions . baseHref ;
91
91
}
92
92
93
+ if ( serverOptions . servePath ) {
94
+ serverOptions . servePath = addLeadingSlash ( serverOptions . servePath ) ;
95
+ }
96
+
93
97
// The development server currently only supports a single locale when localizing.
94
98
// This matches the behavior of the Webpack-based development server but could be expanded in the future.
95
99
if (
@@ -583,15 +587,15 @@ export async function setupServer(
583
587
584
588
// Parse the incoming request.
585
589
// The base of the URL is unused but required to parse the URL.
586
- const pathname = pathnameWithoutServePath ( req . url , serverOptions ) ;
590
+ const pathname = pathnameWithoutServePath ( req . url , server . config . base ) ;
587
591
const extension = extname ( pathname ) ;
588
592
589
593
// Rewrite all build assets to a vite raw fs URL
590
594
const assetSourcePath = assets . get ( pathname ) ;
591
595
if ( assetSourcePath !== undefined ) {
592
596
// The encoding needs to match what happens in the vite static middleware.
593
597
// ref: https://github.com/vitejs/vite/blob/d4f13bd81468961c8c926438e815ab6b1c82735e/packages/vite/src/node/server/middlewares/static.ts#L163
594
- req . url = `/@fs/${ encodeURI ( assetSourcePath ) } ` ;
598
+ req . url = `${ server . config . base } /@fs/${ encodeURI ( assetSourcePath ) } ` ;
595
599
next ( ) ;
596
600
597
601
return ;
@@ -689,7 +693,7 @@ export async function setupServer(
689
693
690
694
// Parse the incoming request.
691
695
// The base of the URL is unused but required to parse the URL.
692
- const pathname = pathnameWithoutServePath ( req . url , serverOptions ) ;
696
+ const pathname = pathnameWithoutServePath ( req . url , server . config . base ) ;
693
697
694
698
if ( pathname === '/' || pathname === `/index.html` ) {
695
699
const rawHtml = outputFiles . get ( '/index.html' ) ?. contents ;
@@ -801,17 +805,15 @@ async function loadViteClientCode(file: string) {
801
805
return contents ;
802
806
}
803
807
804
- function pathnameWithoutServePath ( url : string , serverOptions : NormalizedDevServerOptions ) : string {
808
+ function pathnameWithoutServePath ( url : string , basePath : string ) : string {
805
809
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
- }
810
+ const pathname = decodeURIComponent ( parsedUrl . pathname ) ;
811
+
812
+ return basePath && pathname . startsWith ( basePath ) ? pathname . slice ( basePath . length ) : pathname ;
813
+ }
813
814
814
- return pathname ;
815
+ function addLeadingSlash ( value : string ) : string {
816
+ return value [ 0 ] === '/' ? value : '/' + value ;
815
817
}
816
818
817
819
type ViteEsBuildPlugin = NonNullable <
0 commit comments