From 62d4d8f2da0992b18e2b475b5410a7cf33dd2343 Mon Sep 17 00:00:00 2001 From: Omar Najjar Date: Wed, 2 Apr 2025 21:52:18 +1100 Subject: [PATCH] x --- index.html | 7 +++- worker.js | 113 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 73 insertions(+), 47 deletions(-) diff --git a/index.html b/index.html index 53baddd..66e1240 100644 --- a/index.html +++ b/index.html @@ -266,8 +266,11 @@ function smartCopyShare(proposalId, proposalText) { } } // Configuration - const API_BASE_URL = 'https://theradicalparty.com/api'; // Replace with your actual worker URL - + const API_BASE_URL = + window.location.hostname === 'localhost' + ? 'http://localhost:8787/api' + : 'https://theradicalparty.com/api'; + // Character limit const CHAR_LIMIT = 280; diff --git a/worker.js b/worker.js index e12729e..35145f5 100644 --- a/worker.js +++ b/worker.js @@ -86,53 +86,76 @@ export default { } // Handle styles.css route - if (path === '/styles.css') { - try { - // Fetch CSS from GitHub Pages - const response = await fetch('https://radical.pages.dev/radical/styles.css', { - headers: { - 'User-Agent': request.headers.get('User-Agent') || 'Cloudflare Worker' - } - }); - - if (!response.ok) { - console.error('Failed to fetch styles.css', response.status, response.statusText); - return new Response('CSS File Not Found', { - status: 404, - headers: corsHeaders - }); - } - - // Get the CSS content - const cssContent = await response.text(); - - // Return CSS response with caching headers - const cssResponse = new Response(cssContent, { - status: 200, - headers: { - 'Content-Type': 'text/css', - 'Cache-Control': 'public, max-age=86400', // 1 day - ...corsHeaders // Add CORS headers - } - }); +if (path === '/styles.css') { + try { + // Determine the correct source based on the request's origin + const cssSourceUrl = + request.headers.get('Origin')?.includes('localhost') + ? 'http://localhost:3000/styles.css' + : 'https://radical.pages.dev/radical/styles.css'; - // Store in cache if this is a GET request - if (request.method === "GET") { - const cacheKey = new Request(request.url, { - headers: request.headers - }); - ctx.waitUntil(caches.default.put(cacheKey, cssResponse.clone())); - } - - return cssResponse; - } catch (error) { - console.error('Error fetching styles.css:', error); - return new Response('Internal Server Error', { - status: 500, - headers: corsHeaders - }); - } + // Fetch CSS + const response = await fetch(cssSourceUrl, { + headers: { + 'User-Agent': request.headers.get('User-Agent') || 'Cloudflare Worker' } + }); + + if (!response.ok) { + console.error('Failed to fetch styles.css', response.status, response.statusText); + return new Response('CSS File Not Found', { + status: 404, + headers: { + 'Content-Type': 'text/css', + ...corsHeaders + } + }); + } + + // Get the CSS content + const cssContent = await response.text(); + + // Prepare CORS headers specific to the requesting origin + const requestOrigin = request.headers.get('Origin') || '*'; + const specificCorsHeaders = { + 'Content-Type': 'text/css', + 'Access-Control-Allow-Origin': requestOrigin, + 'Access-Control-Allow-Methods': 'GET', + 'Access-Control-Max-Age': '86400', + 'Cache-Control': 'public, max-age=86400' // 1 day cache + }; + + // Create CSS response with precise headers + const cssResponse = new Response(cssContent, { + status: 200, + headers: specificCorsHeaders + }); + + // Store in cache if this is a GET request + if (request.method === "GET") { + const cacheKey = new Request(request.url, { + headers: request.headers + }); + ctx.waitUntil(caches.default.put(cacheKey, cssResponse.clone())); + } + + return cssResponse; + } catch (error) { + console.error('Comprehensive styles.css fetch error:', { + message: error.message, + stack: error.stack, + name: error.name + }); + + return new Response('Failed to load stylesheet', { + status: 500, + headers: { + 'Content-Type': 'text/css', + ...corsHeaders + } + }); + } +} const isTestMeta = url.searchParams.get('test_meta') === 'true';