@@ -591,7 +591,12 @@ export async function setupServer(
591
591
if ( assetSourcePath !== undefined ) {
592
592
// The encoding needs to match what happens in the vite static middleware.
593
593
// ref: https://github.com/vitejs/vite/blob/d4f13bd81468961c8c926438e815ab6b1c82735e/packages/vite/src/node/server/middlewares/static.ts#L163
594
- req . url = `/@fs/${ encodeURI ( assetSourcePath ) } ` ;
594
+ let { servePath = '' } = serverOptions ;
595
+ if ( servePath ) {
596
+ servePath = addLeadingSlash ( servePath ) ;
597
+ }
598
+
599
+ req . url = `${ serverOptions . servePath } /@fs/${ encodeURI ( assetSourcePath ) } ` ;
595
600
next ( ) ;
596
601
597
602
return ;
@@ -801,19 +806,20 @@ async function loadViteClientCode(file: string) {
801
806
return contents ;
802
807
}
803
808
804
- function pathnameWithoutServePath ( url : string , serverOptions : NormalizedDevServerOptions ) : string {
809
+ function pathnameWithoutServePath ( url : string , { servePath } : NormalizedDevServerOptions ) : string {
805
810
const parsedUrl = new URL ( url , 'http://localhost' ) ;
806
811
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
+ if ( servePath && pathname . startsWith ( servePath ) ) {
813
+ pathname = addLeadingSlash ( pathname . slice ( servePath . length ) ) ;
812
814
}
813
815
814
816
return pathname ;
815
817
}
816
818
819
+ function addLeadingSlash ( value : string ) : string {
820
+ return value . charAt ( 0 ) === '/' ? value : '/' + value ;
821
+ }
822
+
817
823
type ViteEsBuildPlugin = NonNullable <
818
824
NonNullable < DepOptimizationConfig [ 'esbuildOptions' ] > [ 'plugins' ]
819
825
> [ 0 ] ;
0 commit comments