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(assets): make timestamp invalidation lazy #14675

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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