Skip to content

Commit 7e3a2df

Browse files
committed
Readme
1 parent f6f7594 commit 7e3a2df

File tree

1 file changed

+49
-43
lines changed

1 file changed

+49
-43
lines changed

README.md

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,64 @@ Starting from version `2.0.0`, this package no longer depends on `@neshca/cache-
1010
- [1.x.x → ^2.x.x](docs\migration\1_x_x__2_x_x.md)
1111
- [1.2.x -> ^1.3.x](docs\migration\1_2_x__1_3_x.md)
1212

13-
---
14-
1513
## Installation
1614

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`
1816

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**.
2018

2119
## Next 15 Support
2220

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).
2625

2726
We aim to keep up with new Next.js releases and will introduce major changes with appropriate version bumps.
2827

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).
3031

31-
### Revalidate Fetch Breaking Change
32+
#### Cache handler
3233

3334
**Before:**
3435

3536
```js
36-
const { CacheHandler } = require("@neshca/cache-handler");
37+
// cache-handler.mjs
38+
39+
import { CacheHandler } from "@neshca/cache-handler";
3740

3841
CacheHandler.onCreation(() => {
3942
// setup
4043
});
4144

42-
module.exports = CacheHandler;
45+
export default CacheHandler;
4346
```
4447

4548
**After:**
4649

4750
```js
48-
const { CacheHandler } = require("@fortedigital/nextjs-cache-handler");
51+
// cache-handler.mjs
52+
53+
import { CacheHandler } from "@fortedigital/nextjs-cache-handler";
4954

5055
CacheHandler.onCreation(() => {
5156
// setup
5257
});
5358

54-
module.exports = CacheHandler;
59+
export default CacheHandler;
5560
```
5661

5762
---
5863

59-
### Instrumentation Update
64+
#### Instrumentation
6065

6166
**Before:**
6267

6368
```js
69+
// instrumentation.ts
70+
6471
export async function register() {
6572
if (process.env.NEXT_RUNTIME === "nodejs") {
6673
const { registerInitialCache } = await import(
@@ -75,6 +82,8 @@ export async function register() {
7582
**After:**
7683

7784
```js
85+
// instrumentation.ts
86+
7887
export async function register() {
7988
if (process.env.NEXT_RUNTIME === "nodejs") {
8089
const { registerInitialCache } = await import(
@@ -86,8 +95,6 @@ export async function register() {
8695
}
8796
```
8897

89-
---
90-
9198
## Handlers
9299

93100
### `redis-strings`
@@ -160,8 +167,6 @@ const compositeHandler = createCompositeHandler({
160167
});
161168
```
162169
163-
---
164-
165170
### ⚠️ `buffer-string-decorator` | **REMOVED IN 2.0.0!** - integrated into the core package
166171
167172
#### Features:
@@ -185,8 +190,6 @@ const bufferStringDecorator =
185190
createBufferStringDecoratorHandler(redisCacheHandler);
186191
```
187192
188-
---
189-
190193
## Examples
191194
192195
### 2.x.x
@@ -198,30 +201,33 @@ const bufferStringDecorator =
198201
#### Example `cache-handler.js`.
199202
200203
```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";
210210

211211
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
212214
if (global.cacheHandlerConfig) {
213215
return global.cacheHandlerConfig;
214216
}
215217

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
216220
if (global.cacheHandlerConfigPromise) {
217221
return global.cacheHandlerConfigPromise;
218222
}
219223

224+
// You may need to ignore Redis locally, remove this block otherwise
220225
if (process.env.NODE_ENV === "development") {
221226
const lruCache = createLruHandler();
222227
return { handlers: [lruCache] };
223228
}
224229

230+
// Main promise initializing the handler
225231
global.cacheHandlerConfigPromise = (async () => {
226232
let redisClient = null;
227233

@@ -231,6 +237,7 @@ CacheHandler.onCreation(() => {
231237
pingInterval: 10000,
232238
};
233239

240+
// This is optional and needed only if you use access keys
234241
if (process.env.REDIS_ACCESS_KEY) {
235242
settings.password = process.env.REDIS_ACCESS_KEY;
236243
}
@@ -282,11 +289,13 @@ CacheHandler.onCreation(() => {
282289

283290
global.cacheHandlerConfigPromise = null;
284291

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.
285294
global.cacheHandlerConfig = {
286295
handlers: [
287296
createCompositeHandler({
288297
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
290299
}),
291300
],
292301
};
@@ -297,28 +306,25 @@ CacheHandler.onCreation(() => {
297306
return global.cacheHandlerConfigPromise;
298307
});
299308

300-
module.exports = CacheHandler;
309+
exports default CacheHandler;
301310
```
302311
303312
### 1.x.x
304313
305314
```js
306315
// @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";
309318

310319
// 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";
313322

314323
// @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";
322328

323329
// Usual onCreation from @neshca/cache-handler
324330
CacheHandler.onCreation(() => {
@@ -421,7 +427,7 @@ CacheHandler.onCreation(() => {
421427
return global.cacheHandlerConfigPromise;
422428
});
423429

424-
module.exports = Next15CacheHandler;
430+
export default CacheHandler;
425431
```
426432
427433
---

0 commit comments

Comments
 (0)