Infinite Key Serialization
Purpose
Infinite Key Serialization addresses a fundamental challenge in infinite loading: uniquely and consistently identifying each paginated data request to enable reliable caching and revalidation. When fetching data page-by-page, each page's request key must be serialized into a stable string format that the SWR cache can recognize and differentiate. This serialization ensures that cached data for each page is properly stored, retrieved, and updated without collisions or inconsistencies.
Without such serialization utilities, keys representing infinite pages might be ambiguous or incompatible with the cache’s string-keyed storage, leading to stale data or unnecessary re-fetches. Thus, Infinite Key Serialization guarantees cache consistency and correctness across the infinite loading lifecycle.
Functionality
The core functionality revolves around transforming the dynamic page-based keys generated by an infinite loading key loader into stable, serializable strings:
Key Generation for Pages: The subtopic accepts a key loader function that, given a page index and previous page data, returns a request key (which could be a string, array, or null).
Serialization of Keys: These generated keys are serialized into strings compatible with the cache system. This process handles complex key types (arrays, objects) by converting them into unique, comparable string representations.
Prefixing for Infinite Keys: To distinguish infinite loading keys from other cached entries, serialized keys are prefixed with a constant string (
INFINITE_PREFIX). This avoids key collisions and aids middleware or debugging tools in identifying infinite loading cache entries.First Page Key Extraction: Since infinite loading often needs a base key to bootstrap or identify the initial page's cache entry, the utility extracts and serializes the first page’s key.
Key Methods
getFirstPageKey(getKey):
Takes the infinite key loader function and returns the serialized key for page 0.unstable_serialize(getKey):
Returns the serialized and prefixed key string for the infinite loading data source, primarily for internal use or debugging.
Code Snippet
export const getFirstPageKey = (getKey: SWRInfiniteKeyLoader) => {
return serialize(getKey ? getKey(0, null) : null)[0]
}
export const unstable_serialize = (getKey: SWRInfiniteKeyLoader) => {
return INFINITE_PREFIX + getFirstPageKey(getKey)
}
Here, serialize is a utility that converts the key (which may be complex) into a string tuple, ensuring uniqueness and cache compatibility.
Integration
Infinite Key Serialization serves as a foundational utility within the Infinite Loading Support parent topic. While the parent topic manages the overall workflow of paginated data fetching, page size management, and data concatenation, this subtopic focuses exclusively on how each page's request key is uniquely identified and stored.
It enables the core infinite loading middleware and hooks (
useSWRInfinite, infinite hook implementation) to interact seamlessly with the global cache by providing consistent keys.It complements Infinite Loading Types and Infinite Hook Implementation by enforcing a standardized key format, reducing bugs related to cache mismatches or invalidation.
It supports cache normalization and deduplication so that revalidation triggers, mutations, or subscription updates can correctly target the intended page’s cached data without ambiguity.
By prefixing keys, it facilitates middleware layers and developer tooling to distinguish infinite loading entries from other SWR cache entries, aiding debugging and introspection.
In sum, Infinite Key Serialization is the glue that binds the dynamic nature of infinite page keys to the stable, string-based cache infrastructure, enabling all other infinite loading features to function reliably.
Diagram
flowchart TD
A[Infinite Key Loader Function] --> B[Generate Page Key (e.g. page 0,1,...)]
B --> C[Serialize Key to String]
C --> D{Is First Page?}
D -->|Yes| E[Extract First Page Key]
E --> F[Prefix with INFINITE_PREFIX]
D -->|No| G[Use Serialized Key as-is]
F & G --> H[Store/Retrieve in SWR Cache]
H --> I[Enable Infinite Loading Hook & Middleware]
This flowchart illustrates how infinite page keys are generated, serialized, optionally prefixed, and then used by the SWR cache to support infinite loading workflows.