Building the App Without Redis Cache Connection
There are scenarios, especially during deployment, where your Redis server may not be immediately available. This can occur when the Redis server and your app are deployed simultaneously. In such cases, it’s important to configure your build process to handle the absence of the Redis server. Here’s how you can adapt your setup to build the app without requiring an active connection to the Redis server:
Configuration Steps
-
Modify the
cache-handler.mjs
File: Adjust theonCreation
method in yourcache-handler.mjs
file to conditionally connect to the Redis server. The modified code should look like this:cache-handler.mjsCacheHandler.onCreation(async () => { let handler; if (process.env.REDIS_AVAILABLE) { await client.connect(); handler = await createRedisHandler({ client, }); } return { handlers: [handler], }; });
-
Environment Variable Check: The
process.env.REDIS_AVAILABLE
environment variable is used to determine if the server has already started. This ensures that the Redis client attempts a connection only when the server is up and running. You may use a different environment variable or a more sophisticated check based on your deployment setup.