@@ -10,6 +10,7 @@ type Retry = {
10
10
nextRetryUrl : ChunkSrcUrl ;
11
11
originalScriptFilename : ChunkFilename ;
12
12
originalSrcUrl : ChunkSrcUrl ;
13
+ originalQuery : string ;
13
14
} ;
14
15
15
16
type RetryCollector = Record < ChunkId , Record < number , Retry > > ;
@@ -99,37 +100,16 @@ function getUrlRetryQuery(
99
100
return '' ;
100
101
}
101
102
102
- function removeDomainFromUrl ( url : string ) : string {
103
- const protocolStartIndex = url . indexOf ( '//' ) ;
104
-
105
- // case /app/main/static/js/index.js
106
- if ( protocolStartIndex === - 1 && url . startsWith ( '/' ) ) {
107
- return url ;
108
- }
109
-
110
- // case "//cdn.com/app/main/static/js/index.js"
111
- // case "http://cdn.com/app/main/static/js/index.js"
112
- const protocolEndIndex = protocolStartIndex + 2 ;
113
- const pathStartIndex = url . indexOf ( '/' , protocolEndIndex ) ;
114
-
115
- return url . slice ( pathStartIndex ) ;
116
- }
117
-
118
- // "http://cdn.com/app/main/static/js/index.js?query=1#hash" -> "/app/main/static/js/index.js"
119
- function getAbsolutePathFromUrl ( url : string ) : string {
120
- return cleanUrl ( removeDomainFromUrl ( url ) ) ;
121
- }
122
-
123
103
function getNextRetryUrl (
124
- existRetryTimes : number ,
104
+ currRetryUrl : string ,
105
+ domain : string ,
125
106
nextDomain : string ,
126
- originalSrcUrl : string ,
107
+ existRetryTimes : number ,
108
+ originalQuery : string ,
127
109
) {
128
- const absolutePath = getAbsolutePathFromUrl ( originalSrcUrl ) ;
129
110
return (
130
- nextDomain +
131
- absolutePath +
132
- getUrlRetryQuery ( existRetryTimes , getQueryFromUrl ( originalSrcUrl ) )
111
+ cleanUrl ( currRetryUrl . replace ( domain , nextDomain ) ) +
112
+ getUrlRetryQuery ( existRetryTimes + 1 , originalQuery )
133
113
) ;
134
114
}
135
115
@@ -145,18 +125,28 @@ function getCurrentRetry(
145
125
function initRetry ( chunkId : string ) : Retry {
146
126
const originalScriptFilename = originalGetChunkScriptFilename ( chunkId ) ;
147
127
148
- const originalSrcUrl =
149
- __RUNTIME_GLOBALS_PUBLIC_PATH__ + originalScriptFilename ;
128
+ const originalPublicPath = __RUNTIME_GLOBALS_PUBLIC_PATH__ ;
129
+ const originalSrcUrl = originalPublicPath . startsWith ( '/' )
130
+ ? window . origin + originalPublicPath + originalScriptFilename
131
+ : originalPublicPath + originalScriptFilename ;
132
+ const originalQuery = getQueryFromUrl ( originalSrcUrl ) ;
150
133
151
- const existRetryTimes = 1 ;
134
+ const existRetryTimes = 0 ;
152
135
const nextDomain = config . domain ?. [ 0 ] ?? window . origin ;
153
136
154
137
return {
155
138
nextDomain,
156
- nextRetryUrl : getNextRetryUrl ( existRetryTimes , nextDomain , originalSrcUrl ) ,
139
+ nextRetryUrl : getNextRetryUrl (
140
+ originalSrcUrl ,
141
+ nextDomain ,
142
+ nextDomain ,
143
+ existRetryTimes ,
144
+ originalQuery ,
145
+ ) ,
157
146
158
147
originalScriptFilename,
159
148
originalSrcUrl,
149
+ originalQuery,
160
150
} ;
161
151
}
162
152
@@ -170,19 +160,22 @@ function nextRetry(chunkId: string, existRetryTimes: number): Retry {
170
160
nextRetry = initRetry ( chunkId ) ;
171
161
retryCollector [ chunkId ] = [ ] ;
172
162
} else {
173
- const { originalScriptFilename, originalSrcUrl } = currRetry ;
163
+ const { originalScriptFilename, originalSrcUrl, originalQuery } = currRetry ;
174
164
const nextDomain = findNextDomain ( currRetry . nextDomain ) ;
175
165
176
166
nextRetry = {
177
167
nextDomain,
178
168
nextRetryUrl : getNextRetryUrl (
179
- nextExistRetryTimes ,
169
+ currRetry . nextRetryUrl ,
170
+ currRetry . nextDomain ,
180
171
nextDomain ,
181
- originalSrcUrl ,
172
+ existRetryTimes ,
173
+ originalQuery ,
182
174
) ,
183
175
184
176
originalScriptFilename,
185
177
originalSrcUrl,
178
+ originalQuery,
186
179
} ;
187
180
}
188
181
@@ -258,13 +251,13 @@ function ensureChunk(
258
251
// the first calling is not retry
259
252
// if the failed request is 4 in network panel, callingCounter.count === 4, the first one is the normal request, and existRetryTimes is 3, retried 3 times
260
253
const existRetryTimes = callingCounter . count - 1 ;
261
- let originalSrcUrl : string ;
254
+ let originalScriptFilename : string ;
262
255
let nextRetryUrl : string ;
263
256
let nextDomain : string ;
264
257
265
258
try {
266
259
const retryResult = nextRetry ( chunkId , existRetryTimes ) ;
267
- originalSrcUrl = retryResult . originalSrcUrl ;
260
+ originalScriptFilename = retryResult . originalScriptFilename ;
268
261
nextRetryUrl = retryResult . nextRetryUrl ;
269
262
nextDomain = retryResult . nextDomain ;
270
263
} catch ( e ) {
@@ -292,7 +285,7 @@ function ensureChunk(
292
285
if ( existRetryTimes >= maxRetries ) {
293
286
error . message = error . message ?. includes ( 'retries:' )
294
287
? error . message
295
- : `Loading chunk ${ chunkId } from ${ originalSrcUrl } failed after ${ maxRetries } retries: "${ error . message } "` ;
288
+ : `Loading chunk ${ chunkId } from " ${ originalScriptFilename } " failed after ${ maxRetries } retries: "${ error . message } "` ;
296
289
if ( typeof config . onFail === 'function' ) {
297
290
config . onFail ( context ) ;
298
291
}
0 commit comments