Skip to content

Commit

Permalink
fix(assets): make timestamp invalidation lazy (#14675)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Oct 18, 2023
1 parent 6e7b25c commit dd610b5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
23 changes: 19 additions & 4 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import type { ResolvedConfig } from '../config'
import {
cleanUrl,
getHash,
injectQuery,
joinUrlSegments,
normalizePath,
removeLeadingSlash,
withTrailingSlash,
} from '../utils'
import { FS_PREFIX } from '../constants'
import type { ModuleGraph } from '../server/moduleGraph'

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

let moduleGraph: ModuleGraph | undefined

return {
name: 'vite:asset',

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

configureServer(server) {
moduleGraph = server.moduleGraph
},

resolveId(id) {
if (!config.assetsInclude(cleanUrl(id)) && !urlRE.test(id)) {
return
Expand Down Expand Up @@ -192,10 +200,17 @@ 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(
config.command === 'serve' ? `${url}?t=${Date.now()}` : url,
)}`
let url = await fileToUrl(id, config, this)

// Inherit HMR timestamp if this asset was invalidated
if (moduleGraph) {
const mod = moduleGraph.getModuleById(id)
if (mod && mod.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
}
}

return `export default ${JSON.stringify(url)}`
},

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

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

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

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

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

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

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

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

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

Expand Down

0 comments on commit dd610b5

Please sign in to comment.