Skip to content

Commit 6a5ddb7

Browse files
authored
Added new buffer-string-decorator handler (#1)
* Added new buffer-string-decorator handler * Removed try-catch
1 parent 7bcf098 commit 6a5ddb7

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ const compositeHandler = createHandler({
5656
});
5757
```
5858
59+
### 3. `buffer-string-decorator`
60+
61+
#### Features:
62+
63+
This cache handler converts buffers from cached route values to strings on save and back to buffers on read.
64+
65+
Next 15 decided to change type of data.value.body property from String to Buffer which conflicts with how data is serialized to redis.
66+
67+
It is recommended to use this handler with `redis-strings` in Next 15.
68+
5969
## Reference to Original Package
6070
6171
This package builds upon the core functionality provided by [`@neshca/cache-handler`](https://www.npmjs.com/package/@neshca/cache-handler). You can find more information about the core library, including usage examples and API documentation, at the [official documentation page](https://caching-tools.github.io/next-shared-cache).

packages/nextjs-cache-handler/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "git+https://github.com/fortedigital/nextjs-cache-handler.git"
1212
},
13-
"version": "1.0.1",
13+
"version": "1.1.0",
1414
"type": "module",
1515
"license": "MIT",
1616
"description": "Next.js cache handlers",
@@ -27,6 +27,9 @@
2727
],
2828
"redis-strings": [
2929
"dist/redis-strings.d.ts"
30+
],
31+
"buffer-string-decorator": [
32+
"buffer-string-decorator.d.ts"
3033
]
3134
}
3235
},
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Handler } from "@neshca/cache-handler";
2+
import { CachedRouteValue } from "next/dist/server/response-cache";
3+
4+
type ConvertedStaticPageCacheData = CachedRouteValue & {
5+
body: string;
6+
};
7+
8+
/*
9+
* This cache handler converts buffers from cached route values to strings on save and back to buffers on read.
10+
*
11+
* Next 15 decided to change type of data.value.body property from String to Buffer
12+
* which conflicts with how data is serialized to redis.
13+
*/
14+
export default function bufferStringDecorator(handler: Handler): Handler {
15+
return {
16+
name: "forte-digital-next15-buffer-resolver-handler",
17+
18+
async get(key, ctx) {
19+
const hit = await handler.get(key, ctx);
20+
const staticPageCacheData =
21+
hit?.value as unknown as ConvertedStaticPageCacheData;
22+
if (hit?.value && staticPageCacheData?.body) {
23+
return {
24+
...hit,
25+
value: {
26+
...hit.value,
27+
body: Buffer.from(staticPageCacheData.body, "utf-8"),
28+
},
29+
};
30+
}
31+
return hit;
32+
},
33+
34+
async set(key, data) {
35+
const routeValue = data.value as CachedRouteValue;
36+
if (routeValue?.kind === "ROUTE" && routeValue?.body) {
37+
await handler.set(key, {
38+
...data,
39+
value: {
40+
...data.value,
41+
body: routeValue.body.toString(),
42+
} as ConvertedStaticPageCacheData,
43+
});
44+
} else {
45+
await handler.set(key, data);
46+
}
47+
},
48+
49+
async revalidateTag(tag) {
50+
await handler.revalidateTag(tag);
51+
},
52+
53+
async delete(key) {
54+
await handler.delete?.(key);
55+
},
56+
};
57+
}

packages/nextjs-cache-handler/src/handlers/composite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function createHandler({
3939
}
4040

4141
return {
42-
name: "apopro-composite",
42+
name: "forte-digital-composite",
4343

4444
async get(key, ctx) {
4545
for (const handler of handlers) {

packages/nextjs-cache-handler/src/handlers/redis-strings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export default function createHandler({
195195
const revalidatedTagsKey = keyPrefix + REVALIDATED_TAGS_KEY;
196196

197197
return {
198-
name: "apopro-redis-strings",
198+
name: "forte-digital-redis-strings",
199199
async get(key, { implicitTags }) {
200200
assertClientIsReady();
201201

0 commit comments

Comments
 (0)