Skip to content
Draft
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
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,48 @@ A Redis-based handler for key- and tag-based caching. Compared to the original i
import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings";

const redisHandler = await createRedisHandler({
client,
client: createClient({
url: process.env.REDIS_URL,
}),
keyPrefix: "myApp:",
sharedTagsKey: "myTags",
sharedTagsTtlKey: "myTagTtls",
});
```

#### Redis Cluster (Experimental)

```js
import { createCluster } from "@redis/client";
import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings";
import { withAdapter } from "@fortedigital/nextjs-cache-handler/cluster/adapter";

const { hostname: redisHostName } = new URL(process.env.REDIS_URL);
redis = withAdapter(
createCluster({
rootNodes: [{ url: process.env.REDIS_URL }],

// optional if you use TLS and need to resolve shards' ip to proper hostname
nodeAddressMap(address) {
const [_, port] = address.split(":");

return {
host: redisHostName,
port: Number(port),
};
},
})
);

// after using withAdapter you can use redis cluster instance as parameter for createRedisHandler
const redisCacheHandler = createRedisHandler({
client: redis,
keyPrefix: CACHE_PREFIX,
});
```

**Note:** Redis Cluster support is currently experimental and may have limitations or unexpected bugs. Use it with caution.

---

### `local-lru`
Expand Down Expand Up @@ -186,7 +221,8 @@ Next 15 decided to change types of some properties from String to Buffer which c
```js
import createBufferStringDecoratorHandler from "@fortedigital/nextjs-cache-handler/buffer-string-decorator";

const bufferStringDecorator = createBufferStringDecoratorHandler(redisCacheHandler);
const bufferStringDecorator =
createBufferStringDecoratorHandler(redisCacheHandler);
```

## Examples
Expand Down
3 changes: 3 additions & 0 deletions examples/redis-minimal/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const nextConfig: NextConfig = {
? require.resolve("./cache-handler.mjs")
: undefined,
cacheMaxMemorySize: 0, // disable default in-memory caching
experimental: {
ppr: "incremental",
},
};

export default nextConfig;
96 changes: 48 additions & 48 deletions examples/redis-minimal/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/redis-minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"lint": "next lint"
},
"dependencies": {
"@fortedigital/nextjs-cache-handler": "^2.0.2",
"next": "15.4.3",
"@fortedigital/nextjs-cache-handler": "^2.1.0-canary.11",
"next": "^15.5.1-canary.7",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"redis": "^5.6.1"
Expand Down
33 changes: 33 additions & 0 deletions examples/redis-minimal/src/app/isr-example/blog/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
interface Post {
id: string;
title: string;
content: string;
}

export const revalidate = 3600;

export async function generateStaticParams() {
const posts: Post[] = await fetch("https://api.vercel.app/blog").then((res) =>
res.json()
);
return posts.map((post) => ({
id: String(post.id),
}));
}

export default async function Page({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const post: Post = await fetch(`https://api.vercel.app/blog/${id}`).then(
(res) => res.json()
);
return (
<main>
<h1>{post.title}</h1>
<p>{post.content}</p>
</main>
);
}
20 changes: 20 additions & 0 deletions examples/redis-minimal/src/app/manifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { MetadataRoute } from "next";

export default function manifest(): MetadataRoute.Manifest {
return {
name: "Next.js App",
short_name: "Next.js App",
description: "Next.js App",
start_url: "/",
display: "standalone",
background_color: "#fff",
theme_color: "#fff",
icons: [
{
src: "/favicon.ico",
sizes: "any",
type: "image/x-icon",
},
],
};
}
37 changes: 37 additions & 0 deletions examples/redis-minimal/src/app/ppr-example/Example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export async function Example({
searchParams,
}: {
searchParams: Promise<{ characterId: string }>;
}) {
const characterId = (await searchParams).characterId;
try {
const characterResponse = await fetch(
`https://api.sampleapis.com/futurama/characters/${characterId}`,
{
next: {
revalidate: 86400, // 24 hours in seconds
tags: ["futurama"],
},
}
);
const character = await characterResponse.json();
const name = character.name.first;
return (
<div>
<h1>Name: {name}</h1>
<span>{new Date().toISOString()}</span>
</div>
);
} catch (error) {
console.error("Error fetching character data:", error);
return (
<div>
<span>An error occurred during fetch</span>
</div>
);
}
}

export async function Skeleton() {
return <div>Loading...</div>;
}
Loading