Revert "x"

This reverts commit 9e6a2ba4fc.
This commit is contained in:
Omar Najjar 2025-04-30 19:45:42 +10:00
parent 9e6a2ba4fc
commit 29936e8365

View file

@ -31,12 +31,16 @@ function handleOptions(request) {
// Helper to create standardized responses
function createResponse(body, status = 200, cacheDuration = 60*60*24) {
// Convert body to string if it's an object
const bodyString = typeof body === 'object' ? JSON.stringify(body) : String(body);
// Don't allow null or undefined body
if (body === null || body === undefined) {
console.error('createResponse called with null or undefined body');
body = { error: 'Empty response body' };
status = 500;
}
const headers = {
'Content-Type': 'application/json',
...corsHeaders
...corsHeaders // Add CORS headers
};
if (cacheDuration > 0) {
@ -46,7 +50,12 @@ function createResponse(body, status = 200, cacheDuration = 60*60*24) {
headers['Cache-Control'] = 'no-store';
}
return new Response(bodyString, {
// Log the response details for debugging
console.log(`Creating response: status=${status}, headers=${JSON.stringify(headers)}, body type=${typeof body}`);
// Create a standard Response object
// This ensures properties like 'ok' are properly set based on status code
return new Response(JSON.stringify(body), {
status,
headers
});
@ -82,22 +91,17 @@ function getCacheControl(path) {
export default {
async fetch(request, env, ctx) {
try {
// Always handle OPTIONS requests first for CORS
if (request.method === 'OPTIONS') {
return handleOptions(request);
}
const url = new URL(request.url);
const path = url.pathname;
// Add proper CORS headers to all responses
const responseHeaders = new Headers({
...corsHeaders
});
console.log(`Received request for path: ${path}`);
console.log(`Full URL: ${url.toString()}`);
// Handle CORS preflight requests
if (request.method === 'OPTIONS') {
return handleOptions();
}
// Set cache control headers based on path
const cacheControl = getCacheControl(path);
const headers = new Headers();
@ -162,22 +166,14 @@ export default {
// Return 404 for unknown routes
return new Response('Not Found', { status: 404 });
// Make sure error responses also include CORS headers
ctx.passThroughOnException();
} catch (error) {
console.error('Unhandled error:', error);
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
status: 500,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
return new Response('Internal Server Error', { status: 500 });
}
}
};
// ... existing code ...
// Handle media files (memes and audio)
async function serveMedia(request, env) {
@ -272,6 +268,7 @@ async function serveStaticAsset(request, env) {
}
}
// ... existing code ...
async function serveMainPage(request, env) {
try {