Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use CORS? #364

Open
1 task done
mimoo opened this issue Aug 2, 2023 · 2 comments
Open
1 task done

how to use CORS? #364

mimoo opened this issue Aug 2, 2023 · 2 comments

Comments

@mimoo
Copy link

mimoo commented Aug 2, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Description

I'm using it this way now but I'd like confirmation that this is the way :)

#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
    log_request(&req);

    // Optionally, get more helpful error messages written to the console in the case of a panic.
    utils::set_panic_hook();

    // CORS
    let cors = Cors::default()
        .with_max_age(86400)
        .with_origins(vec!["*"])
        .with_methods(vec![
            Method::Get,
            Method::Head,
            Method::Post,
            Method::Options,
        ]);

     // TRUNCATED...

    let router = Router::new();

    router
        // TRUNCATED...
        .run(req, env)
        .await?
        .with_cors(&cors)
@film42
Copy link

film42 commented Sep 11, 2023

This looks correct and it matches the example in the docs: https://docs.rs/worker/latest/worker/struct.Response.html#method.with_cors

You can always confirm by checking your response headers. Here is where the Cors helper applies headers to the response: https://docs.rs/worker/latest/src/worker/cors.rs.html#86-118

@dakom
Copy link
Contributor

dakom commented Mar 15, 2024

note that you may also have to allow some headers, e.g. .with_allowed_headers(["*"]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants