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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make injectQuery idempotent #14424

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 6 additions & 4 deletions packages/vite/src/client/client.ts
Expand Up @@ -625,11 +625,13 @@ export function injectQuery(url: string, queryToInject: string): string {

// can't use pathname from URL since it may be relative like ../
const pathname = url.replace(/#.*$/, '').replace(/\?.*$/, '')
const { search, hash } = new URL(url, 'http://vitejs.dev')
const { search, searchParams, hash } = new URL(url, 'http://vitejs.dev')

return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
hash || ''
}`
return searchParams.get(queryToInject) === ''
? `${pathname}${search}${hash || ''}`
: `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
hash || ''
}`
}

export { ErrorOverlay }
11 changes: 7 additions & 4 deletions packages/vite/src/node/utils.ts
Expand Up @@ -320,12 +320,15 @@ export function injectQuery(url: string, queryToInject: string): string {
url.replace(replacePercentageRE, '%25'),
'relative:///',
)
const { search, hash } = resolvedUrl
const { search, searchParams, hash } = resolvedUrl
let pathname = cleanUrl(url)
pathname = isWindows ? slash(pathname) : pathname
return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
hash ?? ''
}`

return searchParams.get(queryToInject) === ''
? `${pathname}${search}${hash ?? ''}`
: `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
hash ?? ''
}`
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}

const timestampRE = /\bt=\d{13}&?\b/
Expand Down