Registry / storage / apollo3-cache-persist

apollo3-cache-persist

JSON →
library0.15.0jsnpmunverified

Simple persistence for all Apollo Client 3 cache implementations, including InMemoryCache and Hermes. Version 0.15.0 supports web and React Native with multiple storage providers (AsyncStorage, localStorage, etc.). It restores cache asynchronously and persists on every write with debounce. Key differentiators: works with any Apollo 3 cache, simple API, TypeScript types shipped.

npm install apollo3-cache-persist
INSTALL
IMPORT
SIG · APOLLO3-CACHE-PERS
A
apollo3-cache-persist
storagejavascriptv0.15.0
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 'apollo3-cache-persist'
const persistCache = require('apollo3-cache-persist')
ESM-only; no default export. Use named import.
AsyncStorageWrapper
import { AsyncStorageWrapper } from 'apollo3-cache-persist'
import AsyncStorageWrapper from 'apollo3-cache-persist/AsyncStorageWrapper'
Exported from main entry, not a subpath.
LocalStorageWrapper
import { LocalStorageWrapper } from 'apollo3-cache-persist'
import { LocalstorageWrapper } from 'apollo3-cache-persist'
Case-sensitive: 'LocalStorageWrapper' with camelCase.

Shows how to persist an InMemoryCache to localStorage using apollo3-cache-persist. Must be awaited before creating ApolloClient.

import { InMemoryCache } from '@apollo/client/core'; import { persistCache, LocalStorageWrapper } from 'apollo3-cache-persist'; const cache = new InMemoryCache(); await persistCache({ cache, storage: new LocalStorageWrapper(window.localStorage), }); const client = new ApolloClient({ cache, uri: process.env.GRAPHQL_URI ?? 'http://localhost:4000/graphql', });
Debug
Known issues
breakingVersion 0.14.0 changed persistCache signature to accept an options object instead of multiple arguments.
fix
Use persistCache({ cache, storage, ... }) instead of persistCache(cache, storage, ...)
affects: >=0.14.0
deprecateddeprecated-persistor: The old persistCache() that returned a persistor object is deprecated; use the new persistCache() that returns a promise.
fix
Use the new persistCache() signature; remove any persistor.destroy() calls.
affects: >=0.14.0
gotchaPersist must be awaited before creating ApolloClient, otherwise queries may run before cache is restored.
fix
Always await persistCache() before instantiating ApolloClient.
affects: >=0.14.0
gotchaStorage wrapper must be instantiated with a storage provider; passing the raw storage object will fail.
fix
Use 'new AsyncStorageWrapper(AsyncStorage)' or 'new LocalStorageWrapper(window.localStorage)'
affects: >=0.14.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'read')
Passing raw storage object instead of wrapper instance.
fix
Wrap storage in proper wrapper: new AsyncStorageWrapper(AsyncStorage)
Module not found: Error: Can't resolve 'apollo3-cache-persist' in ...
Package not installed or missing peer dependency @apollo/client.
fix
Run npm install apollo3-cache-persist @apollo/client
Error: persistCache must be awaited before creating ApolloClient
Not awaiting persistCache() call, or using old callback API.
fix
Use await persistCache({ cache, storage: ... });
Upgrade
Version history
0.15.0latest on npm
Audit
Dependencies
@apollo/clientrequiredpeer dependency; required to access cache types and API
Agent activity
40 hits · last 30 days
node
34
Resources