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 4f29dee
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
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,21 @@ 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)
}
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 4f29dee

Please sign in to comment.