@@ -101,7 +101,6 @@ export function useFetch<FetchDataType = any, BodyType = any>(
101
101
onOnline = ctx . onOnline ,
102
102
onOffline = ctx . onOffline ,
103
103
onMutate,
104
- onPropsChange,
105
104
revalidateOnMount = ctx . revalidateOnMount ,
106
105
url = '' ,
107
106
query = { } ,
@@ -153,7 +152,6 @@ export function useFetch<FetchDataType = any, BodyType = any>(
153
152
154
153
const willResolve = isDefined ( onResolve )
155
154
const handleError = isDefined ( onError )
156
- const handlePropsChange = isDefined ( onPropsChange )
157
155
const handleOnAbort = isDefined ( onAbort )
158
156
const handleMutate = isDefined ( onMutate )
159
157
const handleOnline = isDefined ( onOnline )
@@ -334,6 +332,17 @@ export function useFetch<FetchDataType = any, BodyType = any>(
334
332
335
333
const { data, loading, online, error, completedAttempts } = fetchState
336
334
335
+ const isLoading = isExpired ? isPending ( resolvedKey ) || loading : false
336
+
337
+ const loadingFirst =
338
+ ! ( hasData [ resolvedDataKey ] || hasData [ resolvedKey ] ) && isLoading
339
+
340
+ if ( ! isExpired ) {
341
+ if ( error ) {
342
+ setError ( null )
343
+ }
344
+ }
345
+
337
346
function setData ( v : any ) {
338
347
setFetchState ( p => {
339
348
if ( isFunction ( v ) ) {
@@ -493,15 +502,8 @@ export function useFetch<FetchDataType = any, BodyType = any>(
493
502
494
503
cacheProvider . set ( ageKey , Date . now ( ) - 1 )
495
504
496
- // @ts -ignore null is a falsy value
497
- setFetchState ( prev => ( {
498
- ...prev ,
499
- loading : true ,
500
- error : null
501
- } ) )
502
-
503
505
requestsProvider . emit ( resolvedKey , {
504
- requestCallId,
506
+ requestCallId : loadingFirst ? requestCallId : undefined ,
505
507
loading : true ,
506
508
requestAbortController : newAbortController ,
507
509
error : null
@@ -535,6 +537,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
535
537
536
538
const r = isRequest
537
539
? new Request ( realUrl + c . query , {
540
+ ...ctx ,
538
541
...reqConfig ,
539
542
...optionsConfig ,
540
543
signal : ( ( ) => {
@@ -544,16 +547,16 @@ export function useFetch<FetchDataType = any, BodyType = any>(
544
547
'Content-Type' : 'application/json' ,
545
548
...ctx . headers ,
546
549
..._headers ,
547
- ...optionsConfig . headers ,
550
+ ...config . headers ,
548
551
...c . headers
549
552
}
550
553
} as any )
551
554
: new Request ( realUrl + c . query , {
552
555
...ctx ,
556
+ ...optionsConfig ,
553
557
signal : ( ( ) => {
554
558
return newAbortController . signal
555
559
} ) ( ) ,
556
- ...optionsConfig ,
557
560
body : isFunction ( formatBody )
558
561
? // @ts -ignore // If formatBody is a function
559
562
formatBody ( optionsConfig ?. body as any )
@@ -563,7 +566,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
563
566
...ctx . headers ,
564
567
...config . headers ,
565
568
...c . headers
566
- } as Headers
569
+ }
567
570
} )
568
571
if ( logStart ) {
569
572
; ( onFetchStart as any ) ( r , optionsConfig , ctx )
@@ -813,7 +816,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
813
816
...p ,
814
817
data : $$data ?? p . data ,
815
818
error : isDefined ( $$error ) ? $$error : p . error ,
816
- loading : false ?? p . loading ,
819
+ loading : false ,
817
820
completedAttempts : $$completedAttempts ?? p . completedAttempts
818
821
} ) )
819
822
@@ -828,6 +831,7 @@ export function useFetch<FetchDataType = any, BodyType = any>(
828
831
...rpc
829
832
} )
830
833
834
+ willSuspend [ resolvedKey ] = false
831
835
queue ( ( ) => {
832
836
canDebounce [ resolvedKey ] = true
833
837
} , debounce )
@@ -917,18 +921,24 @@ export function useFetch<FetchDataType = any, BodyType = any>(
917
921
}
918
922
919
923
if ( v . requestCallId !== requestCallId ) {
920
- queue ( ( ) => {
921
- setFetchState ( p => {
922
- return {
923
- ...p ,
924
- completedAttempts : completedAttempts ?? p . completedAttempts ,
925
- loading : loading ?? p . loading ,
926
- data : ! jsonCompare ( $data , p . data ) ? $data : p . data ?? p . data ,
927
- error : $error ,
928
- online : online ?? p . online
929
- }
924
+ if ( ! willSuspend [ resolvedKey ] ) {
925
+ queue ( ( ) => {
926
+ setFetchState ( p => {
927
+ const n = {
928
+ ...p ,
929
+ completedAttempts : completedAttempts ?? p . completedAttempts ,
930
+ loading : loading ?? p . loading ,
931
+ data : ! jsonCompare ( $data , p . data ) ? $data : p . data ?? p . data ,
932
+ error : $error ,
933
+ online : online ?? p . online
934
+ }
935
+
936
+ if ( jsonCompare ( n , p ) ) return p
937
+
938
+ return n
939
+ } )
930
940
} )
931
- } )
941
+ }
932
942
}
933
943
}
934
944
@@ -937,7 +947,12 @@ export function useFetch<FetchDataType = any, BodyType = any>(
937
947
return ( ) => {
938
948
requestsProvider . removeListener ( resolvedKey , waitFormUpdates )
939
949
}
940
- } , [ JSON . stringify ( optionsConfig ) , requestCallId ] )
950
+ } , [
951
+ JSON . stringify ( optionsConfig ) ,
952
+ resolvedKey ,
953
+ resolvedDataKey ,
954
+ requestCallId
955
+ ] )
941
956
942
957
const reValidate = React . useCallback (
943
958
async function reValidate ( ) {
@@ -1182,19 +1197,21 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1182
1197
}
1183
1198
}
1184
1199
1185
- useEffect ( ( ) => {
1186
- if ( windowExists ) {
1187
- if ( canRevalidate && url !== '' ) {
1188
- if ( ! jsonCompare ( previousConfig [ resolvedKey ] , optionsConfig ) ) {
1189
- if ( ! isPending ( resolvedKey ) ) {
1190
- initializeRevalidation ( )
1191
- } else {
1192
- setLoading ( true )
1200
+ React . useMemo ( ( ) => {
1201
+ if ( ! runningRequests [ resolvedKey ] && isExpired ) {
1202
+ if ( windowExists ) {
1203
+ if ( canRevalidate && url !== '' ) {
1204
+ if ( ! jsonCompare ( previousConfig [ resolvedKey ] , optionsConfig ) ) {
1205
+ if ( ! isPending ( resolvedKey ) ) {
1206
+ initializeRevalidation ( )
1207
+ } else {
1208
+ setLoading ( true )
1209
+ }
1193
1210
}
1194
1211
}
1195
1212
}
1196
1213
}
1197
- } , [ resolvedKey , serialize ( optionsConfig ) ] )
1214
+ } , [ resolvedKey , serialize ( optionsConfig ) , canRevalidate ] )
1198
1215
1199
1216
useEffect ( ( ) => {
1200
1217
const revalidateAfterUnmount = revalidateOnMount
@@ -1312,75 +1329,6 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1312
1329
}
1313
1330
}
1314
1331
1315
- useEffect ( ( ) => {
1316
- const rev = {
1317
- revalidate : ( ) => queue ( ( ) => revalidate ( id ) ) ,
1318
- cancel : ( ) => {
1319
- try {
1320
- if ( url !== '' ) {
1321
- if ( previousConfig [ resolvedKey ] !== serialize ( optionsConfig ) ) {
1322
- if ( auto ) {
1323
- setLoading ( ( ) => {
1324
- requestAbortController ?. abort ( )
1325
- queue ( ( ) => {
1326
- initializeRevalidation ( )
1327
- } )
1328
- return true
1329
- } )
1330
- }
1331
- }
1332
- }
1333
- } catch ( err ) { }
1334
- } ,
1335
- fetcher : imperativeFetch ,
1336
- props : optionsConfig ,
1337
- previousProps : previousProps [ resolvedKey ]
1338
- }
1339
-
1340
- queue ( ( ) => {
1341
- if ( ! canRevalidate && url !== '' && debounce ) {
1342
- canDebounce [ resolvedKey ] = true
1343
- }
1344
- } )
1345
-
1346
- if ( serialize ( previousProps [ resolvedKey ] ) !== serialize ( optionsConfig ) ) {
1347
- if ( debounce ) {
1348
- canDebounce [ resolvedKey ] = true
1349
- }
1350
- if ( handlePropsChange ) {
1351
- ; ( onPropsChange as any ) ( rev as any )
1352
- }
1353
- if ( url !== '' ) {
1354
- previousProps [ resolvedKey ] = optionsConfig
1355
- }
1356
- if ( cancelOnChange ) {
1357
- if ( url !== '' ) {
1358
- if ( auto ) {
1359
- setLoading ( ( ) => {
1360
- requestAbortController ?. abort ( )
1361
- queue ( ( ) => {
1362
- initializeRevalidation ( )
1363
- } )
1364
- return true
1365
- } )
1366
- }
1367
- }
1368
- }
1369
- if ( canRevalidate && url !== '' ) {
1370
- const debounceTimeout = setTimeout ( ( ) => {
1371
- initializeRevalidation ( )
1372
- if ( suspenseInitialized [ resolvedKey ] ) {
1373
- }
1374
- } , debounce )
1375
-
1376
- return ( ) => {
1377
- clearTimeout ( debounceTimeout )
1378
- }
1379
- }
1380
- }
1381
- return ( ) => { }
1382
- } , [ serialize ( optionsConfig ) ] )
1383
-
1384
1332
const [ $requestStart , $requestEnd ] = [
1385
1333
notNull ( cacheProvider . get ( 'requestStart' + resolvedDataKey ) )
1386
1334
? new Date ( cacheProvider . get ( 'requestStart' + resolvedDataKey ) )
@@ -1400,20 +1348,13 @@ export function useFetch<FetchDataType = any, BodyType = any>(
1400
1348
? new Date ( cacheProvider . get ( 'expiration' + resolvedDataKey ) )
1401
1349
: null
1402
1350
1403
- const isLoading = isExpired ? isPending ( resolvedKey ) || loading : false
1404
-
1405
- const isFailed =
1406
- ( hasErrors [ resolvedDataKey ] || hasErrors [ resolvedKey ] || error ) &&
1407
- ! isLoading
1351
+ const isFailed = ( hasErrors [ resolvedDataKey ] || error ) && ! isLoading
1408
1352
1409
1353
const responseData =
1410
1354
( error && isFailed ? ( cacheIfError ? thisCache : null ) : thisCache ) ?? def
1411
1355
1412
1356
const isSuccess = ! isLoading && ! isFailed
1413
1357
1414
- const loadingFirst =
1415
- ! ( hasData [ resolvedDataKey ] || hasData [ resolvedKey ] ) && isLoading
1416
-
1417
1358
const oneRequestResolved =
1418
1359
! loadingFirst &&
1419
1360
( hasData [ resolvedDataKey ] ||
0 commit comments