Skip to content

Commit a0df985

Browse files
committed
Improve handling of origin requests
1 parent bd69ed7 commit a0df985

File tree

10 files changed

+107
-98
lines changed

10 files changed

+107
-98
lines changed

index.mjs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async function graphqlHandler(request, env, ctx) {
6666
// default headers
6767
const responseOptions = {
6868
headers: {
69-
'content-type': 'application/json;charset=UTF-8',
69+
'Content-Type': 'application/json',
7070
}
7171
};
7272

@@ -116,7 +116,11 @@ async function graphqlHandler(request, env, ctx) {
116116
if (env.ORIGIN_OVERRIDE) {
117117
originUrl.host = env.ORIGIN_OVERRIDE;
118118
}
119-
const queryResult = await fetchWithTimeout(originUrl, {
119+
if (env.ORIGIN_PROTOCOL) {
120+
originUrl.protocol = env.ORIGIN_PROTOCOL;
121+
}
122+
console.log(`Querying origin server ${originUrl}`);
123+
const originResponse = await fetchWithTimeout(originUrl, {
120124
method: 'POST',
121125
body: JSON.stringify({
122126
query,
@@ -128,11 +132,11 @@ async function graphqlHandler(request, env, ctx) {
128132
},
129133
timeout: 20000
130134
});
131-
if (queryResult.status !== 200) {
132-
throw new Error(`${queryResult.status} ${await queryResult.text()}`);
135+
if (originResponse.status !== 200) {
136+
throw new Error(`${originResponse.status} ${await originResponse.text()}`);
133137
}
134138
console.log('Request served from origin server');
135-
return new Response(await queryResult.text(), responseOptions);
139+
return new Response(originResponse.body, responseOptions);
136140
} catch (error) {
137141
console.error(`Error getting response from origin server: ${error}`);
138142
}

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-lite-api.mjs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ export async function getLiteApiResponse(request, url, env, serverContext) {
5151
key = await cacheMachine.createKey(env, url.pathname, { q, lang, gameMode, uid, tags, sort, sort_direction });
5252
const cachedResponse = await cacheMachine.get(env, {key});
5353
if (cachedResponse) {
54-
// Construct a new response with the cached data
55-
const newResponse = new Response(cachedResponse);
56-
// Add a custom 'X-CACHE: HIT' header so we know the request hit the cache
57-
newResponse.headers.append('X-CACHE', 'HIT');
5854
console.log(`Request served from cache: ${new Date() - requestStart} ms`);
59-
// Return the new cached response
6055
request.cached = true;
61-
return newResponse;
56+
// Construct a new response with the cached data
57+
58+
return new Response(cachedResponse, {
59+
headers: {
60+
'X-CACHE': 'HIT',
61+
}
62+
});
6263
} else {
6364
console.log('no cached response');
6465
}

plugins/plugin-nightbot.mjs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ export async function getNightbotResponse(request, url, env, serverContext) {
4242
key = await cacheMachine.createKey(env, 'nightbot', { q: query, l: lang, m: gameMode });
4343
const cachedResponse = await cacheMachine.get(env, {key});
4444
if (cachedResponse) {
45-
// Construct a new response with the cached data
46-
const newResponse = new Response(cachedResponse);
47-
// Add a custom 'X-CACHE: HIT' header so we know the request hit the cache
48-
newResponse.headers.append('X-CACHE', 'HIT');
4945
console.log(`Request served from cache: ${new Date() - requestStart} ms`);
50-
// Return the new cached response
5146
request.cached = true;
52-
return newResponse;
47+
return new Response(cachedResponse, {
48+
headers: {
49+
'X-CACHE': 'HIT',
50+
}
51+
});
5352
} else {
5453
console.log('no cached response');
5554
}

0 commit comments

Comments
 (0)