@@ -8,6 +8,8 @@ const NEXT_CACHE_SOFT_TAGS_HEADER = 'x-next-cache-soft-tags';
8
8
9
9
const REQUEST_CONTEXT_KEY = Symbol . for ( '__cloudflare-request-context__' ) ;
10
10
11
+ const CF_NEXT_SUSPENSE_CACHE_HEADER = 'cf-next-suspense-cache' ;
12
+
11
13
/**
12
14
* Handles an internal request to the suspense cache.
13
15
*
@@ -50,14 +52,17 @@ export async function handleSuspenseCacheRequest(request: Request) {
50
52
const data = await cache . get ( cacheKey , { softTags } ) ;
51
53
if ( ! data ) return new Response ( null , { status : 404 } ) ;
52
54
53
- return new Response ( JSON . stringify ( data . value ) , {
54
- status : 200 ,
55
- headers : {
56
- 'Content-Type' : 'application/json' ,
57
- 'x-vercel-cache-state' : 'fresh' ,
58
- age : `${ ( Date . now ( ) - ( data . lastModified ?? Date . now ( ) ) ) / 1000 } ` ,
55
+ return new Response (
56
+ JSON . stringify ( formatCacheValueForResponse ( data . value ) ) ,
57
+ {
58
+ status : 200 ,
59
+ headers : {
60
+ 'Content-Type' : 'application/json' ,
61
+ 'x-vercel-cache-state' : 'fresh' ,
62
+ age : `${ ( Date . now ( ) - ( data . lastModified ?? Date . now ( ) ) ) / 1000 } ` ,
63
+ } ,
59
64
} ,
60
- } ) ;
65
+ ) ;
61
66
}
62
67
case 'POST' : {
63
68
// Retrieve request context.
@@ -124,3 +129,21 @@ async function getInternalCacheAdaptor(
124
129
function getTagsFromHeader ( req : Request , key : string ) : string [ ] | undefined {
125
130
return req . headers . get ( key ) ?. split ( ',' ) ?. filter ( Boolean ) ;
126
131
}
132
+
133
+ function formatCacheValueForResponse ( value : IncrementalCacheValue | null ) {
134
+ switch ( value ?. kind ) {
135
+ case 'FETCH' :
136
+ return {
137
+ ...value ,
138
+ data : {
139
+ ...value . data ,
140
+ headers : {
141
+ ...value . data . headers ,
142
+ [ CF_NEXT_SUSPENSE_CACHE_HEADER ] : 'HIT' ,
143
+ } ,
144
+ } ,
145
+ } ;
146
+ default :
147
+ return value ;
148
+ }
149
+ }
0 commit comments