From e4bca4b4fc476598f8c67b54f307e8a14b1bf8c5 Mon Sep 17 00:00:00 2001 From: danybeltran Date: Mon, 22 Jul 2024 11:55:23 -0600 Subject: [PATCH 1/2] enh(caching): Enhances caching with maxCacheAge with debounce --- package.json | 2 +- src/hooks/use-fetch.ts | 76 +++++++++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index bd44cde..da318c5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "http-react", - "version": "3.6.6", + "version": "3.6.7", "description": "React hooks for data fetching", "main": "dist/index.js", "scripts": { diff --git a/src/hooks/use-fetch.ts b/src/hooks/use-fetch.ts index 5fad1ce..2feb207 100644 --- a/src/hooks/use-fetch.ts +++ b/src/hooks/use-fetch.ts @@ -1333,14 +1333,22 @@ export function useFetch( } useIsomorphicLayoutEffect(() => { - if (url !== '') { - if (!jsonCompare(previousProps.get(resolvedKey), optionsConfig)) { - abortControllers.get(resolvedKey)?.abort() - if (inDeps('data')) { - queue(initializeRevalidation) + const fn = () => { + if (url !== '') { + if (!jsonCompare(previousProps.get(resolvedKey), optionsConfig)) { + abortControllers.get(resolvedKey)?.abort() + if (inDeps('data')) { + queue(initializeRevalidation) + } } } } + if (debounce) { + const tm = setTimeout(fn, debounce) + return () => clearTimeout(tm) + } + fn() + return () => {} }, [serialize(optionsConfig), thisDeps, fetchState]) if (suspense) { @@ -1365,26 +1373,36 @@ export function useFetch( } useIsomorphicLayoutEffect(() => { - if (!runningRequests.get(resolvedKey) && isExpired) { - if (windowExists) { - if (canRevalidate && url !== '') { - if ( - !jsonCompare( - JSON.parse(previousConfig.get(resolvedKey) || '{}'), - optionsConfig - ) - ) { - if (!isPending(resolvedKey)) { - if (inDeps('data')) { - initializeRevalidation() + const fn = () => { + if (!runningRequests.get(resolvedKey) && canRevalidate) { + if (windowExists) { + if (canRevalidate && url !== '') { + if ( + !jsonCompare( + JSON.parse(previousConfig.get(resolvedKey) || '{}'), + optionsConfig + ) + ) { + if (!isPending(resolvedKey)) { + if (inDeps('data')) { + initializeRevalidation() + } + } else { + setLoading(true) } - } else { - setLoading(true) } } } } } + + if (debounce) { + const tm = setTimeout(fn, debounce) + return () => clearTimeout(tm) + } + fn() + + return () => {} }, [resolvedKey, serialize(optionsConfig), canRevalidate, thisDeps]) useIsomorphicLayoutEffect(() => { @@ -1403,16 +1421,26 @@ export function useFetch( } } - if (revalidateAfterUnmount) { - if (suspense) { - if (suspenseInitialized.get(resolvedKey)) { + const fn = () => { + if (revalidateAfterUnmount) { + if (suspense) { + if (suspenseInitialized.get(resolvedKey)) { + revalidate() + } + } else { revalidate() } - } else { - revalidate() } } + if (debounce) { + const tm = setTimeout(fn, debounce) + return () => clearTimeout(tm) + } + fn() + + return () => {} + // eslint-disable-next-line react-hooks/exhaustive-deps }, [serialize(optionsConfig), thisDeps]) From 0814ccdb97428dd988784114bfda7eaa2df52740 Mon Sep 17 00:00:00 2001 From: danybeltran Date: Mon, 22 Jul 2024 11:57:41 -0600 Subject: [PATCH 2/2] enh(debounce): debounce with useFetch is no longer deprecated --- src/types/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/types/index.ts b/src/types/index.ts index d5a84b3..92a68d1 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -330,7 +330,6 @@ export type FetchConfigType = Omit< formatBody?: boolean | ((b: BodyType) => any) /** * The time to wait before revalidation after props change - * @deprecated Use the `useDebounceFetch` hook instead */ debounce?: TimeSpan /**