Skip to content

Commit b7d494b

Browse files
authored
fix(hmr): watch non-inlined assets referenced by CSS (vitejs#20581)
1 parent c583927 commit b7d494b

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,18 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
385385
let resolved = await resolveUrl(id, importer)
386386
if (resolved) {
387387
if (fragment) resolved += '#' + fragment
388-
return [await fileToUrl(this, resolved), resolved]
388+
let url = await fileToUrl(this, resolved)
389+
// Inherit HMR timestamp if this asset was invalidated
390+
if (!url.startsWith('data:') && this.environment.mode === 'dev') {
391+
const mod = [
392+
...(this.environment.moduleGraph.getModulesByFile(resolved) ??
393+
[]),
394+
].find((mod) => mod.type === 'asset')
395+
if (mod?.lastHMRTimestamp) {
396+
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
397+
}
398+
}
399+
return [url, resolved]
389400
}
390401
if (config.command === 'build') {
391402
const isExternal = config.build.rollupOptions.external
@@ -1926,8 +1937,7 @@ const UrlRewritePostcssPlugin: PostCSS.PluginCreator<{
19261937
if (isCssUrl || isCssImageSet) {
19271938
const replacerForDeclaration = async (rawUrl: string) => {
19281939
const [newUrl, resolvedId] = await opts.resolver(rawUrl, importer)
1929-
// only register inlined assets to avoid frequent full refresh (#18979)
1930-
if (newUrl.startsWith('data:') && resolvedId) {
1940+
if (resolvedId) {
19311941
opts.deps.add(resolvedId)
19321942
}
19331943
return newUrl
@@ -3205,8 +3215,7 @@ async function compileLightningCSS(
32053215
dep.url,
32063216
dep.loc.filePath.replace(NULL_BYTE_PLACEHOLDER, '\0'),
32073217
)
3208-
// only register inlined assets to avoid frequent full refresh (#18979)
3209-
if (newUrl.startsWith('data:') && resolvedId) {
3218+
if (resolvedId) {
32103219
deps.add(resolvedId)
32113220
}
32123221
replaceUrl = newUrl

playground/assets/__tests__/assets.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ describe('css url() references', () => {
373373
await expect.poll(() => getBg('.css-url-svg')).toMatch('red')
374374
}
375375
})
376+
377+
test.runIf(isServe)('non inlined url() HMR', async () => {
378+
const bg = await getBg('.css-url-non-inline-hmr')
379+
editFile('nested/donuts-large.svg', (code) =>
380+
code.replace('fill="blue"', 'fill="red"'),
381+
)
382+
await expect.poll(() => getBg('.css-url-non-inline-hmr')).not.toBe(bg)
383+
})
376384
})
377385

378386
describe('image', () => {

playground/assets/css/css-url.css

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

playground/assets/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ <h2>CSS url references</h2>
170170
<span style="background: #fff">CSS SVG background with image-set</span>
171171
</div>
172172

173+
<div class="css-url-non-inline-hmr">
174+
<span style="background: #fff">CSS non-inlined background HMR</span>
175+
</div>
176+
173177
<h2>Unicode URL</h2>
174178
<div>
175179
<code class="unicode-url"></code>

playground/assets/nested/donuts-large.svg

Lines changed: 5 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)