Registry / storage / apollo4-cache-persist

apollo4-cache-persist

JSON →
library1.0.11jsnpmunverified

Simple persistence for all Apollo Client 3.0 and 4.0 cache implementations, including InMemoryCache and Hermes. Version 1.0.11. Supports web and React Native with multiple storage providers like AsyncStorageWrapper and LocalStorageWrapper. Key differentiator: works with both Apollo Client 3 and 4, simple API with async restore and debounced persistence. Release cadence: active maintenance.

npm install apollo4-cache-persist
INSTALL
IMPORT
SIG · APOLLO4-CACHE-PERS
A
apollo4-cache-persist
storagejavascriptv1.0.11
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

persistCache
import { persistCache } from 'apollo4-cache-persist'
import persistCache from 'apollo4-cache-persist'
Named export, not default. ESM-only, no CommonJS require.
AsyncStorageWrapper
import { AsyncStorageWrapper } from 'apollo4-cache-persist'
import AsyncStorageWrapper from 'apollo4-cache-persist'
Named export, not default. For React Native.
LocalStorageWrapper
import { LocalStorageWrapper } from 'apollo4-cache-persist'
import LocalStorageWrapper from 'apollo4-cache-persist'
Named export, not default. For web.
persistCacheSync
import { persistCacheSync } from 'apollo4-cache-persist'
import { persistCache } from 'apollo4-cache-persist'
For synchronous persistence. Different function from persistCache.

Shows how to persist Apollo cache to localStorage on web. Await persistCache before creating ApolloClient.

import { InMemoryCache } from '@apollo/client/core'; import { persistCache, LocalStorageWrapper } from 'apollo4-cache-persist'; const cache = new InMemoryCache(); await persistCache({ cache, storage: new LocalStorageWrapper(window.localStorage), }); const client = new ApolloClient({ cache, uri: 'https://example.com/graphql', });
Debug
Known issues
breakingThe package is named apollo4-cache-persist but works with Apollo Client 3.0+ and 4.0+. Do not assume it only works with v4.
fix
Check @apollo/client peer dependency: ^3.0.0 || ^4.0.0. Supports both.
affects: >=1.0.0
gotchapersistCache is async. Failing to await it before creating ApolloClient may cause race conditions where queries run before cache is restored.
fix
Always await persistCache() before instantiation of ApolloClient.
affects: >=0.1.0
deprecatedOlder versions used a different API with persistCache(cache, storage) instead of the options object.
fix
Upgrade to v1.0.0+ and use the options object signature: persistCache({ cache, storage }).
affects: <1.0.0
gotchaThe package uses named exports only. Default import will be undefined.
fix
Use named imports: import { persistCache, AsyncStorageWrapper } from 'apollo4-cache-persist'.
affects: >=1.0.0
Errors
Common errors & fixes
`persistCache` is not a function
Default import used instead of named import.
fix
Use import { persistCache } from 'apollo4-cache-persist' instead of import persistCache from 'apollo4-cache-persist'.
Cannot find module 'apollo4-cache-persist' or its corresponding type declarations.
Missing @apollo/client or incorrect TypeScript configuration.
fix
Ensure you have @apollo/client installed as a peer dependency. Also check tsconfig 'moduleResolution': 'node'.
TypeError: AsyncStorage is not a constructor (in React Native)
AsyncStorage should be the imported module, not new AsyncStorage().
fix
Use AsyncStorage from @react-native-async-storage/async-storage directly, not instantiated.
persistCache is not a function (when using require)
CommonJS require is not supported. The package is ESM-only.
fix
Use ES module import: import { ... } from 'apollo4-cache-persist'.
Upgrade
Version history
1.0.11latest on npm
Audit
Dependencies
@apollo/clientrequiredPeer dependency for cache implementations
react-nativeoptionalPeer dependency for React Native storage
rxjsoptionalPeer dependency for reactive programming support
Agent activity
45 hits · last 30 days
node
40
Resources
apollo4-cache-persist — npm install apollo4-cache-persist · libregistry