Skip to content

Commit

Permalink
fix: injectQuery break relative path (#9760)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho authored Aug 24, 2022
1 parent a8279af commit 61273b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
27 changes: 27 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 3 additions & 5 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? ''
}`
Expand Down

0 comments on commit 61273b2

Please sign in to comment.