Skip to content

V4#93

Merged
MattCCC merged 145 commits intomasterfrom
v4
Jul 14, 2025
Merged

V4#93
MattCCC merged 145 commits intomasterfrom
v4

Conversation

@MattCCC
Copy link
Owner

@MattCCC MattCCC commented Jul 14, 2025

Changelog

New Features

  • React useFetcher(url, config) Hook
    Introduced a new React hook for data fetching with built-in caching, loading states, and revalidation. You can import { useFetcher } from 'fetchff/react';
    Check the new React Integration section in the documentationf or more infromation.

  • Stale-While-Revalidate (SWR) Support
    Added staleTime configuration for serving stale data while revalidating in the background.

  • Custom Cache Keys and Infinite Cache
    Added support for custom cache key functions and possibility to cache infinitely for permanent caching scenarios.

  • Added refetchOnFocus setting
    Added support for refetchOnFocus (boolean) feature that automatically refetches data when the browser tab becomes visible/focused.

  • New refetchOnReconnect setting
    Enhance revalidation options by adding refetchOnReconnect (boolean) and updating related logic

  • Added onRetry(response, attempt) interceptor
    Implemented onRetry configuration to add interceptor based before each retry attempt.

  • Added onAbort(response) interceptor
    Implemented onAbort configuration to add interceptor based before each abort attempt.

  • Added cacheErrors setting
    Implemented cacheErrors (boolean) configuration to cache error responses for improved error handling.

  • Add keepPreviousData setting
    With keepPreviousData: true the last query’s data remains visible until the new one finishes loading.

  • Adaptive timeouts based on connection speed
    implement adaptive timeouts based on connection speed for improved user experience

  • Added isCancelled property to error objects
    This boolean lets us to quickly preview if request has been aborted.

  • Automatically set the Content-Type header based on th body type (beyond just JSON)
    Updated Content-Type handling in fetchff to automatically set based on request body type and enhanced isJSONSerializable checks.

  • ** Add select(data) => modifiedData setting**
    Add select(data) => modifiedData setting to transform response data for better performance.

Breaking changes

  • The fetcher setting now accepts a function only
    Previously it was an object with create and request. To simplify this, it's enough to pass fetcher function now. No instance creation is needed anymore.

  • Removed old, unused CanceledError detection for cancelled requests
    Use AbortError or TimeoutError instead.

  • Only some of request headers are considered in auto-key generation
    This is to improve cache hit rates. Carefully curated list is available in the readme file.

  • flattenData setting can flatten when there are more keys beyond just data available
    This will flatten up to the last data in an object available, recursively.

  • Single object typings over 4 generics
    The typings have been improved for performance and clarity reasons. Usage: {response: ResponseData, body: RequestBody, params: QueryParams, urlPathParams: UrlPathParams}. Check documentation for more examples

Module and Build Improvements

  • Exports Field in package.json
    Defined module entry points for Node.js, browser, and React environments to improve compatibility.

  • Centralized Build Configuration
    Simplified build setup with a shared tsup.config.ts file.

Request and Cache Enhancements

  • Improved Cache Mutation and Busting
    Enhanced mutate() with revalidation and subscriber notifications.
    getCachedResponse now supports full RequestConfig for cache busting.

  • Private Cache Implementation
    Internal _cache map is now fully encapsulated for better control and safety.

  • Inflight Manager Refactoring
    Replaced queue-manager with inflight-manager for clearer request deduplication and timeout handling.

  • shouldRetry can return null
    shouldRetry can return null so to fallback to retryOn status codes.

  • Interceptors running in right order
    Interceptors are now running in right order - FIFO for all, LIFO for On Response + added a section in documentation about it.

  • Removed properties from cache key auto-generation
    The following behavior controlled cache key properties are removed: cache, mode, redirect, referrer, integrity as they should not impact the key generation.

Testing

  • Added tests for React hook, concurrent request behavior, cache consistency, and memory cleanup.

Bug Fixes

  • Base URL Handling
    Ensured baseURL defaults to an empty string when undefined.

  • Memory Management
    Improved cleanup of timers and subscriptions to prevent leaks.

  • Cancellable requests abortion
    Returning an aborted controller would cause any new fetch to fail instantly.

  • Previous Cancellable requests were blocked by dedupeTime
    Deduplication logic should not block cancellable requests.

  • Absolute URLs in API Handler
    Apply endpoint config to absolute URLs defined if defined.

Developer Experience

  • Exposed subscribe(cacheKey, callback) function
    Added subscribe(cacheKey, callback) function for global real-time cache updates and data synchronization (tree-shakeable).

  • Exposed abortRequest(cacheKey, error) function
    Added abortRequest(cacheKey, error) function for global request aborting (tree-shakeable).

  • Exposed mutate(key, newData, settings) function
    Added mutate(key, newData, {fetch: boolean}) function for global request mutations (tree-shakeable).

  • Exposed setDefaultConfig(customConfig) function
    Added setDefaultConfig(customConfig) function for default config overwrites (tree-shakeable).

  • Exposed isSlowConnection() function
    Added isSlowConnection() function to detect slow connections in browsers (tree-shakeable).

  • Exposed getCache(key) function
    Added getCache(key) function for directly retrieving cached data without triggering network requests (tree-shakeable).

  • Exposed setCache(key, response, ttl, staleTime) function
    Added setCache(key, response, ttl, staleTime) function for low-level cache data management and direct cache entry creation (tree-shakeable).

  • Exposed deleteCache(key) function
    Added deleteCache(key) function for selective cache invalidation and cleanup operations (tree-shakeable).

  • Exposed revalidate(key, isStaleRevalidation) function
    Added revalidate(key, isStaleRevalidation) function for manual cache revalidation and fresh data fetching (tree-shakeable).

  • Exposed revalidateAll(type, isStaleRevalidation) function
    Added revalidateAll(type, isStaleRevalidation) function for bulk revalidation of focus or online event-triggered cache entries (tree-shakeable).

  • Exposed removeRevalidators(type) function
    Added removeRevalidators(type) function for cleanup of revalidation event listeners and memory leak prevention (tree-shakeable).

  • Better Type Safety
    Improved TypeScript definitions and type clarity throughout codebase

  • Code Organization
    Modularized code structure for better maintainability

MattCCC added 30 commits June 8, 2025 01:54
…Properties" to explain cache key auto-generation
…nstead of object with create() and request()
- Introduced INFINITE_CACHE_TIME constant to handle cache expiration logic.
- Updated isCacheExpired function to consider -1 as a valid cache time for infinite caching.
- Modified getCache and pruneCache functions to accommodate new cache time logic.
- Added cache reference counting utilities in a new cache-ref module for better cache management in React.
- Updated useFetcher hook to integrate cache reference counting, ensuring proper cache cleanup.
- Enhanced error handling and configuration options in useFetcher for improved user experience.
- Created comprehensive integration tests for React components utilizing useFetcher, covering various scenarios including error handling, polling, and revalidation.
- Implemented mockFetchResponse utility for simulating fetch responses in tests, improving test reliability and maintainability.
MattCCC added 18 commits July 12, 2025 01:03
SWR Support + React hook + Stability improvements
TypeScript Typings + integration improvements
@github-actions
Copy link

size-limit report 📦

Path Size
dist/browser/index.mjs 5.28 KB (+34.11% 🔺)
dist/browser/index.global.js 5.44 KB (+32.54% 🔺)
dist/node/index.js 5.35 KB (+33.51% 🔺)
dist/react/index.mjs 8.32 KB (+100% 🔺)
dist/react/index.js 9.03 KB (+100% 🔺)

@devdgna
Copy link
Collaborator

devdgna commented Jul 14, 2025

+1

@MattCCC MattCCC merged commit 70ab9a1 into master Jul 14, 2025
10 checks passed
@MattCCC MattCCC deleted the v4 branch July 14, 2025 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants