diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 51990edf709da2..858ec8437362a1 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -19,6 +19,33 @@ describe('injectQuery', () => { }) } + test('relative path', () => { + expect(injectQuery('usr/vite/%20a%20', 'direct')).toEqual( + 'usr/vite/%20a%20?direct' + ) + expect(injectQuery('./usr/vite/%20a%20', 'direct')).toEqual( + './usr/vite/%20a%20?direct' + ) + expect(injectQuery('../usr/vite/%20a%20', 'direct')).toEqual( + '../usr/vite/%20a%20?direct' + ) + }) + + test('path with hash', () => { + expect(injectQuery('/usr/vite/path with space/#1?2/', 'direct')).toEqual( + '/usr/vite/path with space/?direct#1?2/' + ) + }) + + test('path with protocol', () => { + expect(injectQuery('file:///usr/vite/%20a%20', 'direct')).toMatch( + 'file:///usr/vite/%20a%20?direct' + ) + expect(injectQuery('http://usr.vite/%20a%20', 'direct')).toMatch( + 'http://usr.vite/%20a%20?direct' + ) + }) + test('path with multiple spaces', () => { expect(injectQuery('/usr/vite/path with space', 'direct')).toEqual( '/usr/vite/path with space?direct' diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 0f681ec8f7378f..7378b080eafd0c 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -311,11 +311,9 @@ export function injectQuery(url: string, queryToInject: string): string { if (resolvedUrl.protocol !== 'relative:') { resolvedUrl = pathToFileURL(url) } - let { protocol, pathname, search, hash } = resolvedUrl - if (protocol === 'file:') { - pathname = pathname.slice(1) - } - pathname = decodeURIComponent(pathname) + const { search, hash } = resolvedUrl + let pathname = cleanUrl(url) + pathname = isWindows ? slash(pathname) : pathname return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${ hash ?? '' }`