Skip to content
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
28 changes: 28 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
numberToPos,
posToNumber,
processSrcSetSync,
removeTimestampQuery,
resolveHostname,
resolveServerUrls,
} from '../utils'
Expand Down Expand Up @@ -195,6 +196,33 @@ describe('injectQuery', () => {
})
})

describe('removeTimestampQuery', () => {
test('removes timestamp query parameter', () => {
expect(removeTimestampQuery('/foo.js?t=1712345678901')).toBe('/foo.js')
expect(removeTimestampQuery('/foo.js?t=1712345678901&bar=1')).toBe(
'/foo.js?bar=1',
)
expect(removeTimestampQuery('/foo.js?bar=1&t=1712345678901')).toBe(
'/foo.js?bar=1',
)
expect(removeTimestampQuery('/foo.js?bar=1&t=1712345678901&baz=2')).toBe(
'/foo.js?bar=1&baz=2',
)
})

test('does not strip params with names ending in hyphen-t', () => {
expect(removeTimestampQuery('/foo.js?current-t=1712345678901')).toBe(
'/foo.js?current-t=1712345678901',
)
expect(removeTimestampQuery('/foo.js?my-t=1712345678901&other=1')).toBe(
'/foo.js?my-t=1712345678901&other=1',
)
expect(removeTimestampQuery('/foo.js#t=1712345678901')).toBe(
'/foo.js#t=1712345678901',
)
})
})

describe('resolveHostname', () => {
test('defaults to localhost', async () => {
const resolved = await getLocalhostAddressIfDiffersFromDNS()
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ export function injectQuery(url: string, queryToInject: string): string {
return `${normalizedFile}?${queryToInject}${postfix[0] === '?' ? `&${postfix.slice(1)}` : /* hash only */ postfix}`
}

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

export async function asyncReplace(
Expand Down
Loading