Skip to content

Commit 3dea216

Browse files
authored
Merge pull request #384 from the-hideout/improve-origin-handling
Improve handling of origin requests
2 parents 8f3e3a8 + 145f4a3 commit 3dea216

12 files changed

+156
-113
lines changed

index.mjs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ async function graphqlHandler(request, env, ctx) {
6464
);
6565
}
6666

67-
// default headers
68-
const responseOptions = {
69-
headers: {
70-
'content-type': 'application/json;charset=UTF-8',
71-
}
72-
};
73-
7467
if (!dataAPI) {
7568
dataAPI = new DataSource(env);
7669
}
@@ -95,9 +88,13 @@ async function graphqlHandler(request, env, ctx) {
9588
const cachedResponse = await cacheMachine.get(env, {key});
9689
if (cachedResponse) {
9790
// 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+
});
10198
console.log('Request served from cache');
10299
// Return the new cached response
103100
return newResponse;
@@ -113,7 +110,11 @@ async function graphqlHandler(request, env, ctx) {
113110
if (env.ORIGIN_OVERRIDE) {
114111
originUrl.host = env.ORIGIN_OVERRIDE;
115112
}
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, {
117118
method: 'POST',
118119
body: JSON.stringify({
119120
query,
@@ -125,11 +126,19 @@ async function graphqlHandler(request, env, ctx) {
125126
},
126127
timeout: 20000
127128
});
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()}`);
130131
}
131132
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;
133142
} catch (error) {
134143
console.error(`Error getting response from origin server: ${error}`);
135144
}
@@ -177,7 +186,14 @@ async function graphqlHandler(request, env, ctx) {
177186

178187
const body = JSON.stringify(result);
179188

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+
}
181197

182198
if (env.SKIP_CACHE !== 'true' && ttl > 0) {
183199
key = key ?? await cacheMachine.createKey(env, query, variables, specialCache);

package-lock.json

Lines changed: 66 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"devDependencies": {
2121
"newman": "^6.2.1",
2222
"prettier": "^3.5.3",
23-
"wrangler": "^4.18.0"
23+
"wrangler": "^4.19.2"
2424
},
2525
"dependencies": {
2626
"@graphql-tools/merge": "9.0.24",

plugins/plugin-graphql-origin.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ export default function useGraphQLOrigin(env) {
2424
throw new Error(`${queryResult.status} ${queryResult.statusText}: ${await queryResult.text()}`);
2525
}
2626
console.log('Request served from origin server');
27-
setResult(await queryResult.json());
2827
request.cached = true;
28+
if (queryResult.headers.has('X-Cache-Ttl')) {
29+
request.resultTtl = queryResult.headers.get('X-Cache-Ttl');
30+
}
31+
setResult(await queryResult.json());
2932
} catch (error) {
3033
console.error(`Error getting response from origin server: ${error}`);
3134
}

0 commit comments

Comments
 (0)