Skip to content

Commit 1569021

Browse files
committed
Added buffer conversion for rscData and segmentData
1 parent 1a65ca3 commit 1569021

File tree

3 files changed

+107
-31
lines changed

3 files changed

+107
-31
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.1.4",
13+
"version": "1.2.0-alpha",
1414
"type": "module",
1515
"license": "MIT",
1616
"description": "Next.js cache handlers",

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

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Handler } from "@neshca/cache-handler";
2-
import { CachedRouteValue } from "next/dist/server/response-cache";
3-
4-
type ConvertedStaticPageCacheData = CachedRouteValue & {
5-
body: string;
6-
};
2+
import {
3+
CachedAppPageValue,
4+
CachedRouteValue,
5+
ConvertedCachedAppPageValue,
6+
ConvertedCachedRouteValue,
7+
} from "./buffer-string-decorator.types";
78

89
/*
910
* This cache handler converts buffers from cached route values to strings on save and back to buffers on read.
@@ -17,37 +18,89 @@ export default function bufferStringDecorator(handler: Handler): Handler {
1718

1819
async get(key, ctx) {
1920
const hit = await handler.get(key, ctx);
20-
const staticPageCacheData =
21-
hit?.value as unknown as ConvertedStaticPageCacheData;
22-
if (
23-
hit?.value &&
24-
(staticPageCacheData?.kind as string) === "APP_ROUTE" &&
25-
staticPageCacheData?.body
26-
) {
27-
return {
28-
...hit,
29-
value: {
30-
...hit.value,
31-
body: Buffer.from(staticPageCacheData.body, "utf-8"),
32-
},
33-
};
21+
22+
if (!hit?.value) {
23+
return hit;
24+
}
25+
26+
const value = hit.value;
27+
const kind = value?.kind as string;
28+
29+
if (kind === "APP_ROUTE") {
30+
const appRouteData = value as unknown as ConvertedCachedRouteValue;
31+
32+
if (appRouteData?.body) {
33+
// Convert body string to Buffer
34+
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L97
35+
36+
const appRouteValue = value as unknown as CachedRouteValue;
37+
appRouteValue.body = Buffer.from(appRouteData.body, "utf-8");
38+
}
39+
} else if (kind === "APP_PAGE") {
40+
const appPageData = value as unknown as ConvertedCachedAppPageValue;
41+
const appPageValue = value as unknown as CachedAppPageValue;
42+
43+
if (appPageData.rscData) {
44+
// Convert rscData string to Buffer
45+
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L76
46+
47+
appPageValue.rscData = Buffer.from(appPageData.rscData, "utf-8");
48+
}
49+
50+
if (appPageData.segmentData) {
51+
// Convert segmentData Record<string, string> to Map<string, Buffer>
52+
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L80
53+
54+
appPageValue.segmentData = new Map(
55+
Object.entries(appPageData.segmentData).map(([key, value]) => [
56+
key,
57+
Buffer.from(value, "utf-8"),
58+
]),
59+
);
60+
}
3461
}
62+
3563
return hit;
3664
},
3765

3866
async set(key, data) {
39-
const routeValue = data.value as unknown as CachedRouteValue;
40-
if ((routeValue?.kind as string) === "APP_ROUTE" && routeValue?.body) {
41-
await handler.set(key, {
42-
...data,
43-
value: {
44-
...data.value,
45-
body: routeValue.body.toString(),
46-
} as ConvertedStaticPageCacheData,
47-
});
48-
} else {
49-
await handler.set(key, data);
67+
const value = data.value;
68+
const kind = value?.kind as string;
69+
70+
if (kind === "APP_ROUTE") {
71+
const appRouteData = value as unknown as ConvertedCachedRouteValue;
72+
const appRouteValue = value as unknown as CachedRouteValue;
73+
74+
if (appRouteValue?.body) {
75+
// Convert body Buffer to string
76+
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L97
77+
78+
appRouteData.body = appRouteValue.body.toString();
79+
}
80+
} else if (kind === "APP_PAGE") {
81+
const appPageData = value as unknown as ConvertedCachedAppPageValue;
82+
const appPageValue = value as unknown as CachedAppPageValue;
83+
84+
if (appPageValue?.rscData) {
85+
// Convert rscData string to Buffer
86+
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L76
87+
88+
appPageData.rscData = appPageValue.rscData.toString();
89+
}
90+
91+
if (appPageValue?.segmentData) {
92+
// Convert segmentData Record<string, string> to Map<string, Buffer>
93+
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L80
94+
95+
appPageData.segmentData = Object.fromEntries(
96+
Array.from(appPageValue.segmentData.entries()).map(
97+
([key, value]) => [key, value.toString()],
98+
),
99+
);
100+
}
50101
}
102+
103+
await handler.set(key, data);
51104
},
52105

53106
async revalidateTag(tag) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export type CachedRouteValue = {
2+
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L97
3+
kind: "APP_ROUTE";
4+
body: Buffer | undefined;
5+
};
6+
7+
export type ConvertedCachedRouteValue = {
8+
kind: "ROUTE";
9+
body: string | undefined;
10+
};
11+
12+
export type CachedAppPageValue = {
13+
// See: https://github.com/vercel/next.js/blob/f5444a16ec2ef7b82d30048890b613aa3865c1f1/packages/next/src/server/response-cache/types.ts#L76
14+
kind: "APP_PAGE";
15+
rscData: Buffer | undefined;
16+
segmentData: Map<string, Buffer> | undefined;
17+
};
18+
19+
export type ConvertedCachedAppPageValue = {
20+
kind: "PAGE";
21+
rscData: string | undefined;
22+
segmentData: Record<string, string> | undefined;
23+
};

0 commit comments

Comments
 (0)