{"success":true,"name":"understand-cors-before-fighting-it","description":"Browser requests to your API fail with CORS errors in the console.","category":"ai-brimind","path":"ai-brimind/understand-cors-before-fighting-it/SKILL.md","file":"SKILL.md","content":"---\nname: understand-cors-before-fighting-it\ndescription: \"Browser requests to your API fail with CORS errors in the console.\"\n---\n\n# Understand CORS Before Fighting It\n\n## When to use\nProject has a web frontend calling an API on a different origin (domain/port), and requests fail with cross-origin errors or work in one tool but not the browser.\n\n## Example request\nAPI works in Postman/curl but fails in the browser → CORS is a browser-only rule; the server must send Access-Control-Allow-Origin — misunderstanding where CORS lives. Adding allow-origin:* while sending cookies → the browser rejects it; credentials require an exact origin — incompatible combination. Random headers/preflight fixes copy-pasted until something works → nobody knows which part matters; it breaks again on deploy — cargo-cult configuration.\n\n## Instructions\nAct like a developer who explains CORS in one sentence before touching config: the BROWSER blocks the frontend from reading cross-origin responses unless the SERVER explicitly allows that origin. Consequences to apply: (1) CORS errors are fixed on the server (or its proxy), never by frontend code — no fetch option makes the browser skip the check; (2) identify the exact frontend origin (scheme + domain + port; localhost:3000 ≠ [PRIVATE_HOST]) and have the server return it in Access-Control-Allow-Origin — use the framework's CORS middleware rather than hand-setting headers; (3) if requests send cookies/credentials, the wildcard * is forbidden — the server must echo the specific origin and set allow-credentials; (4) non-simple requests (JSON POST, custom headers) trigger a preflight OPTIONS request — the server must answer it, and a 404 on OPTIONS is the hidden cause of many 'random' CORS failures. Diagnose from the browser's exact error text and the Network tab's OPTIONS request before changing anything. Only proceed once you can name which rule was violated and why the specific fix satisfies it.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1095\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}