Use an out-of-process editing data cache with Vercel deployments
This topic is relevant if you're using the older, chromes-based method to integrate with Pages. It is also relevant if you are using the preview mode, which is still chromes-based, in the newer, newer, metadata-based integration added in JSS 22.1.
If you intend to use your Vercel deployment as an editing host for Sitecore, you need to specify an out-of-process editing cache implementation for the EditingDataMiddleware middleware/handler. You can use either the Vercel KV solution that Sitecore provides out of the box or add a custom implementation using Redis caching. This topic provides information about both approaches.
Use a Vercel KV cache
Sitecore JSS SDK v21.4.0 provides an out-of-the-box solution using Vercel KV out-of-process caching.
To enable the Vercel KV caching solution:
-
Enable Vercel KV on your Vercel project:
ImportantYou must set values for the
KV_REST_API_URLand theKV_REST_API_TOKENvariables in your.envfile before building and running your app locally. -
In the
src/libdirectory, create a file (for example,redis-editing-service.ts), to initialize and export the editing data service. -
Import and initialize the
VercelEditingDataCacheservice, and then import theBasicEditingDataServiceservice and initialize it with an instance ofredisDataCache: -
In the
src/pages/api/editing/render.tsfile, import and pass theredisDataServiceservice into theEditingRenderMiddlewarehandler: -
In the
src/lib/page-props-factory/plugins/preview-mode.tsfile, modify theeditingDataServiceimport to use the Vercel KV implementation instead:
Use a custom Redis cache endpoint
You can use non-Vercel Redis storage for the editing data cache. This example shows an implementation using the Node Redis library, but you can use the client of your choice.
To set up a custom Redis cache endpoint:
-
Use one of the following commands to add the
@redis/clientdependency to your project:or
-
In the
src/libdirectory, create a file (for example,redis-editing-service.ts) to initialize and export the editing data service. -
Create and export a class that implements
EditingDataCache. The class must be able to put key(string)-value(JSON) data into Redis and retrieve it later.The following example uses a Redis client that is initialized using an endpoint URL only, without specifying a username or password.
-
In the
src/pages/api/editing/render.tsfile, import and passredisDataServiceinto theEditingRenderMiddlewarehandler: -
In the
src/lib/page-props-factory/plugins/preview-mode.tsfile, modify theeditingDataServiceimport to use the Redis KV implementation: