Skip to content

feat(trailing-slash): add alwaysRedirect option to support wildcard routes#4658

Merged
yusukebe merged 3 commits intonextfrom
feat/trailing-slash-strict-optoin
Feb 19, 2026
Merged

feat(trailing-slash): add alwaysRedirect option to support wildcard routes#4658
yusukebe merged 3 commits intonextfrom
feat/trailing-slash-strict-optoin

Conversation

@yusukebe
Copy link
Copy Markdown
Member

@yusukebe yusukebe commented Feb 2, 2026

Resolves #3407

Add strict alwaysRedirect option to trimTrailingSlash and appendTrailingSlash middleware.

When strict: true alwaysRedirect:true, the middleware redirects before executing handlers, which fixes the issue where trailing slash handling doesn't work with wildcard routes (*).

app.use(trimTrailingSlash({ alwaysRedirect: true }))

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.45%. Comparing base (f7d272a) to head (7cdfb57).
⚠️ Report is 21 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #4658      +/-   ##
==========================================
+ Coverage   91.43%   91.45%   +0.02%     
==========================================
  Files         173      173              
  Lines       11373    11435      +62     
  Branches     3296     3321      +25     
==========================================
+ Hits        10399    10458      +59     
- Misses        973      976       +3     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

@yusukebe yusukebe added the v4.12 label Feb 2, 2026
Comment thread src/middleware/trailing-slash/index.ts Outdated
* If `false` (default), it will only redirect when the route is not found (404).
* @default false
*/
strict?: boolean
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we invert the logic? When I hear an option strict it is more limited, hence only applied to 404. So what I mean if we implement as

  /**
   * If `true` (default), it will only redirect when the route is not found (404).
   * If `false`, the middleware will always redirect requests with a trailing slash.
   * This is useful for routes with wildcards (`*`).
   * @default true
   */

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codler Thanks.

I thought strict was confusing, since I saw your comment that we can interpret it in either sense. So I propose the eager instead of strict ( 1b4d0b8). This obviously executes before the handler with eager:true.

What do you think of it?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better. How about alwaysRedirect?: boolean?

app.use(trimTrailingSlash({ alwaysRedirect: true }))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codler

alwaysRedirect is good! I replaced.

@yusukebe yusukebe changed the title feat(trailing-slash): add strict option to support wildcard routes feat(trailing-slash): add eager option to support wildcard routes Feb 18, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@yusukebe yusukebe changed the title feat(trailing-slash): add eager option to support wildcard routes feat(trailing-slash): add alwaysRedirect option to support wildcard routes Feb 19, 2026
@github-actions
Copy link
Copy Markdown

Bundle size check

main (11c7828) #4658 (7118468) +/-
Bundle Size (B) 18,438B 18,438B 0B
Bundle Size (KB) 18.01K 18.01K 0K

Compiler Diagnostics (tsc)

main (11c7828) #4658 (7118468) +/-
Files 303 303 0
Lines 141,175 141,175 0
Identifiers 128,951 128,951 0
Symbols 269,501 269,501 0
Types 202,701 202,701 0
Instantiations 609,801 609,801 0
Memory used 336,421K 345,208K 8,787K
I/O read 0.03s 0.02s -0.01s
I/O write 0s 0s 0s
Parse time 0.68s 0.7s 0.02s
Bind time 0.29s 0.3s 0.01s
Check time 2.35s 2.45s 0.1s
Emit time 0s 0s 0s
Total time 3.32s 3.45s 0.13s

Compiler Diagnostics (typescript-go)

main (11c7828) #4658 (7118468) +/-
Files 303 303 0
Lines 145,356 145,356 0
Identifiers 130,542 130,542 0
Symbols 388,106 390,909 2,803
Types 372,849 372,849 0
Instantiations 984,004 984,004 0
Memory used 254,547K 254,978K 431K
Memory allocs 2,552,836 2,553,062 226
Config time 0.001s 0.001s 0s
Parse time 0.085s 0.098s 0.013s
Bind time 0.026s 0.019s -0.007s
Check time 0.578s 0.594s 0.016s
Emit time 0s 0s 0s
Total time 0.74s 0.821s 0.081s

Reported by octocov

@github-actions
Copy link
Copy Markdown

HTTP Performance Benchmark

Framework Runtime Average Ping Query Body
hono (origin/main) bun 35,632.26 47,895.79 32,567.07 26,433.91
hono (current) bun 35,765.49 48,533.53 31,971.12 26,791.81
Change +0.37% +1.33% -1.83% +1.35%

@yusukebe yusukebe changed the base branch from main to next February 19, 2026 10:15
@yusukebe yusukebe merged commit 034223f into next Feb 19, 2026
21 checks passed
@yusukebe yusukebe deleted the feat/trailing-slash-strict-optoin branch February 19, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

trimTrailingSlash doesn't work on paths with *

2 participants