Skip to content

Commit dd610b5

Browse files
authored
fix(assets): make timestamp invalidation lazy (#14675)
1 parent 6e7b25c commit dd610b5

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ import type { ResolvedConfig } from '../config'
2020
import {
2121
cleanUrl,
2222
getHash,
23+
injectQuery,
2324
joinUrlSegments,
2425
normalizePath,
2526
removeLeadingSlash,
2627
withTrailingSlash,
2728
} from '../utils'
2829
import { FS_PREFIX } from '../constants'
30+
import type { ModuleGraph } from '../server/moduleGraph'
2931

3032
// referenceId is base64url but replaces - with $
3133
export const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g
@@ -144,6 +146,8 @@ const viteBuildPublicIdPrefix = '\0vite:asset:public'
144146
export function assetPlugin(config: ResolvedConfig): Plugin {
145147
registerCustomMime()
146148

149+
let moduleGraph: ModuleGraph | undefined
150+
147151
return {
148152
name: 'vite:asset',
149153

@@ -152,6 +156,10 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
152156
generatedAssets.set(config, new Map())
153157
},
154158

159+
configureServer(server) {
160+
moduleGraph = server.moduleGraph
161+
},
162+
155163
resolveId(id) {
156164
if (!config.assetsInclude(cleanUrl(id)) && !urlRE.test(id)) {
157165
return
@@ -192,10 +200,17 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
192200
}
193201

194202
id = id.replace(urlRE, '$1').replace(unnededFinalQueryCharRE, '')
195-
const url = await fileToUrl(id, config, this)
196-
return `export default ${JSON.stringify(
197-
config.command === 'serve' ? `${url}?t=${Date.now()}` : url,
198-
)}`
203+
let url = await fileToUrl(id, config, this)
204+
205+
// Inherit HMR timestamp if this asset was invalidated
206+
if (moduleGraph) {
207+
const mod = moduleGraph.getModuleById(id)
208+
if (mod && mod.lastHMRTimestamp > 0) {
209+
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
210+
}
211+
}
212+
213+
return `export default ${JSON.stringify(url)}`
199214
},
200215

201216
renderChunk(code, chunk, opts) {

playground/assets/__tests__/assets.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,7 @@ describe('svg fragments', () => {
293293

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

@@ -323,11 +321,11 @@ test('?url import', async () => {
323321
test('?url import on css', async () => {
324322
const src = readFile('css/icons.css')
325323
const txt = await page.textContent('.url-css')
326-
isBuild
327-
? expect(txt).toEqual(
328-
`data:text/css;base64,${Buffer.from(src).toString('base64')}`,
329-
)
330-
: expect(txt).toMatch(/^\/foo\/bar\/css\/icons.css\?t=\d+$/)
324+
expect(txt).toEqual(
325+
isBuild
326+
? `data:text/css;base64,${Buffer.from(src).toString('base64')}`
327+
: '/foo/bar/css/icons.css',
328+
)
331329
})
332330

333331
describe('unicode url', () => {

playground/assets/__tests__/relative-base/relative-base-assets.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ describe('svg fragments', () => {
179179

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

playground/assets/__tests__/runtime-base/runtime-base-assets.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ describe('svg fragments', () => {
179179

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

playground/assets/__tests__/url-base/url-base-assets.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ describe('svg fragments', () => {
173173

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

0 commit comments

Comments
 (0)