@@ -570,7 +570,12 @@ export async function setupServer(
570
570
if ( assetSourcePath !== undefined ) {
571
571
// The encoding needs to match what happens in the vite static middleware.
572
572
// ref: https://github.com/vitejs/vite/blob/d4f13bd81468961c8c926438e815ab6b1c82735e/packages/vite/src/node/server/middlewares/static.ts#L163
573
- req . url = `/@fs/${ encodeURI ( assetSourcePath ) } ` ;
573
+ let { servePath = '' } = serverOptions ;
574
+ if ( servePath ) {
575
+ servePath = addLeadingSlash ( servePath ) ;
576
+ }
577
+
578
+ req . url = `${ serverOptions . servePath } /@fs/${ encodeURI ( assetSourcePath ) } ` ;
574
579
next ( ) ;
575
580
576
581
return ;
@@ -778,19 +783,20 @@ async function loadViteClientCode(file: string) {
778
783
return contents ;
779
784
}
780
785
781
- function pathnameWithoutServePath ( url : string , serverOptions : NormalizedDevServerOptions ) : string {
786
+ function pathnameWithoutServePath ( url : string , { servePath } : NormalizedDevServerOptions ) : string {
782
787
const parsedUrl = new URL ( url , 'http://localhost' ) ;
783
788
let pathname = decodeURIComponent ( parsedUrl . pathname ) ;
784
- if ( serverOptions . servePath && pathname . startsWith ( serverOptions . servePath ) ) {
785
- pathname = pathname . slice ( serverOptions . servePath . length ) ;
786
- if ( pathname [ 0 ] !== '/' ) {
787
- pathname = '/' + pathname ;
788
- }
789
+ if ( servePath && pathname . startsWith ( servePath ) ) {
790
+ pathname = addLeadingSlash ( pathname . slice ( servePath . length ) ) ;
789
791
}
790
792
791
793
return pathname ;
792
794
}
793
795
796
+ function addLeadingSlash ( value : string ) : string {
797
+ return value . charAt ( 0 ) === '/' ? value : '/' + value ;
798
+ }
799
+
794
800
type ViteEsBuildPlugin = NonNullable <
795
801
NonNullable < DepOptimizationConfig [ 'esbuildOptions' ] > [ 'plugins' ]
796
802
> [ 0 ] ;
0 commit comments