update cache handler version in example (#65330)

### 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>
This commit is contained in:
David Sa 2024-05-13 17:58:55 -04:00 committed by GitHub
parent 47769d14bc
commit 7725047c89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 5 deletions

View file

@ -1,7 +1,8 @@
const { IncrementalCache } = require("@neshca/cache-handler");
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",
@ -11,7 +12,7 @@ client.on("error", (error) => {
console.error("Redis error:", error.message);
});
IncrementalCache.onCreation(async () => {
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;
@ -21,7 +22,7 @@ IncrementalCache.onCreation(async () => {
let redisCache;
if (process.env.REDIS_AVAILABLE) {
if (PHASE_PRODUCTION_BUILD !== process.env.NEXT_PHASE) {
await client.connect();
redisCache = await createRedisCache({
@ -41,4 +42,4 @@ IncrementalCache.onCreation(async () => {
};
});
module.exports = IncrementalCache;
module.exports = CacheHandler;

View file

@ -11,7 +11,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@neshca/cache-handler": "^0.6",
"@neshca/cache-handler": "^1.3.1",
"@neshca/json-replacer-reviver": "^1",
"@types/node": "^20",
"@types/react": "^18",