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(client): don't inject queries for data URLs (fixes #2658) #2703

Merged
merged 1 commit into from
Mar 29, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ test('should load full dynamic import from public', async () => {
await untilUpdated(() => page.textContent('.view'), 'Qux view', true)
})

test('should load data URL of `blob:`', async () => {
await page.click('.issue-2658-1')
await untilUpdated(() => page.textContent('.view'), 'blob', true)
})

test('should load data URL of `data:`', async () => {
await page.click('.issue-2658-2')
await untilUpdated(() => page.textContent('.view'), 'data', true)
})

// since this test has a timeout, it should be put last so that it
// does not bleed on the last
test('should load dynamic import with vars', async () => {
await page.click('.foo')
await untilUpdated(() => page.textContent('.view'), 'Foo view', true)
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/dynamic-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<button class="bar">Bar</button>
<button class="baz">Baz</button>
<button class="qux">Qux</button>
<button class="issue-2658-1">Issue 2658 - 1</button>
<button class="issue-2658-2">Issue 2658 - 2</button>

<div class="view"></div>

Expand Down
20 changes: 20 additions & 0 deletions packages/playground/dynamic-import/nested/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ document.querySelector('.qux').addEventListener('click', async () => {
text('.view', msg)
})

// data URLs (`blob:`)
const code1 = 'export const msg = "blob"'
const blob = new Blob([code1], { type: 'text/javascript;charset=UTF-8' })
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const blobURL = URL.createObjectURL(blob)
document.querySelector('.issue-2658-1').addEventListener('click', async () => {
const { msg } = await import(/*@vite-ignore*/ blobURL)
text('.view', msg)
})

// data URLs (`data:`)
const code2 = 'export const msg = "data";'
const dataURL = `data:text/javascript;charset=utf-8,${encodeURIComponent(
code2
)}`
document.querySelector('.issue-2658-2').addEventListener('click', async () => {
const { msg } = await import(/*@vite-ignore*/ dataURL)
text('.view', msg)
})

function text(el, text) {
document.querySelector(el).textContent = text
}
8 changes: 7 additions & 1 deletion packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,13 @@ export const createHotContext = (ownerPath: string) => {
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 } = new URL(url, 'http://vitejs.dev')
const { search, hash, protocol } = new URL(url, 'http://vitejs.dev')

// data URLs shouldn't be appended queries, #2658
if (protocol === 'blob:' || protocol === 'data:') {
return url
}

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