Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions examples/redis-cache-components/cache-handler.js

This file was deleted.

27 changes: 27 additions & 0 deletions examples/redis-cache-components/cache-handler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createClient } from "redis";
import { PHASE_PRODUCTION_BUILD } from "next/constants.js";
import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings-cache-components";

const client = createClient({
url: process.env.REDIS_URL || "redis://localhost:6379",
});

client.on("error", (err) => {
if (process.env.NEXT_PRIVATE_DEBUG_CACHE !== undefined) {
console.error("Redis Client Error", err);
}
});

// Only connect to Redis outside of `next build` — Redis is not available
// during the build phase. The handler checks client.isReady on each method
// call, so an unconnected client at build time is safe.
if (process.env.NEXT_PHASE !== PHASE_PRODUCTION_BUILD) {
client.connect().catch((err) => {
console.warn("Redis connection failed:", err.message);
});
}
Comment on lines +15 to +22
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not really solve the problem of handler saving to redis at built time. It only causes failures, the cache handler is still registered, still calls redis methods.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this is exactly why the npm run build does not work.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check how PHASE_PRODUCTION_BUILD is used in cache-handler.ts to properly implement skipping redis calls at built time.


export default createRedisHandler({
client,
keyPrefix: process.env.CACHE_PREFIX || "",
});
4 changes: 2 additions & 2 deletions examples/redis-cache-components/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { NextConfig } from "next";
const nextConfig: NextConfig = {
cacheComponents: true,
cacheHandlers: {
default: require.resolve("./cache-handler.js"),
remote: require.resolve("./cache-handler.js"),
default: require.resolve("./cache-handler.mjs"),
remote: require.resolve("./cache-handler.mjs"),
},
};

Expand Down
3 changes: 3 additions & 0 deletions packages/nextjs-cache-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
"redis-strings": [
"dist/handlers/redis-strings.d.ts"
],
"redis-strings-cache-components": [
"dist/handlers/redis-strings-cache-components.d.ts"
],
"local-lru": [
"dist/handlers/local-lru.d.ts"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
export interface CacheComponentsEntry {
value: ReadableStream<Uint8Array>;
tags: string[];
stale: number;
timestamp: number;
expire: number;
revalidate: number;
value: ReadableStream<Uint8Array>;
tags: string[];
stale: number;
timestamp: number;
expire: number;
revalidate: number;
}

export interface CacheComponentsHandler {
get(
cacheKey: string,
softTags: string[],
): Promise<CacheComponentsEntry | undefined>;
set(
cacheKey: string,
pendingEntry: Promise<CacheComponentsEntry>,
): Promise<void>;
refreshTags(): Promise<void>;
getExpiration(tags: string[]): Promise<number>;
updateTags(tags: string[], durations?: { expire?: number }): Promise<void>;
}
get(
cacheKey: string,
softTags: string[],
): Promise<CacheComponentsEntry | undefined>;
set(
cacheKey: string,
pendingEntry: Promise<CacheComponentsEntry>,
): Promise<void>;
refreshTags(): Promise<void>;
getExpiration(tags: string[]): Promise<number>;
updateTags(tags: string[], durations?: { expire?: number }): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export type CacheHandlerParametersGetWithTags = [
* Context information provided during cache set operations.
*/
export type SetContext = {

/**
* When true, the cache write should only occur if the key does not already exist.
* Cache handlers should use non-destructive write operations (e.g., Redis NX)
Expand Down Expand Up @@ -154,7 +153,11 @@ export type Handler = {
*
* Use the absolute time (`expireAt`) to set and expiration time for the cache entry in your cache store to be in sync with the file system cache.
*/
set: (key: string, value: CacheHandlerValue, ctx?: SetContext) => Promise<void>;
set: (
key: string,
value: CacheHandlerValue,
ctx?: SetContext,
) => Promise<void>;
/**
* Deletes all cache entries that are associated with the specified tag.
* See [fetch `options.next.tags` and `revalidateTag` ↗](https://nextjs.org/docs/app/building-your-application/caching#fetch-optionsnexttags-and-revalidatetag)
Expand Down
Loading