7725047c89
### What? Update the cache-handler package to the latest and changed logic for opting out of caching during build. ### Why? The current implementation in the cache-handler-redis example requires an environment variable check for `REDIS_AVAILABLE` to determine if the server has already started in order to opt out of caching during build. This update leverages the `NEXT_PHASE` environment variable instead. ### How? This updates the environment variable check to leverage the `NEXT_PHASE` variable so a user doesn't have to manage a new environment variable. --------- Co-authored-by: JJ Kasper <jj@jjsweb.site>
45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
const { CacheHandler } = require("@neshca/cache-handler");
|
|
const createRedisCache = require("@neshca/cache-handler/redis-stack").default;
|
|
const createLruCache = require("@neshca/cache-handler/local-lru").default;
|
|
const { createClient } = require("redis");
|
|
const { PHASE_PRODUCTION_BUILD } = require("next/constants");
|
|
|
|
const client = createClient({
|
|
url: process.env.REDIS_URL ?? "redis://localhost:6379",
|
|
});
|
|
|
|
client.on("error", (error) => {
|
|
console.error("Redis error:", error.message);
|
|
});
|
|
|
|
CacheHandler.onCreation(async () => {
|
|
// read more about TTL limitations https://caching-tools.github.io/next-shared-cache/configuration/ttl
|
|
function useTtl(maxAge) {
|
|
const evictionAge = maxAge * 1.5;
|
|
|
|
return evictionAge;
|
|
}
|
|
|
|
let redisCache;
|
|
|
|
if (PHASE_PRODUCTION_BUILD !== process.env.NEXT_PHASE) {
|
|
await client.connect();
|
|
|
|
redisCache = await createRedisCache({
|
|
client,
|
|
useTtl,
|
|
});
|
|
}
|
|
|
|
const localCache = createLruCache({
|
|
useTtl,
|
|
});
|
|
|
|
return {
|
|
cache: [redisCache, localCache],
|
|
// read more about useFileSystem limitations https://caching-tools.github.io/next-shared-cache/configuration/use-file-system
|
|
useFileSystem: false,
|
|
};
|
|
});
|
|
|
|
module.exports = CacheHandler;
|