This guide explains how to migrate from @contentstack/delivery-sdk v4.x to v5, including breaking changes and required update steps.
You need to update your code only if you use SDK caching with a policy other than IGNORE_CACHE.
Version 5.0.0 of the TypeScript Delivery SDK changes how cache persistence is handled.
Instead, the SDK now:
This design keeps the core SDK lightweight and gives you full control over where and how cached data is stored, such as:
Caching policies themselves remain unchanged from v4.x.
| Area | v4.x | v5.x | Migration Required? |
| Cache storage | SDK could accept storeType (e.g. 'localStorage') in cacheOptions and bundle persistence logic. | SDK does not accept storeType in cacheOptions. You must pass a persistenceStore instance that implements the PersistenceStore interface. | Yes (if caching is used) |
| Persistence implementation | Persistence logic could be bundled inside the SDK. | Persistence is external: use @contentstack/persistence-plugin or implement the PersistenceStore interface yourself. | Yes (if caching is used) |
| Cache policies | Same policies: IGNORE_CACHE, CACHE_THEN_NETWORK, CACHE_ELSE_NETWORK, NETWORK_ELSE_CACHE. | Same. If you use any policy other than IGNORE_CACHE without cacheOptions.persistenceStore, the SDK throws at stack creation. | No |
All other SDK APIs (stack creation, content types, entries, assets, queries, regions, live preview, etc.) remain the same. Only the cache configuration is affected.
npm install @contentstack/delivery-sdk@5Or with yarn:
yarn add @contentstack/delivery-sdk@5| Your setup | Action |
| You never configured cacheOptions | No changes required |
| You use Policy.IGNORE_CACHE | No changes required |
| You use any other cache policy such as CACHE_THEN_NETWORK, CACHE_ELSE_NETWORK, or NETWORK_ELSE_CACHE | Continue with the steps below |
If you use a cache policy other than IGNORE_CACHE without setting cacheOptions.persistenceStore, stack creation fails with:
Error: Cache policy requires cacheOptions.persistenceStore (storage). Install @contentstack/persistence-plugin and pass a PersistenceStore instance and try again.
Fix: Install @contentstack/persistence-plugin, create a PersistenceStore instance, and pass it as cacheOptions.persistenceStore as shown above.
Install the official persistence plugin so the SDK has a concrete store to use:
npm install @contentstack/persistence-plugin
Or with yarn:
yarn add @contentstack/persistence-plugin
Note:
v4.x (no longer valid in v5):
import contentstack, { Policy } from '@contentstack/delivery-sdk';
const stack = contentstack.stack({
apiKey: 'your-api-key',
deliveryToken: 'your-delivery-token',
environment: 'your-environment',
cacheOptions: {
policy: Policy.CACHE_THEN_NETWORK,
storeType: 'localStorage', // ❌ Not supported in v5
maxAge: 86400000,
},
});v5.x (correct):
import contentstack, { Policy } from '@contentstack/delivery-sdk';
import { PersistenceStore } from '@contentstack/persistence-plugin';
const stack = contentstack.stack({
apiKey: 'your-api-key',
deliveryToken: 'your-delivery-token',
environment: 'your-environment',
cacheOptions: {
policy: Policy.CACHE_THEN_NETWORK,
persistenceStore: new PersistenceStore({
storeType: 'localStorage',
maxAge: 86400000,
}),
maxAge: 86400000,
},
});About maxAge
If both values are provided:
You can omit cacheOptions.maxAge if the persistence store handles expiration internally.
Important Cache Configuration Rules
The SDK requires an object that implements the PersistenceStore interface exported from the SDK.
PersistenceStore Interface
interface PersistenceStore {
setItem(key: string, value: any, contentTypeUid?: string, maxAge?: number): void;
getItem(key: string, contentTypeUid?: string): any;
}You can implement this interface yourself—for example, using an in-memory store, Redis, or any key-value store and pass it to cacheOptions.persistenceStore without installing a plugin.
Example: In-Memory Persistence Store
import contentstack, { Policy, PersistenceStore } from '@contentstack/delivery-sdk';
const memoryStore: PersistenceStore = {
_cache: new Map(),
setItem(key, value, _contentTypeUid?, _maxAge?) {
this._cache.set(key, value);
},
getItem(key, _contentTypeUid?) {
return this._cache.get(key);
},
};
const stack = contentstack.stack({
apiKey: 'your-api-key',
deliveryToken: 'your-delivery-token',
environment: 'your-environment',
cacheOptions: {
policy: Policy.CACHE_THEN_NETWORK,
persistenceStore: memoryStore,
},
});No changes. Just upgrade to v5.
const stack = contentstack.stack({
apiKey: 'your-api-key',
deliveryToken: 'your-delivery-token',
environment: 'your-environment',
});No persistenceStore is required. No changes beyond upgrading.
If you don’t provide persistenceStore, the SDK throws at stack creation with a message directing you to install the persistence plugin and pass a PersistenceStore instance.
Symptom
When you use a cache policy other than IGNORE_CACHE and do not set cacheOptions.persistenceStore, stack creation fails with the following error:
Cause
The SDK requires a persistence store whenever you use any cache policy other than IGNORE_CACHE.
Resolution
Use this list to confirm you have completed the migration steps:
Use these checks to validate that your migration is successful:
If you need to revert to v4.x:
For more on the persistence plugin (options like storeType, maxAge, custom storage), see @contentstack/persistence-plugin.