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

Route rules with cors: true does not work #2340

Open
MickL opened this issue Apr 5, 2024 · 1 comment
Open

Route rules with cors: true does not work #2340

MickL opened this issue Apr 5, 2024 · 1 comment

Comments

@MickL
Copy link
Contributor

MickL commented Apr 5, 2024

Environment

nitropack: 2.9.6

Reproduction

https://stackblitz.com/edit/github-xaqmuq?file=nitro.config.ts

Describe the bug

Setting cors: true to does not work:

routeRules: {
    '/**': {
      cors: true,
    },
},

The browser still throws cors error:

Access to fetch at 'http://localhost:3333/' from origin 'http://localhost:3001' 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. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

With header, too:

routeRules: {
    '/**': {
      cors: true,
      headers: {
        'Access-Control-Allow-Origin': '*',
      },
    },
  },

Error:

Access to fetch at 'http://localhost:3333/' from origin 'http://localhost:3001' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

Additional context

I already opened an issue at Nuxt that setting corse:true does not have an effect on /api routes, only on /public folder.

Logs

No response

@MickL
Copy link
Contributor Author

MickL commented May 22, 2024

Hi @pi0 @danielroe , I digged deeper into this and found a critical bug with route rules not beeing applied. Maybe a problem with https://github.com/unjs/radix3?

The following rule works for all routes:

routeRules: {
    '/**': {
      cors: true,
      headers: {
        'Access-Control-Allow-Origin': '*',
      },
    },
  },

BUT if a route does not have GET route, the routeRoule is not applied. E.g.:

/products/index.ts
/products/index.patch.ts
/items/index.patch.ts -> Route rule not applied

Therefor the routes without a GET route throw a cors error in my case. This could also explain nuxt/nuxt#25686

I made a reproduction repo, you can simply run bun install and then bun dev to start a Nuxt app at localhost:3000 and a Nitro app at localhost:3001

https://github.com/MickL/nitro-route-rules-bug

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

No branches or pull requests

1 participant