Skip to content

Commit

Permalink
update cache handler version in example (#65330)
Browse files Browse the repository at this point in the history
### 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>
  • Loading branch information
davidsa03 and ijjk committed May 13, 2024
1 parent 47769d1 commit 7725047
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions examples/cache-handler-redis/cache-handler.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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;
Expand All @@ -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({
Expand All @@ -41,4 +42,4 @@ IncrementalCache.onCreation(async () => {
};
});

module.exports = IncrementalCache;
module.exports = CacheHandler;
2 changes: 1 addition & 1 deletion examples/cache-handler-redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7725047

Please sign in to comment.