Skip to content

Routing support for wildcard parameters #4158

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

Open
ruimaciel opened this issue May 23, 2025 · 5 comments
Open

Routing support for wildcard parameters #4158

ruimaciel opened this issue May 23, 2025 · 5 comments
Labels
enhancement New feature or request.

Comments

@ruimaciel
Copy link

ruimaciel commented May 23, 2025

What is the feature you are proposing?

Currently Hono's routing provides support for a number of parameters. However, there is no first-class support for usecases such as:

  1. a request is routed by prefix,
  2. hono sets a parameter by stripping the prefix from the request path,
  3. the route handler passes the parameter (i.e., request's path with the prefix stripped) to a subrequest and/or origin server.

This type of usecase happens frequently when using Cloudflare Workers. Currently, the best way to address this usecase is to manually put together the parameter, similar to:

app.get('/someprefix/*', async (c, next) => {
  const path = getPathFromURL(c.req.url)
  const parameter = path.replace(/^\/someprefix\//, ''))
  // do things
})

This works ok, but it leads to a subpar developer experience. I suppose that this could be done with regex parameters too.

Ideally, Hono would do this for us. It would be great if Hono provided support for wildcard parameters, where anything matching a wildcard would be conveniently provided through a parameter, similar to:

app.get('/someprefix/:parameter*', async (c, next) => {
  // do things
})

This topic was briefly touched in #591 , but so far without a response.

Would it be possible to implement wildcard parameters?

@ruimaciel ruimaciel added the enhancement New feature or request. label May 23, 2025
@EdamAme-x
Copy link
Contributor

I think you can implement wildcard parameter by regexp patameter

Image

app.get('/posts/:path{.+}', async (c) => {
 return c.text(c.req.param("path"))
})

@EdamAme-x
Copy link
Contributor

EdamAme-x commented May 23, 2025

One thing to add is that there is a way to implement this type of wildcard routing without compromising performance.
@yusukebe What do you think?

app.get('/someprefix/:parameter*', async (c, next) => {
  // do things
})

@yusukebe
Copy link
Member

Hi @ruimaciel

As @EdamAme-x said, it can support a wildcard parameter with regexp like the following:

app.get('/posts/:path{.+}', async (c) => {
 return c.text(c.req.param("path"))
})

@yusukebe
Copy link
Member

@EdamAme-x

Indeed, supporting /:parameter* will decrease the user's confusion. But, hmm. In my opinion, it's okay whether we do it or not.

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

No branches or pull requests

3 participants