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