What is the name of the domain?
What is the error number?
TypeError: Failed to fetch
What is the error message?
Access to fetch at ‘https://home-line-shop-backend.antmwes104-1.workers.dev/api/checkout/initiate’ from origin ‘https://shop-home-line-academy-github.pages.dev’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
What is the issue you’re encountering
My Cloudflare Pages site (shop-home-line-academy-github.pages.dev) cannot make fetch POST requests to my Cloudflare Worker (home-line-shop-backend.antmwes104-1.workers.dev). The browser blocks the request due to a CORS preflight (OPTIONS) failure, specifically citing that the Access-Control-Allow-Origin header is missing from the preflight response.
What steps have you taken to resolve the issue?
- Verified via wrangler tail that the Worker does receive the OPTIONS request and executes the handling code without crashing (logs show “Ok” status and console logs within the handler run).
- Implemented explicit OPTIONS request handling within the Worker’s main fetch handler (before routing) to return new Response(null, { status: 204, headers: {…} }) with hardcoded Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers. Tried setting Origin to both * and the specific Pages domain.
- Used itty-router with router.options(‘*’, …) to handle preflights.
- Created and enabled a Cloudflare Transform Rule (HTTP Response Header Modification) for the Worker hostname to “Set static” the required Access-Control-Allow-Origin, …-Methods, and …-Headers. Tried setting Origin to both * and the specific Pages domain.
- Tried combinations of explicit worker handling and the Transform Rule.
- Verified necessary environment variables (FRONTEND_BASE_URL secret in Worker, VITE_WORKER_API_ENDPOINT variable in Pages) are correctly set and deployed.
- Ensured latest code for worker and pages frontend was deployed.
What are the steps to reproduce the issue?
- Deploy the Worker code (latest version from previous chat attempts) to home-line-shop-backend.antmwes104-1.workers.dev.
- Deploy the Pages frontend code (Vite/React version that fetches from the worker) to shop-home-line-academy-github.pages.dev, ensuring VITE_WORKER_API_ENDPOINT environment variable points to the worker URL.
- Navigate to the /checkout page on the deployed Pages site.
- Open browser developer tools (Console and Network tabs).
- Attempt to submit the checkout form (which triggers a fetch POST to the worker’s /api/checkout/initiate endpoint).
- Observe the CORS error in the browser console and the failed preflight (OPTIONS) request in the Network tab. Observe wrangler tail logs showing the OPTIONS request being received by the worker without crashing.