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 middleware: Possible to make OPTIONS handling optional? #3979

Open
alvinsw opened this issue Mar 6, 2025 · 3 comments
Open

CORS middleware: Possible to make OPTIONS handling optional? #3979

alvinsw opened this issue Mar 6, 2025 · 3 comments

Comments

@alvinsw
Copy link

alvinsw commented Mar 6, 2025

Does the CORS specification mandates that the HTTP OPTIONS method to be handled in a specific way?

if (c.req.method === 'OPTIONS') {

Can we make that behavior optional by adding all the required headers before the await next() and returning a default 204 response after the await next() only if no response has been sent by the previous route handlers?

@yusukebe
Copy link
Member

yusukebe commented Mar 7, 2025

Hi @alvinsw

You may do it with custom middleware. But, though I've tried, I haven't found a good way right now.

@alvinsw
Copy link
Author

alvinsw commented Mar 12, 2025

What is the best way to "wrap" another middleware with our own custom middleware? Can you give a code example?

@tclohm
Copy link

tclohm commented Mar 21, 2025

Here is how I would wrap another middleware,

const custom: MiddlewareHandler = async (c, next) => {
  console.log('Inside')
  await next()
}

const wrap = (middleware: MiddlewareHandler): MiddlewareHandler => {
  return async (c, next) => {
    console.log('Before')
    await middleware(c, next)
    console.log('After')
  }
}

app.use('/', wrap(custom))

Hope that helps

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