Skip to content

Commit 47848fc

Browse files
committed
Fixed #10 bufferStringDecorator mutates original data
1 parent 2a5464f commit 47848fc

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

packages/nextjs-cache-handler/package.json

Lines changed: 1 addition & 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.3.0-canary4",
13+
"version": "1.3.0-canary5",
1414
"type": "module",
1515
"license": "MIT",
1616
"description": "Next.js cache handlers",

packages/nextjs-cache-handler/src/handlers/buffer-string-decorator.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { Handler } from "@neshca/cache-handler";
1+
import { CacheHandlerValue, Handler } from "@neshca/cache-handler";
22
import {
33
CachedAppPageValue,
44
CachedRouteValue,
55
ConvertedCachedAppPageValue,
66
ConvertedCachedRouteValue,
77
} from "./buffer-string-decorator.types";
8+
import { IncrementalCacheValue } from "next/dist/server/response-cache";
9+
import { Next15IncrementalCacheValue } from "./next15.types";
810

911
/*
1012
* This cache handler converts buffers from cached route values to strings on save and back to buffers on read.
@@ -64,7 +66,12 @@ export default function bufferStringDecorator(handler: Handler): Handler {
6466
},
6567

6668
async set(key, data) {
67-
const value = data.value;
69+
if (!data?.value) {
70+
await handler.set(key, data);
71+
return;
72+
}
73+
74+
const value = { ...data.value };
6875
const kind = value?.kind as string;
6976

7077
if (kind === "APP_ROUTE") {
@@ -100,7 +107,7 @@ export default function bufferStringDecorator(handler: Handler): Handler {
100107
}
101108
}
102109

103-
await handler.set(key, data);
110+
await handler.set(key, { ...data, value });
104111
},
105112

106113
async revalidateTag(tag) {

packages/nextjs-cache-handler/src/handlers/next-15-cache-handler.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
11
import { CacheHandler } from "@neshca/cache-handler";
2-
import { OutgoingHttpHeaders } from "http";
32
import {
4-
CachedRedirectValue,
5-
CachedImageValue,
63
CachedFetchValue,
74
IncrementalCacheValue,
85
} from "next/dist/server/response-cache";
9-
10-
export interface CachedRouteValue {
11-
kind: "APP_ROUTE";
12-
body: Buffer;
13-
status: number;
14-
headers: OutgoingHttpHeaders;
15-
}
16-
17-
interface IncrementalCachedPageValue {
18-
kind: "APP_PAGE";
19-
html: string;
20-
pageData: Object;
21-
postponed: string | undefined;
22-
headers: OutgoingHttpHeaders | undefined;
23-
status: number | undefined;
24-
}
25-
26-
export type Next15IncrementalCacheValue =
27-
| CachedRedirectValue
28-
| IncrementalCachedPageValue
29-
| CachedImageValue
30-
| CachedFetchValue
31-
| CachedRouteValue;
6+
import { Next15IncrementalCacheValue } from "./next15.types";
327

338
/*
349
* Use this handler in Next 15.2.1 and higher after `revalidate` property had been removed from context object.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
CachedRedirectValue,
3+
CachedImageValue,
4+
CachedFetchValue,
5+
} from "next/dist/server/response-cache";
6+
import { OutgoingHttpHeaders } from "http";
7+
8+
export interface CachedRouteValue {
9+
kind: "APP_ROUTE";
10+
body: Buffer;
11+
status: number;
12+
headers: OutgoingHttpHeaders;
13+
}
14+
15+
export interface IncrementalCachedPageValue {
16+
kind: "APP_PAGE";
17+
html: string;
18+
pageData: Object;
19+
postponed: string | undefined;
20+
headers: OutgoingHttpHeaders | undefined;
21+
status: number | undefined;
22+
}
23+
24+
export type Next15IncrementalCacheValue =
25+
| CachedRedirectValue
26+
| IncrementalCachedPageValue
27+
| CachedImageValue
28+
| CachedFetchValue
29+
| CachedRouteValue;

0 commit comments

Comments
 (0)