serialize.ts


Overview

The serialize.ts file provides essential utility functions related to key serialization for infinite loading in the SWR (stale-while-revalidate) data fetching ecosystem. Specifically, it focuses on generating and serializing the cache keys for paginated or infinite data requests in a consistent, stable, and unique manner.

Infinite loading relies on dynamically generated keys for each page of data fetched. This file:

These utilities are foundational for the infinite loading middleware and hooks (e.g., useSWRInfinite), enabling them to properly identify and manage paginated data caches.


Exports

getFirstPageKey

export const getFirstPageKey = (getKey: SWRInfiniteKeyLoader) => string

Description

Generates the serialized cache key for the first page (pageIndex = 0) of an infinite data request, using the provided infinite key loader function.

Parameters

Name

Type

Description

getKey

SWRInfiniteKeyLoader

A function that returns the key for a given page index and previous page data.

Returns

Usage Example

import { getFirstPageKey } from './serialize'
import type { SWRInfiniteKeyLoader } from './types'

const keyLoader: SWRInfiniteKeyLoader = (index, previousPageData) => {
  if (index === 0) return ['api/items', { limit: 10 }]
  if (previousPageData && previousPageData.nextCursor)
    return ['api/items', { cursor: previousPageData.nextCursor }]
  return null
}

const firstPageKey = getFirstPageKey(keyLoader)
console.log(firstPageKey) // e.g. "api/items?limit=10"

unstable_serialize

export const unstable_serialize = (getKey: SWRInfiniteKeyLoader) => string

Description

Generates a unique serialized cache key string for the entire infinite loading data source by prefixing the serialized first page key with a constant string INFINITE_PREFIX.

This function is primarily for internal or experimental use (as suggested by the unstable_ prefix). It helps differentiate infinite loading keys from normal keys in the global SWR cache, preventing collisions and allowing middleware layers to recognize infinite loading entries.

Parameters

Name

Type

Description

getKey

SWRInfiniteKeyLoader

The infinite key loader function to generate keys from.

Returns

Usage Example

import { unstable_serialize } from './serialize'
import type { SWRInfiniteKeyLoader } from './types'

const keyLoader: SWRInfiniteKeyLoader = (index, prev) => ['api/items', index]

const infiniteKey = unstable_serialize(keyLoader)
console.log(infiniteKey) // e.g. "__INFINITE__api/items?index=0"

Implementation Details


Relationships and Interactions


Mermaid Diagram

flowchart TD
  A[Infinite Key Loader Function (getKey)] --> B[Generate Page Key for Page 0]
  B --> C[Serialize Key to String Tuple]
  C --> D[Extract Serialized First Page Key (string)]
  D --> E{Build Infinite Key?}
  E -->|Yes| F[Prefix with INFINITE_PREFIX]
  E -->|No| G[Return Serialized Key As-Is]
  F & G --> H[Use as Cache Key in SWR Global Cache]
  H --> I[Support Infinite Loading Hook & Middleware]

Summary

The serialize.ts file encapsulates the logic to:

This simple but critical utility ensures cache consistency and uniqueness, enabling the infinite loading features to function correctly across the application.


Appendix: Types and Constants (Context)


End of Documentation for serialize.ts