File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
198226describe ( 'resolveHostname' , ( ) => {
199227 test ( 'defaults to localhost' , async ( ) => {
200228 const resolved = await getLocalhostAddressIfDiffersFromDNS ( )
Original file line number Diff line number Diff 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 = / \b t = \d { 13 } & ? \b /
341+ const timestampRE = / ( \? | & ) t = \d { 13 } (?: & | $ ) /
342342export function removeTimestampQuery ( url : string ) : string {
343- return url . replace ( timestampRE , '' ) . replace ( trailingSeparatorRE , '' )
343+ return url . replace ( timestampRE , '$1 ' ) . replace ( trailingSeparatorRE , '' )
344344}
345345
346346export async function asyncReplace (
You can’t perform that action at this time.
0 commit comments