Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: injectQuery break relative path #9760

Merged
merged 9 commits into from Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
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/'
)
})
poyoho marked this conversation as resolved.
Show resolved Hide resolved

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
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