hono icon indicating copy to clipboard operation
hono copied to clipboard

fix(compose): preserve `c.req.routeIndex` across `next()`

Open usualoma opened this issue 3 months ago • 3 comments

Reference: https://github.com/honojs/hono/issues/4196#issuecomment-3317581222

This is a bug that has existed since quite early on. After calling next(), param() and routePath() were not returning the correct values. I believe this needs to be fixed.

Although this usage pattern is not very common, since it involves a change to the core behavior, I think it would be appropriate to merge this change during the next major version update.

An app that can reproduce the bug

const app = new Hono()

app.use('/:all{.*}', async (c, next) => {
  console.log(routePath(c))
  await next()
  console.log(routePath(c)) // should be '/:all{.*}'
  console.log(c.req.param('all')) // should be the request path
})

app.get('/posts/:id', (c) => {
  return c.text(routePath(c))
})

export default app

Expected result

  • Outputs /:all{.*}, and the request path

Actual result

  • Outputs /posts/:id, and undefined

The author should do the following, if applicable

  • [x] Add tests
  • [x] Run tests
  • [x] bun run format:fix && bun run lint:fix to format the code

usualoma avatar Sep 23 '25 04:09 usualoma

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 91.26%. Comparing base (ba98aa2) to head (1b7414c). :warning: Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4424   +/-   ##
=======================================
  Coverage   91.25%   91.26%           
=======================================
  Files         171      171           
  Lines       10913    10916    +3     
  Branches     3144     3147    +3     
=======================================
+ Hits         9959     9962    +3     
  Misses        953      953           
  Partials        1        1           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Sep 23 '25 04:09 codecov[bot]

Hi @yusukebe Would you please review this?

usualoma avatar Sep 23 '25 04:09 usualoma

@usualoma

Thank you. Looks good to me!

Although this usage pattern is not very common, since it involves a change to the core behavior, I think it would be appropriate to merge this change during the next major version update.

It's hard to decide. I want to release this before the major version update, but we don't know if it will be soon or not. As you said, some users may be damaged. For now, keep it not merged.

yusukebe avatar Sep 25 '25 00:09 yusukebe