@@ -10,57 +10,64 @@ Starting from version `2.0.0`, this package no longer depends on `@neshca/cache-
10
10
- [ 1.x.x → ^2.x.x] ( docs\migration\1_x_x__2_x_x.md )
11
11
- [ 1.2.x -> ^1.3.x] ( docs\migration\1_2_x__1_3_x.md )
12
12
13
- ---
14
-
15
13
## Installation
16
14
17
- If upgrading from Next 14 or earlier, ** flush your Redis cache** . Cache formats between Next 14 and 15 are incompatible.
15
+ ` npm i @fortedigital/nextjs- cache-handler `
18
16
19
- ---
17
+ If upgrading from Next 14 or earlier, ** flush your Redis cache ** before running new version of the application locally and on your hosted environments. ** Cache formats between Next 14 and 15 are incompatible ** .
20
18
21
19
## Next 15 Support
22
20
23
- ` @neshca/cache-handler ` does not support Next.js 15. Prior to 2.0.0, this package provided wrappers and enhancements.
24
- From version 2.0.0 onward, ` @fortedigital/nextjs-cache-handler ` is a standalone solution with no dependency on ` @neshca/cache-handler ` .
25
- ` @fortedigital/nextjs-cache-handler ` is fully compatible with Next.js 15 and [ redis 5] ( https://www.npmjs.com/package/redis ) .
21
+ The original ` @neshca/cache-handler ` package does not support Next.js 15.
22
+
23
+ Prior to 2.0.0, this package provided wrappers and enhancements to allow using ` @neshca/cache-handler ` with Next.js 15.
24
+ From version 2.0.0 onward, ` @fortedigital/nextjs-cache-handler ` is a standalone solution with no dependency on ` @neshca/cache-handler ` and is fully compatible with Next.js 15 and [ redis 5] ( https://www.npmjs.com/package/redis ) .
26
25
27
26
We aim to keep up with new Next.js releases and will introduce major changes with appropriate version bumps.
28
27
29
- ---
28
+ ### Swapping from ` @neshca/cache-handler `
29
+
30
+ If you already use ` @neshca/cache-handler ` the setup is very streamlined and you just need to replace package references. If you're starting fresh please check [ the example project] ( ./examples/redis-minimal ) .
30
31
31
- ### Revalidate Fetch Breaking Change
32
+ #### Cache handler
32
33
33
34
** Before:**
34
35
35
36
``` js
36
- const { CacheHandler } = require (" @neshca/cache-handler" );
37
+ // cache-handler.mjs
38
+
39
+ import { CacheHandler } from " @neshca/cache-handler" ;
37
40
38
41
CacheHandler .onCreation (() => {
39
42
// setup
40
43
});
41
44
42
- module . exports = CacheHandler;
45
+ export default CacheHandler ;
43
46
```
44
47
45
48
** After:**
46
49
47
50
``` js
48
- const { CacheHandler } = require (" @fortedigital/nextjs-cache-handler" );
51
+ // cache-handler.mjs
52
+
53
+ import { CacheHandler } from " @fortedigital/nextjs-cache-handler" ;
49
54
50
55
CacheHandler .onCreation (() => {
51
56
// setup
52
57
});
53
58
54
- module . exports = CacheHandler;
59
+ export default CacheHandler ;
55
60
```
56
61
57
62
---
58
63
59
- ### Instrumentation Update
64
+ #### Instrumentation
60
65
61
66
** Before:**
62
67
63
68
``` js
69
+ // instrumentation.ts
70
+
64
71
export async function register () {
65
72
if (process .env .NEXT_RUNTIME === " nodejs" ) {
66
73
const { registerInitialCache } = await import (
@@ -75,6 +82,8 @@ export async function register() {
75
82
** After:**
76
83
77
84
``` js
85
+ // instrumentation.ts
86
+
78
87
export async function register () {
79
88
if (process .env .NEXT_RUNTIME === " nodejs" ) {
80
89
const { registerInitialCache } = await import (
@@ -86,8 +95,6 @@ export async function register() {
86
95
}
87
96
```
88
97
89
- ---
90
-
91
98
## Handlers
92
99
93
100
### ` redis-strings `
@@ -160,8 +167,6 @@ const compositeHandler = createCompositeHandler({
160
167
});
161
168
` ` `
162
169
163
- ---
164
-
165
170
### ⚠️ ` buffer- string- decorator` | **REMOVED IN 2.0.0!** - integrated into the core package
166
171
167
172
#### Features:
@@ -185,8 +190,6 @@ const bufferStringDecorator =
185
190
createBufferStringDecoratorHandler (redisCacheHandler);
186
191
` ` `
187
192
188
- ---
189
-
190
193
## Examples
191
194
192
195
### 2.x.x
@@ -198,30 +201,33 @@ const bufferStringDecorator =
198
201
#### Example ` cache- handler .js ` .
199
202
200
203
` ` ` js
201
- const { createClient } = require (" redis" );
202
- const { PHASE_PRODUCTION_BUILD } = require (" next/constants" );
203
- const createCompositeHandler =
204
- require (" @fortedigital/nextjs-cache-handler/composite" ).default ;
205
- const createRedisHandler =
206
- require (" @fortedigital/nextjs-cache-handler/redis-strings" ).default ;
207
- const createLruHandler =
208
- require (" @fortedigital/nextjs-cache-handler/local-lru" ).default ;
209
- const { CacheHandler } = require (" @fortedigital/nextjs-cache-handler" );
204
+ import { createClient } from " redis" ;
205
+ import { PHASE_PRODUCTION_BUILD } from " next/constants.js" ;
206
+ import { CacheHandler } from " @fortedigital/nextjs-cache-handler" ;
207
+ import createLruHandler from " @fortedigital/nextjs-cache-handler/local-lru" ;
208
+ import createRedisHandler from " @fortedigital/nextjs-cache-handler/redis-strings" ;
209
+ import createCompositeHandler from " @fortedigital/nextjs-cache-handler/composite" ;
210
210
211
211
CacheHandler .onCreation (() => {
212
+ // Important - It's recommended to use global scope to ensure only one Redis connection is made
213
+ // This ensures only one instance get created
212
214
if (global .cacheHandlerConfig ) {
213
215
return global .cacheHandlerConfig ;
214
216
}
215
217
218
+ // Important - It's recommended to use global scope to ensure only one Redis connection is made
219
+ // This ensures new instances are not created in a race condition
216
220
if (global .cacheHandlerConfigPromise ) {
217
221
return global .cacheHandlerConfigPromise ;
218
222
}
219
223
224
+ // You may need to ignore Redis locally, remove this block otherwise
220
225
if (process .env .NODE_ENV === " development" ) {
221
226
const lruCache = createLruHandler ();
222
227
return { handlers: [lruCache] };
223
228
}
224
229
230
+ // Main promise initializing the handler
225
231
global .cacheHandlerConfigPromise = (async () => {
226
232
let redisClient = null ;
227
233
@@ -231,6 +237,7 @@ CacheHandler.onCreation(() => {
231
237
pingInterval: 10000 ,
232
238
};
233
239
240
+ // This is optional and needed only if you use access keys
234
241
if (process .env .REDIS_ACCESS_KEY ) {
235
242
settings .password = process .env .REDIS_ACCESS_KEY ;
236
243
}
@@ -282,11 +289,13 @@ CacheHandler.onCreation(() => {
282
289
283
290
global .cacheHandlerConfigPromise = null ;
284
291
292
+ // This example uses composite handler to switch from Redis to LRU cache if tags contains `memory-cache` tag.
293
+ // You can skip composite and use Redis or LRU only.
285
294
global .cacheHandlerConfig = {
286
295
handlers: [
287
296
createCompositeHandler ({
288
297
handlers: [lruCache, redisCacheHandler],
289
- setStrategy : (ctx ) => (ctx? .tags .includes (" memory-cache" ) ? 0 : 1 ),
298
+ setStrategy : (ctx ) => (ctx? .tags .includes (" memory-cache" ) ? 0 : 1 ), // You can adjust strategy for deciding which cache should the composite use
290
299
}),
291
300
],
292
301
};
@@ -297,28 +306,25 @@ CacheHandler.onCreation(() => {
297
306
return global .cacheHandlerConfigPromise ;
298
307
});
299
308
300
- module . exports = CacheHandler;
309
+ exports default CacheHandler;
301
310
` ` `
302
311
303
312
### 1.x.x
304
313
305
314
` ` ` js
306
315
// @neshca/cache-handler dependencies
307
- const { CacheHandler } = require ( " @neshca/cache-handler" ) ;
308
- const createLruHandler = require ( " @neshca/cache-handler/local-lru" ). default ;
316
+ import { CacheHandler } from " @neshca/cache-handler" ;
317
+ import createLruHandler from " @neshca/cache-handler/local-lru" ;
309
318
310
319
// Next/Redis dependencies
311
- const { createClient } = require ( " redis" ) ;
312
- const { PHASE_PRODUCTION_BUILD } = require ( " next/constants" ) ;
320
+ import { createClient } from " redis" ;
321
+ import { PHASE_PRODUCTION_BUILD } from " next/constants" ;
313
322
314
323
// @fortedigital/nextjs-cache-handler dependencies
315
- const createCompositeHandler =
316
- require (" @fortedigital/nextjs-cache-handler/composite" ).default ;
317
- const createRedisHandler =
318
- require (" @fortedigital/nextjs-cache-handler/redis-strings" ).default ;
319
- const createBufferStringHandler =
320
- require (" @fortedigital/nextjs-cache-handler/buffer-string-decorator" ).default ;
321
- const { Next15CacheHandler } = require (" @fortedigital/nextjs-cache-handler" );
324
+ import createCompositeHandler from " @fortedigital/nextjs-cache-handler/composite" ;
325
+ import createRedisHandler from " @fortedigital/nextjs-cache-handler/redis-strings" ;
326
+ import createBufferStringHandler from " @fortedigital/nextjs-cache-handler/buffer-string-decorator" ;
327
+ import { Next15CacheHandler } from " @fortedigital/nextjs-cache-handler" ;
322
328
323
329
// Usual onCreation from @neshca/cache-handler
324
330
CacheHandler .onCreation (() => {
@@ -421,7 +427,7 @@ CacheHandler.onCreation(() => {
421
427
return global .cacheHandlerConfigPromise ;
422
428
});
423
429
424
- module . exports = Next15CacheHandler ;
430
+ export default CacheHandler ;
425
431
` ` `
426
432
427
433
---
0 commit comments