@@ -64,13 +64,6 @@ async function graphqlHandler(request, env, ctx) {
64
64
) ;
65
65
}
66
66
67
- // default headers
68
- const responseOptions = {
69
- headers : {
70
- 'content-type' : 'application/json;charset=UTF-8' ,
71
- }
72
- } ;
73
-
74
67
if ( ! dataAPI ) {
75
68
dataAPI = new DataSource ( env ) ;
76
69
}
@@ -95,9 +88,13 @@ async function graphqlHandler(request, env, ctx) {
95
88
const cachedResponse = await cacheMachine . get ( env , { key} ) ;
96
89
if ( cachedResponse ) {
97
90
// Construct a new response with the cached data
98
- const newResponse = new Response ( cachedResponse , responseOptions ) ;
99
- // Add a custom 'X-CACHE: HIT' header so we know the request hit the cache
100
- newResponse . headers . append ( 'X-CACHE' , 'HIT' ) ;
91
+ const newResponse = new Response ( await cachedResponse . json ( ) , {
92
+ headers : {
93
+ 'Content-Type' : 'application/json' ,
94
+ 'X-CACHE' : 'HIT' , // we know request hit the cache
95
+ 'Cache-Control' : `public, max-age=${ cachedResponse . headers . get ( 'X-Cache-Ttl' ) } ` ,
96
+ } ,
97
+ } ) ;
101
98
console . log ( 'Request served from cache' ) ;
102
99
// Return the new cached response
103
100
return newResponse ;
@@ -113,7 +110,11 @@ async function graphqlHandler(request, env, ctx) {
113
110
if ( env . ORIGIN_OVERRIDE ) {
114
111
originUrl . host = env . ORIGIN_OVERRIDE ;
115
112
}
116
- const queryResult = await fetchWithTimeout ( originUrl , {
113
+ if ( env . ORIGIN_PROTOCOL ) {
114
+ originUrl . protocol = env . ORIGIN_PROTOCOL ;
115
+ }
116
+ console . log ( `Querying origin server ${ originUrl } ` ) ;
117
+ const originResponse = await fetchWithTimeout ( originUrl , {
117
118
method : 'POST' ,
118
119
body : JSON . stringify ( {
119
120
query,
@@ -125,11 +126,19 @@ async function graphqlHandler(request, env, ctx) {
125
126
} ,
126
127
timeout : 20000
127
128
} ) ;
128
- if ( queryResult . status !== 200 ) {
129
- throw new Error ( `${ queryResult . status } ${ await queryResult . text ( ) } ` ) ;
129
+ if ( originResponse . status !== 200 ) {
130
+ throw new Error ( `${ originResponse . status } ${ await originResponse . text ( ) } ` ) ;
130
131
}
131
132
console . log ( 'Request served from origin server' ) ;
132
- return new Response ( await queryResult . text ( ) , responseOptions ) ;
133
+ const newResponse = new Response ( originResponse . body , {
134
+ headers : {
135
+ 'Content-Type' : 'application/json' ,
136
+ } ,
137
+ } ) ;
138
+ if ( originResponse . headers . has ( 'X-Cache-Ttl' ) ) {
139
+ newResponse . headers . set ( 'Cache-Control' , `public, max-age=${ originResponse . headers . get ( 'X-Cache-Ttl' ) } ` ) ;
140
+ }
141
+ return newResponse ;
133
142
} catch ( error ) {
134
143
console . error ( `Error getting response from origin server: ${ error } ` ) ;
135
144
}
@@ -177,7 +186,14 @@ async function graphqlHandler(request, env, ctx) {
177
186
178
187
const body = JSON . stringify ( result ) ;
179
188
180
- const response = new Response ( body , responseOptions ) ;
189
+ const response = new Response ( body , {
190
+ headers : {
191
+ 'Content-Type' : 'application/json' ,
192
+ } ,
193
+ } ) ;
194
+ if ( ttl > 0 ) {
195
+ response . headers . set ( 'Cache-Control' , `public, max-age=${ ttl } ` ) ;
196
+ }
181
197
182
198
if ( env . SKIP_CACHE !== 'true' && ttl > 0 ) {
183
199
key = key ?? await cacheMachine . createKey ( env , query , variables , specialCache ) ;
0 commit comments