@@ -78,36 +78,40 @@ export function serveStaticMiddleware(
78
78
return next ( )
79
79
}
80
80
81
- const url = decodeURIComponent ( req . url ! )
81
+ const url = new URL ( req . url ! , 'http://example.com' )
82
+ const pathname = decodeURIComponent ( url . pathname )
82
83
83
84
// apply aliases to static requests as well
84
- let redirected : string | undefined
85
+ let redirectedPathname : string | undefined
85
86
for ( const { find, replacement } of server . config . resolve . alias ) {
86
87
const matches =
87
- typeof find === 'string' ? url . startsWith ( find ) : find . test ( url )
88
+ typeof find === 'string'
89
+ ? pathname . startsWith ( find )
90
+ : find . test ( pathname )
88
91
if ( matches ) {
89
- redirected = url . replace ( find , replacement )
92
+ redirectedPathname = pathname . replace ( find , replacement )
90
93
break
91
94
}
92
95
}
93
- if ( redirected ) {
96
+ if ( redirectedPathname ) {
94
97
// dir is pre-normalized to posix style
95
- if ( redirected . startsWith ( dir ) ) {
96
- redirected = redirected . slice ( dir . length )
98
+ if ( redirectedPathname . startsWith ( dir ) ) {
99
+ redirectedPathname = redirectedPathname . slice ( dir . length )
97
100
}
98
101
}
99
102
100
- const resolvedUrl = redirected || url
101
- let fileUrl = path . resolve ( dir , resolvedUrl . replace ( / ^ \/ / , '' ) )
102
- if ( resolvedUrl . endsWith ( '/' ) && ! fileUrl . endsWith ( '/' ) ) {
103
+ const resolvedPathname = redirectedPathname || pathname
104
+ let fileUrl = path . resolve ( dir , resolvedPathname . replace ( / ^ \/ / , '' ) )
105
+ if ( resolvedPathname . endsWith ( '/' ) && ! fileUrl . endsWith ( '/' ) ) {
103
106
fileUrl = fileUrl + '/'
104
107
}
105
108
if ( ! ensureServingAccess ( fileUrl , server , res , next ) ) {
106
109
return
107
110
}
108
111
109
- if ( redirected ) {
110
- req . url = encodeURIComponent ( redirected )
112
+ if ( redirectedPathname ) {
113
+ url . pathname = encodeURIComponent ( redirectedPathname )
114
+ req . url = url . href . slice ( url . origin . length )
111
115
}
112
116
113
117
serve ( req , res , next )
@@ -121,16 +125,17 @@ export function serveRawFsMiddleware(
121
125
122
126
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
123
127
return function viteServeRawFsMiddleware ( req , res , next ) {
124
- let url = decodeURIComponent ( req . url ! )
128
+ const url = new URL ( req . url ! , 'http://example.com' )
125
129
// In some cases (e.g. linked monorepos) files outside of root will
126
130
// reference assets that are also out of served root. In such cases
127
131
// the paths are rewritten to `/@fs/` prefixed paths and must be served by
128
132
// searching based from fs root.
129
- if ( url . startsWith ( FS_PREFIX ) ) {
133
+ if ( url . pathname . startsWith ( FS_PREFIX ) ) {
134
+ const pathname = decodeURIComponent ( url . pathname )
130
135
// restrict files outside of `fs.allow`
131
136
if (
132
137
! ensureServingAccess (
133
- slash ( path . resolve ( fsPathFromId ( url ) ) ) ,
138
+ slash ( path . resolve ( fsPathFromId ( pathname ) ) ) ,
134
139
server ,
135
140
res ,
136
141
next
@@ -139,10 +144,11 @@ export function serveRawFsMiddleware(
139
144
return
140
145
}
141
146
142
- url = url . slice ( FS_PREFIX . length )
143
- if ( isWindows ) url = url . replace ( / ^ [ A - Z ] : / i, '' )
147
+ let newPathname = pathname . slice ( FS_PREFIX . length )
148
+ if ( isWindows ) newPathname = newPathname . replace ( / ^ [ A - Z ] : / i, '' )
144
149
145
- req . url = encodeURIComponent ( url )
150
+ url . pathname = encodeURIComponent ( newPathname )
151
+ req . url = url . href . slice ( url . origin . length )
146
152
serveFromRoot ( req , res , next )
147
153
} else {
148
154
next ( )
0 commit comments