Skip to content

Commit 60bb9eb

Browse files
committed
Redis upgrade
1 parent 1530e20 commit 60bb9eb

19 files changed

+2492
-479
lines changed

.LICENSE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ MIT License
22

33
This extension is developed and maintained by Forte Digital. For more information about our products and services, visit [our website](https://fortedigital.com).
44

5-
Copyright (c) 2024 Forte_ Digital and Arseny Kruglikov
5+
This package was built on top of [`@neshca/cache-handler`](https://www.npmjs.com/package/@neshca/cache-handler).
6+
7+
Copyright (c) 2024 Forte\_ Digital and Arseny Kruglikov
68

79
Permission is hereby granted, free of charge, to any person obtaining a copy
810
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 53 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,50 @@
22

33
# @fortedigital/nextjs-cache-handler
44

5-
This package extends the functionality of [`@neshca/cache-handler`](https://www.npmjs.com/package/@neshca/cache-handler) by providing additional cache handlers for specialized use cases, specifically for Redis-based caching solutions. The original `@neshca/cache-handler` offers a robust caching API for Next.js applications, and this package introduces two new handlers for managing Redis cache with different expiration strategies and tag-based revalidation.
6-
5+
This package was initially built on top of [`@neshca/cache-handler`](https://www.npmjs.com/package/@neshca/cache-handler) by providing additional cache handlers for specialized use cases, specifically for Redis-based caching solutions. The original `@neshca/cache-handler` stopped getting updated for Next 15 and above. This package addresses compatibility issues with Next 15 and onwards.
76

87
## Migration
98

10-
### 1.2.x -> 1.3.x
9+
### 1.x.x -> 2.x.x
10+
11+
1.x.x
12+
13+
```
14+
const { Next15CacheHandler } = require("@fortedigital/nextjs-cache-handler/next-15-cache-handler");
15+
module.exports = new Next15CacheHandler();
16+
```
17+
18+
2.x.x or higher
19+
20+
```
21+
const { CacheHandler } = require("@fortedigital/nextjs-cache-handler");
22+
module.exports = CacheHandler;
23+
```
24+
25+
####
26+
27+
### 1.2.x -> ^1.3.x
1128

1229
#### cache-handler
30+
1331
1.2.x
32+
1433
```
1534
const { Next15CacheHandler } = require("@fortedigital/nextjs-cache-handler/next-15-cache-handler");
1635
module.exports = new Next15CacheHandler();
1736
```
1837

19-
1.3.x
38+
^1.3.x
39+
2040
```
2141
const { Next15CacheHandler } = require("@fortedigital/nextjs-cache-handler");
2242
module.exports = Next15CacheHandler;
2343
```
2444

2545
#### instrumentation
46+
2647
1.2.x
48+
2749
```
2850
if (process.env.NEXT_RUNTIME === "nodejs") {
2951
const { registerInitialCache } = await import('@neshca/cache-handler/instrumentation')
@@ -32,7 +54,8 @@ if (process.env.NEXT_RUNTIME === "nodejs") {
3254
}
3355
```
3456

35-
1.3.x
57+
^1.3.x
58+
3659
```
3760
if (process.env.NEXT_RUNTIME === "nodejs") {
3861
const { registerInitialCache } = await import("@fortedigital/nextjs-cache-handler/instrumentation");
@@ -43,60 +66,34 @@ if (process.env.NEXT_RUNTIME === "nodejs") {
4366

4467
## Installation
4568

46-
To install this package along with its dependencies:
47-
48-
```bash
49-
npm install @fortedigital/nextjs-cache-handler
50-
```
51-
52-
Package depends on the original `@neshca/cache-handler` package - you can use anything provided by it by using import/require from `@neshca/cache-handler`.
53-
54-
## Next 15 Support
69+
## Next 15 Support and migration from `@neshca/cache-handler`
5570

5671
As `@neshca/cache-handler` does not officially support Next 15+ yet, we try to keep up with Next and prepare more or less temporary workarounds. At some point we will either create a fork of `@neshca/cache-handler` to fully support Next 15 or it gets updated by the maintainers. As for now we're building a set of decorators/workarounds you can use to build cache solutions for Next 15. We might need to do a full-blown rework which will be marked with a proper major version upgrade.
5772

58-
### String buffer breaking change
59-
60-
If you use Redis Strings cache handler with Next15+ you need to decorate the default Redis String handler with a buffer converter like this:
61-
62-
```
63-
// ...
64-
const redisCacheHandler = createRedisHandler({
65-
client: redisClient,
66-
keyPrefix: "nextjs:",
67-
});
68-
69-
return {
70-
handlers: [
71-
createBufferStringHandler(redisCacheHandler)
72-
]
73-
}
74-
75-
// ...
76-
```
77-
78-
Read more about this in Handlers section below.
79-
8073
### Revalidate fetch breaking change
8174

8275
Instead of:
8376

8477
```js
8578
const { CacheHandler } = require("@neshca/cache-handler");
79+
80+
CacheHandler.onCreation(() => {
81+
// your usual setup
82+
});
83+
8684
module.exports = CacheHandler;
8785
```
8886

8987
Use this:
9088

9189
```js
92-
const { CacheHandler } = require("@neshca/cache-handler");
93-
const { Next15CacheHandler } = require("@fortedigital/nextjs-cache-handler");
90+
const { CacheHandler } = require("@fortedigital/nextjs-cache-handler");
9491

9592
CacheHandler.onCreation(() => {
9693
// your usual setup
9794
});
9895

99-
module.exports = Next15CacheHandler;
96+
module.exports = CacheHandler;
10097
```
10198

10299
### Instrumentation
@@ -105,23 +102,27 @@ Instead of:
105102

106103
```js
107104
export async function register() {
108-
if (process.env.NEXT_RUNTIME === 'nodejs') {
109-
const { registerInitialCache } = await import('@neshca/cache-handler/instrumentation');
110-
const CacheHandler = (await import('../cache-handler.mjs')).default;
111-
await registerInitialCache(CacheHandler);
112-
}
105+
if (process.env.NEXT_RUNTIME === "nodejs") {
106+
const { registerInitialCache } = await import(
107+
"@neshca/cache-handler/instrumentation"
108+
);
109+
const CacheHandler = (await import("../cache-handler.mjs")).default;
110+
await registerInitialCache(CacheHandler);
111+
}
113112
}
114113
```
115114

116115
Use this:
117116

118117
```js
119118
export async function register() {
120-
if (process.env.NEXT_RUNTIME === 'nodejs') {
121-
const { registerInitialCache } = await import('@fortedigital/nextjs-cache-handler/instrumentation');
122-
const CacheHandler = (await import('../cache-handler.mjs')).default;
123-
await registerInitialCache(CacheHandler);
124-
}
119+
if (process.env.NEXT_RUNTIME === "nodejs") {
120+
const { registerInitialCache } = await import(
121+
"@fortedigital/nextjs-cache-handler/instrumentation"
122+
);
123+
const CacheHandler = (await import("../cache-handler.mjs")).default;
124+
await registerInitialCache(CacheHandler);
125+
}
125126
}
126127
```
127128

@@ -167,26 +168,10 @@ const compositeHandler = createHandler({
167168
});
168169
```
169170
170-
### 3. `buffer-string-decorator`
171-
172-
#### Features:
173-
174-
This cache handler converts buffers from cached route values to strings on save and back to buffers on read.
175-
176-
Next 15 decided to change types of some properties from String to Buffer which conflicts with how data is serialized to redis. It is recommended to use this handler with `redis-strings` in Next 15 as this handler make the following adjustment.
177-
178-
- **Converts `body` `Buffer` to `string`**
179-
See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L97
180-
- **Converts `rscData` `string` to `Buffer`**
181-
See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L76
182-
- **Converts `segmentData` `Record<string, string>` to `Map<string, Buffer>`**
183-
See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L80
184-
185171
## Full example
186172
187173
```js
188174
// @neshca/cache-handler dependencies
189-
const { CacheHandler } = require("@neshca/cache-handler");
190175
const createLruHandler = require("@neshca/cache-handler/local-lru").default;
191176

192177
// Next/Redis dependencies
@@ -200,9 +185,7 @@ const createRedisHandler =
200185
require("@fortedigital/nextjs-cache-handler/redis-strings").default;
201186
const createBufferStringHandler =
202187
require("@fortedigital/nextjs-cache-handler/buffer-string-decorator").default;
203-
const {
204-
Next15CacheHandler,
205-
} = require("@fortedigital/nextjs-cache-handler");
188+
const { CacheHandler } = require("@fortedigital/nextjs-cache-handler");
206189

207190
// Usual onCreation from @neshca/cache-handler
208191
CacheHandler.onCreation(() => {
@@ -305,7 +288,7 @@ CacheHandler.onCreation(() => {
305288
return global.cacheHandlerConfigPromise;
306289
});
307290

308-
module.exports = Next15CacheHandler;
291+
module.exports = CacheHandler;
309292
```
310293
311294
## Reference to Original Package
@@ -314,4 +297,4 @@ This package builds upon the core functionality provided by [`@neshca/cache-hand
314297
315298
## License
316299
317-
This project is licensed under the [MIT License](./LICENSE), as is the original `@neshca/cache-handler` package.
300+
This project is licensed under the [MIT License](./LICENSE), as was the original `@neshca/cache-handler` package.

0 commit comments

Comments
 (0)