Skip to content

Commit bb550af

Browse files
authored
Merge pull request #381 from the-hideout/dont-enforce-content-type
don't enforce content type being set
2 parents d200de2 + ca91cd7 commit bb550af

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

index.mjs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import fetchWithTimeout from './utils/fetch-with-timeout.mjs';
2929
import { getNightbotResponse, useNightbotOnUrl } from './plugins/plugin-nightbot.mjs';
3030
import { getTwitchResponse } from './plugins/plugin-twitch.mjs';
3131
import { getLiteApiResponse, useLiteApiOnUrl } from './plugins/plugin-lite-api.mjs';
32+
import { getSpecialCache } from './plugins/plugin-use-cache-machine.mjs';
3233

3334
let dataAPI;
3435

@@ -84,11 +85,7 @@ async function graphqlHandler(request, env, ctx) {
8485
//return new Response(JSON.stringify({}), responseOptions);
8586
}
8687

87-
let specialCache = '';
88-
const contentType = request.headers.get('content-type');
89-
if (!contentType || !contentType.startsWith('application/json')) {
90-
specialCache = 'application/json';
91-
}
88+
const specialCache = getSpecialCache(request);
9289

9390
let key;
9491
// Check the cache service for data first - If cached data exists, return it

plugins/plugin-use-cache-machine.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import cacheMachine from '../utils/cache-machine.mjs';
22
import setCors from '../utils/setCors.mjs';
33

4-
function specialCache(request) {
4+
export function getSpecialCache(request) {
55
const contentType = request.headers.get('content-type');
6-
if (!contentType || !contentType.startsWith('application/json')) {
7-
return 'application/json';
6+
if (request.method === 'POST' && !contentType?.startsWith('application/json')) {
7+
//return 'application/json'; // don't enforce content type
88
}
99
return undefined;
1010
}
@@ -22,7 +22,7 @@ export default function useCacheMachine(env) {
2222
console.log(`Skipping cache check already performed by worker`);
2323
return;
2424
}
25-
const cachedResponse = await cacheMachine.get(env, {query: params.query, variables: params.variables, specialCache: specialCache(request)});
25+
const cachedResponse = await cacheMachine.get(env, {query: params.query, variables: params.variables, specialCache: getSpecialCache(request)});
2626
if (cachedResponse) {
2727
console.log('Request served from cache');
2828
request.cached = true;
@@ -82,7 +82,7 @@ export default function useCacheMachine(env) {
8282

8383
let ttl = request.data?.getRequestTtl(request.requestId) ?? 60 * 5;
8484

85-
const sCache = specialCache(request);
85+
const sCache = getSpecialCache(request);
8686
if (result.errors?.some(err => err.message === 'Unexpected error.')) {
8787
ttl = 0;
8888
} else if (result.errors?.some(err => err.message.startsWith('Syntax Error'))) {

0 commit comments

Comments
 (0)