CF Workers - Empty Body to origin servers
Less than 1% of records of passed through CF workers sending empty body to origin servers and a leading to 422 and at times 524 errors. Please help us where we are doing mistake.
1 Reply
function rewriteUrlPath(url: URL): string {
// Rewrite path for primary domain
if (url.host === 'api.example.com' && url.pathname.startsWith('/api/v2/')) {
url.pathname = url.pathname.replace('/api/v2/', '/v1/');
}
// Rewrite paths for subdomains
if (url.host.startsWith('service.')) {
['/api/v2/', '/v1/', '/service/'].forEach((prefix) => {
if (url.pathname.startsWith(prefix)) {
url.pathname = url.pathname.replace(prefix, '/api/');
}
});
}
return url.toString();
}
export default {
async fetch(
request: Request,
env: {
PRIMARY_DOMAIN: string;
ALTERNATE_DOMAIN: string;
},
ctx: ExecutionContext,
): Promise<Response> {
ctx.passThroughOnException();
const url = new URL(request.url);
const params = new URLSearchParams(url.search);
const featureFlag = params.get('feature_enabled');
const isFeatureEnabled =
url.pathname.indexOf('/items/') > 0 &&
['y', '1', 'yes', 'true'].includes(featureFlag);
if (!isFeatureEnabled && url.host === env.PRIMARY_DOMAIN) {
// No feature flag and no need to rewrite domain, pass it to the origin
return fetch(request);
}
if (isFeatureEnabled) {
// Override domain if feature is enabled
url.host = env.ALTERNATE_DOMAIN || url.host;
} else {
url.host = env.PRIMARY_DOMAIN;
}
const headers = request.headers;
const method = request.method;
const baseParams = { headers, method };
const reqParams = ['get', 'head'].includes(request.method.toLowerCase())
? baseParams
: {
...baseParams,
body: await request.text(),
};
const newRequest = new Request(rewriteUrlPath(url), reqParams);
return fetch(newRequest);
},
};
function rewriteUrlPath(url: URL): string {
// Rewrite path for primary domain
if (url.host === 'api.example.com' && url.pathname.startsWith('/api/v2/')) {
url.pathname = url.pathname.replace('/api/v2/', '/v1/');
}
// Rewrite paths for subdomains
if (url.host.startsWith('service.')) {
['/api/v2/', '/v1/', '/service/'].forEach((prefix) => {
if (url.pathname.startsWith(prefix)) {
url.pathname = url.pathname.replace(prefix, '/api/');
}
});
}
return url.toString();
}
export default {
async fetch(
request: Request,
env: {
PRIMARY_DOMAIN: string;
ALTERNATE_DOMAIN: string;
},
ctx: ExecutionContext,
): Promise<Response> {
ctx.passThroughOnException();
const url = new URL(request.url);
const params = new URLSearchParams(url.search);
const featureFlag = params.get('feature_enabled');
const isFeatureEnabled =
url.pathname.indexOf('/items/') > 0 &&
['y', '1', 'yes', 'true'].includes(featureFlag);
if (!isFeatureEnabled && url.host === env.PRIMARY_DOMAIN) {
// No feature flag and no need to rewrite domain, pass it to the origin
return fetch(request);
}
if (isFeatureEnabled) {
// Override domain if feature is enabled
url.host = env.ALTERNATE_DOMAIN || url.host;
} else {
url.host = env.PRIMARY_DOMAIN;
}
const headers = request.headers;
const method = request.method;
const baseParams = { headers, method };
const reqParams = ['get', 'head'].includes(request.method.toLowerCase())
? baseParams
: {
...baseParams,
body: await request.text(),
};
const newRequest = new Request(rewriteUrlPath(url), reqParams);
return fetch(newRequest);
},
};