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

CORS setup for API route #33

Open
nikolalsvk opened this issue Nov 9, 2023 · 0 comments
Open

CORS setup for API route #33

nikolalsvk opened this issue Nov 9, 2023 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@nikolalsvk
Copy link

nikolalsvk commented Nov 9, 2023

Hey @dulnan, great project here 🙌

I'm running into an issue where I'm calling the purge API from another server. For that, I had to configure CORS for my purge route.

This is what I added:

// ~/server/middleware/cors.ts

export default defineEventHandler((event) => {
  if (!event.req.url?.startsWith('/__cache/')) return

  setResponseHeaders(event, {
    'Access-Control-Allow-Methods': 'GET,HEAD,PUT,PATCH,POST,DELETE',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Credentials': 'true',
    'Access-Control-Allow-Headers': '*',
    'Access-Control-Expose-Headers': '*',
  })

  if (event.method === 'OPTIONS') {
    event.node.res.statusCode = 204
    event.node.res.statusMessage = 'No Content.'
    return 'OK'
  }
})

I can add it to docs if you agree. It might be useful to other people.

@dulnan dulnan added the documentation Improvements or additions to documentation label Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants