Skip to content

Commit 61273b2

Browse files
authored
fix: injectQuery break relative path (#9760)
1 parent a8279af commit 61273b2

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

packages/vite/src/node/__tests__/utils.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@ describe('injectQuery', () => {
1919
})
2020
}
2121

22+
test('relative path', () => {
23+
expect(injectQuery('usr/vite/%20a%20', 'direct')).toEqual(
24+
'usr/vite/%20a%20?direct'
25+
)
26+
expect(injectQuery('./usr/vite/%20a%20', 'direct')).toEqual(
27+
'./usr/vite/%20a%20?direct'
28+
)
29+
expect(injectQuery('../usr/vite/%20a%20', 'direct')).toEqual(
30+
'../usr/vite/%20a%20?direct'
31+
)
32+
})
33+
34+
test('path with hash', () => {
35+
expect(injectQuery('/usr/vite/path with space/#1?2/', 'direct')).toEqual(
36+
'/usr/vite/path with space/?direct#1?2/'
37+
)
38+
})
39+
40+
test('path with protocol', () => {
41+
expect(injectQuery('file:///usr/vite/%20a%20', 'direct')).toMatch(
42+
'file:///usr/vite/%20a%20?direct'
43+
)
44+
expect(injectQuery('http://usr.vite/%20a%20', 'direct')).toMatch(
45+
'http://usr.vite/%20a%20?direct'
46+
)
47+
})
48+
2249
test('path with multiple spaces', () => {
2350
expect(injectQuery('/usr/vite/path with space', 'direct')).toEqual(
2451
'/usr/vite/path with space?direct'

packages/vite/src/node/utils.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,9 @@ export function injectQuery(url: string, queryToInject: string): string {
311311
if (resolvedUrl.protocol !== 'relative:') {
312312
resolvedUrl = pathToFileURL(url)
313313
}
314-
let { protocol, pathname, search, hash } = resolvedUrl
315-
if (protocol === 'file:') {
316-
pathname = pathname.slice(1)
317-
}
318-
pathname = decodeURIComponent(pathname)
314+
const { search, hash } = resolvedUrl
315+
let pathname = cleanUrl(url)
316+
pathname = isWindows ? slash(pathname) : pathname
319317
return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
320318
hash ?? ''
321319
}`

0 commit comments

Comments
 (0)