Skip to content

Commit

Permalink
fix(hmr): add timestamp for assets in dev (#13371)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre authored Oct 17, 2023
1 parent d4f62e4 commit 40ee245
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 10 deletions.
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ export function assetPlugin(config: ResolvedConfig): Plugin {

id = id.replace(urlRE, '$1').replace(unnededFinalQueryCharRE, '')
const url = await fileToUrl(id, config, this)
return `export default ${JSON.stringify(url)}`
return `export default ${JSON.stringify(
config.command === 'serve' ? `${url}?t=${Date.now()}` : url,
)}`
},

renderChunk(code, chunk, opts) {
Expand Down
14 changes: 8 additions & 6 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ describe('svg fragments', () => {

test('from js import', async () => {
const img = await page.$('.svg-frag-import')
expect(await img.getAttribute('src')).toMatch(/svg#icon-heart-view$/)
expect(await img.getAttribute('src')).toMatch(
isBuild ? /svg#icon-heart-view$/ : /svg\?t=\d+#icon-heart-view$/,
)
})
})

Expand Down Expand Up @@ -321,11 +323,11 @@ test('?url import', async () => {
test('?url import on css', async () => {
const src = readFile('css/icons.css')
const txt = await page.textContent('.url-css')
expect(txt).toEqual(
isBuild
? `data:text/css;base64,${Buffer.from(src).toString('base64')}`
: '/foo/bar/css/icons.css',
)
isBuild
? expect(txt).toEqual(
`data:text/css;base64,${Buffer.from(src).toString('base64')}`,
)
: expect(txt).toMatch(/^\/foo\/bar\/css\/icons.css\?t=\d+$/)
})

describe('unicode url', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ describe('svg fragments', () => {

test('from js import', async () => {
const img = await page.$('.svg-frag-import')
expect(await img.getAttribute('src')).toMatch(/svg#icon-heart-view$/)
expect(await img.getAttribute('src')).toMatch(
isBuild ? /svg#icon-heart-view$/ : /svg\?t=\d+#icon-heart-view$/,
)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ describe('svg fragments', () => {

test('from js import', async () => {
const img = await page.$('.svg-frag-import')
expect(await img.getAttribute('src')).toMatch(/svg#icon-heart-view$/)
expect(await img.getAttribute('src')).toMatch(
isBuild ? /svg#icon-heart-view$/ : /svg\?t=\d+#icon-heart-view$/,
)
})
})

Expand Down
4 changes: 3 additions & 1 deletion playground/assets/__tests__/url-base/url-base-assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ describe('svg fragments', () => {

test('from js import', async () => {
const img = await page.$('.svg-frag-import')
expect(await img.getAttribute('src')).toMatch(/svg#icon-heart-view$/)
expect(await img.getAttribute('src')).toMatch(
isBuild ? /svg#icon-heart-view$/ : /svg\?t=\d+#icon-heart-view$/,
)
})
})

Expand Down
13 changes: 13 additions & 0 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,17 @@ if (import.meta.hot) {
)
await untilUpdated(() => el.textContent(), 'cc')
})

test('assets HMR', async () => {
await page.goto(viteTestUrl)
const el = await page.$('#logo')
await untilBrowserLogAfter(
() =>
editFile('logo.svg', (code) =>
code.replace('height="30px"', 'height="40px"'),
),
/Logo updated/,
)
await untilUpdated(() => el.evaluate((it) => `${it.clientHeight}`), '40')
})
}
11 changes: 11 additions & 0 deletions playground/hmr/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import './importing-updated'
import './invalidation/parent'
import './file-delete-restore'
import './optional-chaining/parent'
import logo from './logo.svg'

export const foo = 1
text('.app', foo)
text('.dep', depFoo)
text('.nested', nestedFoo)
text('.virtual', virtual)
setLogo(logo)

const btn = document.querySelector('.virtual-update') as HTMLButtonElement
btn.onclick = () => {
Expand All @@ -34,6 +36,11 @@ if (import.meta.hot) {
text('.nested', newNestedFoo)
}

import.meta.hot.accept('./logo.svg', (newUrl) => {
setLogo(newUrl.default)
console.log('Logo updated', newUrl.default)
})

import.meta.hot.accept('./hmrDep', ({ foo, nestedFoo }) => {
handleDep('single dep', foo, nestedFoo)
})
Expand Down Expand Up @@ -121,6 +128,10 @@ function text(el, text) {
document.querySelector(el).textContent = text
}

function setLogo(src) {
;(document.querySelector('#logo') as HTMLImageElement).src = src
}

function removeCb({ msg }) {
text('.toRemove', msg)
import.meta.hot.off('custom:remove', removeCb)
Expand Down
1 change: 1 addition & 0 deletions playground/hmr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
<div class="importing-reloaded"></div>
<div class="file-delete-restore"></div>
<div class="optional-chaining"></div>
<image id="logo"></image>
3 changes: 3 additions & 0 deletions playground/hmr/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 40ee245

Please sign in to comment.