Skip to content

Commit

Permalink
fix: Improve injectQuery path handling
Browse files Browse the repository at this point in the history
close #2422
  • Loading branch information
GrygrFlzr committed Mar 8, 2021
1 parent f4998c0 commit 004a12f
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/vite/src/node/utils.ts
Expand Up @@ -3,7 +3,7 @@ import chalk from 'chalk'
import fs from 'fs'
import os from 'os'
import path from 'path'
import { parse as parseUrl } from 'url'
import { pathToFileURL, URL, URLSearchParams } from 'url'
import { FS_PREFIX, DEFAULT_EXTENSIONS, VALID_ID_PREFIX } from './constants'
import resolve from 'resolve'
import builtins from 'builtin-modules'
Expand Down Expand Up @@ -119,10 +119,22 @@ export function removeImportQuery(url: string) {
}

export function injectQuery(url: string, queryToInject: string) {
const { pathname, search, hash } = parseUrl(url)
return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ''}${
hash || ''
}`
let resolvedUrl = new URL(url, 'relative:///')
if (resolvedUrl.protocol !== 'relative:') {
resolvedUrl = pathToFileURL(url)
}
let { protocol, pathname, searchParams, hash } = resolvedUrl
if (protocol === 'file:') {
pathname = pathname.slice(1)
}
for (const [key, value] of new URLSearchParams(queryToInject).entries()) {
searchParams.append(key, value)
}
searchParams.sort()
const search = Array.from(searchParams.entries(), ([key, value]) =>
value !== '' ? `${key}=${value}` : key
).join('&')
return `${pathname}?${search}${hash || ''}`
}

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

0 comments on commit 004a12f

Please sign in to comment.