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(dev): Prevent stripping query params from CSS in HMR #6589

Merged
merged 3 commits into from
Jan 26, 2022
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
16 changes: 16 additions & 0 deletions packages/playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,20 @@ if (!isBuild) {
'title2'
)
})

test('CSS update preserves query params', async () => {
await page.goto(viteTestUrl)

editFile('global.css', (code) => code.replace('white', 'tomato'))

const elprev = await page.$('.css-prev')
const elpost = await page.$('.css-post')
await untilUpdated(() => elprev.textContent(), 'param=required')
await untilUpdated(() => elpost.textContent(), 'param=required')
const textprev = await elprev.textContent()
const textpost = await elpost.textContent()
expect(textprev).not.toBe(textpost)
expect(textprev).not.toMatch('direct')
expect(textpost).not.toMatch('direct')
})
}
3 changes: 3 additions & 0 deletions packages/playground/hmr/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: white;
}
13 changes: 13 additions & 0 deletions packages/playground/hmr/hmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ if (import.meta.hot) {

import.meta.hot.on('vite:beforeUpdate', (event) => {
console.log(`>>> vite:beforeUpdate -- ${event.type}`)

const cssUpdate = event.updates.find(
(update) =>
update.type === 'css-update' && update.path.match('global.css')
)
if (cssUpdate) {
const el = document.querySelector('#global-css')
text('.css-prev', el.href)
// We don't have a vite:afterUpdate event, but updates are currently sync
setTimeout(() => {
text('.css-post', el.href)
}, 0)
}
})

import.meta.hot.on('vite:error', (event) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/hmr/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<link id="global-css" rel="stylesheet" href="./global.css?param=required" />
<script type="module" src="./hmr.js"></script>

<div class="app"></div>
<div class="dep"></div>
<div class="nested"></div>
<div class="custom"></div>
<div class="css-prev"></div>
<div class="css-post"></div>
18 changes: 12 additions & 6 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ function warnFailedFetch(err: Error, path: string | string[]) {
)
}

function cleanUrl(pathname: string): string {
const url = new URL(pathname, location.toString())
url.searchParams.delete('direct')
return url.pathname + url.search
}

// Listen for messages
socket.addEventListener('message', async ({ data }) => {
handleMessage(JSON.parse(data))
Expand Down Expand Up @@ -73,21 +79,21 @@ async function handleMessage(payload: HMRPayload) {
} else {
// css-update
// this is only sent when a css file referenced with <link> is updated
let { path, timestamp } = update
path = path.replace(/\?.*/, '')
const { path, timestamp } = update
const searchUrl = cleanUrl(path)
// can't use querySelector with `[href*=]` here since the link may be
// using relative paths so we need to use link.href to grab the full
// URL for the include check.
const el = Array.from(
document.querySelectorAll<HTMLLinkElement>('link')
).find((e) => e.href.includes(path))
).find((e) => cleanUrl(e.href).includes(searchUrl))
if (el) {
const newPath = `${base}${path.slice(1)}${
path.includes('?') ? '&' : '?'
const newPath = `${base}${searchUrl.slice(1)}${
searchUrl.includes('?') ? '&' : '?'
}t=${timestamp}`
el.href = new URL(newPath, el.href).href
}
console.log(`[vite] css hot updated: ${path}`)
console.log(`[vite] css hot updated: ${searchUrl}`)
}
})
break
Expand Down