Skip to content

Commit 21a466c

Browse files
authored
Merge pull request #214 from atomic-state/fixes/cache
fix:
2 parents 7caaa64 + b3f71a4 commit 21a466c

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-react",
3-
"version": "3.8.3",
3+
"version": "3.8.4",
44
"description": "React hooks for data fetching",
55
"main": "dist/index.js",
66
"scripts": {

src/hooks/use-fetch.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
'use client'
2-
import {
3-
useState,
4-
useEffect,
5-
useMemo,
6-
useRef,
7-
useCallback,
8-
startTransition
9-
} from 'react'
2+
import { useState, useEffect, useMemo, useRef, useCallback } from 'react'
103

114
import {
125
abortControllers,
@@ -55,7 +48,6 @@ import {
5548
createImperativeFetch,
5649
getMiliseconds,
5750
getTimePassed,
58-
mutateData,
5951
revalidate,
6052
useIsomorphicLayoutEffect
6153
} from '../utils'
@@ -433,6 +425,10 @@ export function useFetch<FetchDataType = any, TransformData = any>(
433425

434426
const { data, loading, online, error, completedAttempts } = fetchState
435427

428+
const thisCache = paginationCache ?? normalCache ?? data ?? def ?? null
429+
430+
const rawJSON = serialize(data)
431+
436432
const isLoading = isExpired ? isPending(resolvedKey) || loading : false
437433

438434
const loadingFirst =
@@ -1451,7 +1447,13 @@ export function useFetch<FetchDataType = any, TransformData = any>(
14511447
return () => clearTimeout(handler)
14521448
}
14531449

1454-
revalidationLogic()
1450+
const startRevalidate = setTimeout(() => {
1451+
revalidationLogic()
1452+
}, 0)
1453+
1454+
return () => {
1455+
clearTimeout(startRevalidate)
1456+
}
14551457
}, [
14561458
resolvedKey,
14571459
serialize(optionsConfig),
@@ -1561,7 +1563,7 @@ Learn more: https://httpr.vercel.app/docs/api#suspense
15611563
hasErrors.get(resolvedDataKey) || hasErrors.get(resolvedKey) || error
15621564

15631565
const dataCandidate =
1564-
(error && isFailed ? (cacheIfError ? data : null) : data) ?? def
1566+
(error && isFailed ? (cacheIfError ? thisCache : null) : thisCache) ?? def
15651567

15661568
const responseData = isDefined(dataCandidate)
15671569
? transform!(dataCandidate)

0 commit comments

Comments
 (0)