Skip to content

Commit 41f3c6f

Browse files
authored
fix(utils): match timestamp query parameter with proper delimiters (#23364)
1 parent 57f0bf5 commit 41f3c6f

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎packages/vite/src/node/__tests__/utils.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
numberToPos,
2727
posToNumber,
2828
processSrcSetSync,
29+
removeTimestampQuery,
2930
resolveHostname,
3031
resolveServerUrls,
3132
} from '../utils'
@@ -195,6 +196,33 @@ describe('injectQuery', () => {
195196
})
196197
})
197198

199+
describe('removeTimestampQuery', () => {
200+
test('removes timestamp query parameter', () => {
201+
expect(removeTimestampQuery('/foo.js?t=1712345678901')).toBe('/foo.js')
202+
expect(removeTimestampQuery('/foo.js?t=1712345678901&bar=1')).toBe(
203+
'/foo.js?bar=1',
204+
)
205+
expect(removeTimestampQuery('/foo.js?bar=1&t=1712345678901')).toBe(
206+
'/foo.js?bar=1',
207+
)
208+
expect(removeTimestampQuery('/foo.js?bar=1&t=1712345678901&baz=2')).toBe(
209+
'/foo.js?bar=1&baz=2',
210+
)
211+
})
212+
213+
test('does not strip params with names ending in hyphen-t', () => {
214+
expect(removeTimestampQuery('/foo.js?current-t=1712345678901')).toBe(
215+
'/foo.js?current-t=1712345678901',
216+
)
217+
expect(removeTimestampQuery('/foo.js?my-t=1712345678901&other=1')).toBe(
218+
'/foo.js?my-t=1712345678901&other=1',
219+
)
220+
expect(removeTimestampQuery('/foo.js#t=1712345678901')).toBe(
221+
'/foo.js#t=1712345678901',
222+
)
223+
})
224+
})
225+
198226
describe('resolveHostname', () => {
199227
test('defaults to localhost', async () => {
200228
const resolved = await getLocalhostAddressIfDiffersFromDNS()

‎packages/vite/src/node/utils.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ export function injectQuery(url: string, queryToInject: string): string {
338338
return `${normalizedFile}?${queryToInject}${postfix[0] === '?' ? `&${postfix.slice(1)}` : /* hash only */ postfix}`
339339
}
340340

341-
const timestampRE = /\bt=\d{13}&?\b/
341+
const timestampRE = /(\?|&)t=\d{13}(?:&|$)/
342342
export function removeTimestampQuery(url: string): string {
343-
return url.replace(timestampRE, '').replace(trailingSeparatorRE, '')
343+
return url.replace(timestampRE, '$1').replace(trailingSeparatorRE, '')
344344
}
345345

346346
export async function asyncReplace(

0 commit comments

Comments
 (0)