diff --git a/src/docs/src/Workers/router.md b/src/docs/src/Workers/router.md index 71a1ffeef..405311b3b 100644 --- a/src/docs/src/Workers/router.md +++ b/src/docs/src/Workers/router.md @@ -88,11 +88,9 @@ A common use is a catch-all route for unmatched paths — define it last so it o ## CORS -Every response from your worker automatically includes `Access-Control-Allow-Origin: *`, so **simple cross-origin requests work out of the box** — a basic `GET` or `POST` from another origin just works, no extra code. +CORS is automatically handled for you. Every response includes `Access-Control-Allow-Origin: *`, and preflight `OPTIONS` requests are answered automatically. Cross-origin requests work out of the box, including [`puter.workers.exec()`](/Workers/exec/), which sends the user's Puter token in a custom `puter-auth` header (this is what populates `user.puter`) without you writing any CORS code. -Some requests need a **CORS preflight** first: the browser sends an `OPTIONS` request and waits for the allowed methods and headers before sending the real one. This happens when the request uses a method like `PUT` or `DELETE`, or carries custom headers (e.g. `Authorization`). - -To handle this, you can add an `OPTIONS` handler that returns the methods and headers you want to allow: +You only need to think about CORS if you define your own `OPTIONS` handler. Doing so takes over preflight handling, so you become responsible for the headers the browser expects: ```js router.options("/*path", async () => { @@ -107,11 +105,7 @@ router.options("/*path", async () => { }); ``` -This answers the preflight for any path with the CORS headers the browser expects, so your other routes work cross-origin. - -
puter-auth header is important: when you call your worker with puter.workers.exec(), it attaches the user's Puter token in a puter-auth header so the worker can act on the calling user's behalf (this is what populates user.puter). Because that's a custom header, the browser runs a preflight first — so puter-auth must be listed in Access-Control-Allow-Headers, otherwise the preflight fails and the request never reaches your worker.puter.workers.exec(), list puter-auth in Access-Control-Allow-Headers — otherwise the preflight fails and the request never reaches your worker.