From e291f2a2e0ca6406f2cf984af505b3eafc839760 Mon Sep 17 00:00:00 2001 From: Alonso Corona Date: Thu, 25 Jun 2026 22:56:43 +0000 Subject: [PATCH] Fix useFetch paginated URL factory errors --- src/useFetch.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/useFetch.ts b/src/useFetch.ts index 1d0489c..4139ccc 100644 --- a/src/useFetch.ts +++ b/src/useFetch.ts @@ -144,7 +144,16 @@ export function useFetch(null); const firstPageUrlRef = useRef(null); - const firstPageUrl = typeof url === "function" ? url({ page: 0 }) : undefined; + const isPaginated = typeof url === "function"; + let firstPageUrl: RequestInfo | undefined; + + if (isPaginated) { + try { + firstPageUrl = url({ page: 0 }); + } catch { + // Defer URL factory errors to the paginated fetcher so usePromise can expose them through its error state. + } + } /** * When paginating, `url` is a `PaginatedRequestInfo`, so we only want to update the ref when the `firstPageUrl` changes. * When not paginating, `url` is a `RequestInfo`, so we want to update the ref whenever `url` changes. @@ -174,11 +183,11 @@ export function useFetch { - if (firstPageUrlRef.current) { + if (isPaginated) { return paginatedFn; } return fn; - }, [firstPageUrlRef, fn, paginatedFn]); + }, [isPaginated, fn, paginatedFn]); // @ts-expect-error lastItem can't be inferred properly return useCachedPromise(promise, [urlRef.current as PaginatedRequestInfo, fetchOptions], {