Skip to content

Commit

Permalink
fix: do not inject ?import query to external urls
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 31, 2021
1 parent ef3ca62 commit be3a4f5
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ async function waitForSuccessfulPing(ms = 1000) {
await fetch(`${base}__vite_ping`)
break
} catch (e) {
await new Promise(resolve => setTimeout(resolve, ms))
await new Promise((resolve) => setTimeout(resolve, ms))
}
}
}
Expand Down Expand Up @@ -433,16 +433,19 @@ export const createHotContext = (ownerPath: string) => {
return hot
}

/**
* urls here are dynamic import() urls that couldn't be statically analyzed
*/
export function injectQuery(url: string, queryToInject: string) {
// can't use pathname from URL since it may be relative like ../
const pathname = url.replace(/#.*$/, '').replace(/\?.*$/, '')
const { search, hash, protocol } = new URL(url, 'http://vitejs.dev')

// data URLs shouldn't be appended queries, #2658
if (protocol === 'blob:' || protocol === 'data:') {
// skip urls that won't be handled by vite
if (!url.startsWith('.') && !url.startsWith('/')) {
return url
}

// 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')

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

0 comments on commit be3a4f5

Please sign in to comment.