1
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
- } ;
2
+ import {
3
+ CachedAppPageValue ,
4
+ CachedRouteValue ,
5
+ ConvertedCachedAppPageValue ,
6
+ ConvertedCachedRouteValue ,
7
+ } from "./buffer-string-decorator.types" ;
7
8
8
9
/*
9
10
* 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 {
17
18
18
19
async get ( key , ctx ) {
19
20
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
+ }
34
61
}
62
+
35
63
return hit ;
36
64
} ,
37
65
38
66
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
+ }
50
101
}
102
+
103
+ await handler . set ( key , data ) ;
51
104
} ,
52
105
53
106
async revalidateTag ( tag ) {
0 commit comments