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: preserve base in HMR for CSS referenced in link tags #4407

Merged
merged 3 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -1,4 +1,10 @@
import { isBuild, readManifest } from '../../testUtils'
import {
editFile,
getColor,
isBuild,
readManifest,
untilUpdated
} from '../../testUtils'

const outerAssetMatch = isBuild
? /\/dev\/assets\/logo\.\w{8}\.png/
Expand All @@ -25,4 +31,16 @@ if (isBuild) {
expect(htmlEntry.css.length).toEqual(1)
expect(htmlEntry.assets.length).toEqual(1)
})
} else {
test('preserve the base in CSS HMR', async () => {
await untilUpdated(() => getColor('body'), 'black') // sanity check
editFile('frontend/entrypoints/global.css', (code) =>
code.replace('black', 'red')
)
await untilUpdated(() => getColor('body'), 'red') // successful HMR

// Verify that the base (/dev/) was added during the css-update
const link = await page.$('link[rel="stylesheet"]')
expect(await link.getAttribute('href')).toContain('/dev/global.css?t=')
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ body {
}

body {
color: black;
margin: 4vh auto;
max-width: 800px;
padding: 0 4vw;
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ async function handleMessage(payload: HMRPayload) {
) as HTMLLinkElement[]
).find((e) => e.href.includes(path))
if (el) {
const newPath = `${path}${
path.includes('?') ? '&' : '?'
}t=${timestamp}`
const newPath =
base +
`${path.slice(1)}${path.includes('?') ? '&' : '?'}t=${timestamp}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
base +
`${path.slice(1)}${path.includes('?') ? '&' : '?'}t=${timestamp}`
`${base}${path.slice(1)}${path.includes('?') ? '&' : '?'}t=${timestamp}`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also please run the formatter after this change? Could change the suggestion

el.href = new URL(newPath, el.href).href
}
console.log(`[vite] css hot updated: ${path}`)
Expand Down