Hono v4.12.0 — 2026-02-19
Hono v4.12.0 をリリースしました。このリリースでは、Hono クライアントの新機能、ミドルウェアの改善、アダプターの拡張、そしてルーターやコンテキスト周りの大幅なパフォーマンス改善が含まれます。
主な変更点
$path for Hono Client
Hono クライアントに $path() メソッドが追加され、フル URL ではなくパス文字列を返すようになりました。ルーティングやキャッシュキーなど、パス部分だけが必要な場合に便利です。
const client = hc < typeof app > ( 'http://localhost:8787' )
const path = client . api . posts . $path ( )
const postPath = client . api . posts [ ':id' ] . $path ( { param : { id : '123' } , } )
const searchPath = client . api . posts . $path ( { query : { filter : 'test' } , } )
$url() が URL オブジェクトを返すのに対し、$path() はプレーンなパス文字列を返すため、ルーターやキャッシュキーとして扱いやすくなります。Thanks @ShaMan123!
ApplyGlobalResponse 型ヘルパー (RPC Client)
新しい ApplyGlobalResponse 型ヘルパーにより、RPC クライアント内の全ルートに対してグローバルなエラー応答タイプを追加できます。app.onError() やグローバルミドルウェアの共通のエラー応答を型付けするのに有用です。
const app = new Hono ( )
. get ( '/api/users' , ( c ) => c . json ( { users : [ 'alice' , 'bob' ] } , 200 ) )
. onError ( ( err , c ) => c . json ( { error : err . message } , 500 ) )
type AppWithErrors = ApplyGlobalResponse < typeof app , {
401 : { json : { error : string ; message : string } }
500 : { json : { error : string ; message : string } }
} >
const client = hc < AppWithErrors > ( 'http://api.example.com' )
const res = await client . api . users . $get ( )
Thanks @mohankumarelec!
SSG Redirect Plugin
SSG 用の redirectPlugin を追加しました。HTTP リダイレクト応答(301, 302, 303, 307, 308)に対して静的な HTML リダイレクトページを生成します。
import { toSSG } from 'hono/ssg'
import { defaultPlugin , redirectPlugin } from 'hono/ssg'
const app = new Hono ( )
app . get ( '/old' , ( c ) => c . redirect ( '/new' ) )
app . get ( '/new' , ( c ) => c . html ( 'New Page' ) )
await toSSG ( app , fs , { plugins : [ redirectPlugin ( ) , defaultPlugin ( ) ] , } )
生成されるリダイレクトページには <meta http-equiv="refresh"> タグ、canonical リンク、robots の noindex メタタグが含まれます。Thanks @3w36zj6!
Basic Auth の onAuthSuccess コールバック
Basic Auth ミドルウェアに onAuthSuccess コールバックが追加されました。認証成功後に呼ばれ、Authorization ヘッダを再解析せずにコンテキスト変数の設定やログ出力ができます。非同期関数や verifyUser モードにも対応しています。
app . use ( '/auth/*' , basicAuth ( {
username : 'hono' ,
password : 'ahotproject' ,
onAuthSuccess : ( c , username ) => {
c . set ( 'user' , { name : username , role : 'admin' } )
console . log ( `User ${ username } authenticated` )
} ,
} ) )
Thanks @AprilNEA!
getConnInfo が AWS Lambda, Cloudflare Pages, Netlify に対応
getConnInfo() が以下のアダプターで利用可能になりました。
-
AWS Lambda (API Gateway v1/v2 と ALB をサポート)
import { handle , getConnInfo } from 'hono/aws-lambda'
-
Cloudflare Pages
import { handle , getConnInfo } from 'hono/cloudflare-pages'
-
Netlify
import { handle , getConnInfo } from 'hono/netlify'
例:
app . get ( '/' , ( c ) => {
const info = getConnInfo ( c )
return c . text ( `Your IP: ${ info . remote . address } ` )
} )
Thanks @rokasta12!
Trailing Slash ミドルウェアの alwaysRedirect オプション
trailing slash ミドルウェアに alwaysRedirect オプションを追加しました。有効にすると、ハンドラー実行前にリダイレクトを行うため、ワイルドカードルートでの末尾スラッシュ処理が正しく動作します。
app . use ( trimTrailingSlash ( { alwaysRedirect : true } ) )
app . get ( '/my-path/*' , async ( c ) => { return c . text ( 'wildcard' ) } )
/my-path/something/ はワイルドカードハンドラーが実行される前に /my-path/something にリダイレクトされます。
プログレッシブなロケールコードの切り捨て
language ミドルウェア内の normalizeLanguage が RFC 4647 Lookup ベースのプログレッシブ切り捨てをサポートするようになりました。ja-JP のようなロケールは、supportedLanguages にベース言語 ja があれば ja にマッチします。
app . use ( '/*' , languageDetector ( {
supportedLanguages : [ 'en' , 'ja' ] ,
fallbackLanguage : 'en' ,
order : [ 'cookie' , 'header' ] ,
} ) )
// Accept-Language: ja-JP → matches 'ja'
// Accept-Language: ko-KR → falls back to 'en'
Thanks @sorafujitani!
ExecutionContext の exports フィールド
ExecutionContext 型に Cloudflare Workers 用の exports プロパティが追加されました。Wrangler が生成する型とモジュール拡張で型付けできます。
import 'hono'
declare module 'hono' {
interface ExecutionContext {
readonly exports : Cloudflare . Exports
}
}
Thanks @toreis-up!
パフォーマンス改善
TrieRouter が 1.5x ~ 2.0x 高速化
TrieRouter を最適化し、spread 構文の削減、O(1) の hasChildren チェック、遅延正規表現生成、冗長処理の削除などを行いました。ベンチマーク結果(相対):
| Route | Node.js | Deno | Bun |
|---|
| short static GET /user | 1.70x | 1.40x | 1.34x |
| dynamic GET /user/lookup/username/hey | 1.38x | 1.69x | 1.51x |
| wildcard GET /static/index.html | 1.51x | 1.72x | 1.43x |
| all together | 1.58x | 1.60x | 1.82x |
Thanks @EdamAme-x!
c.json() の高速パス
c.json() は c.text() と同様の高速パス最適化が追加されました。カスタムステータスやヘッダー、finalized 状態がない場合、Headers オブジェクトを割り当てずに直接 Response を生成します。一般的なパターンが高速化されます。
return c . json ( { message : 'Hello' } )
ベンチマーク:
| Metric | Before | After | Change |
|---|
| Reqs/sec | 92,268 | 95,244 | +3.2% |
| Latency | 5.42ms | 5.25ms | -3.1% |
| Throughput | 17.24MB/s | 19.07MB/s | +10.6% |
Thanks @mgcrea!
変更履歴(抜粋)
- feat(client): Add ApplyGlobalResponse type helper for RPC Client (#4556)
- feat(ssg): add redirect plugin (#4599)
- feat(client): $path (#4636)
- feat(basic-auth): add context key and callback options (#4645)
- feat(adapter): add getConnInfo for AWS Lambda, Cloudflare Pages, and Netlify (#4649)
- feat(trailing-slash): add alwaysRedirect option to support wildcard routes (#4658)
- feat(language): add progressive locale code truncation to normalizeLanguage (#4717)
- feat(types): Add exports field to ExecutionContext (#4719)
- perf(context): add fast path to c.json() matching c.text() optimization (#4707)
- perf(trie-router): improve performance (1.5x ~ 2.0x) (#4724)
- perf(context): use createResponseInstance for new Response (#4733)
他にもバグ修正や最適化、ドキュメント更新が含まれています。
新しいコントリビューター
- @mixelburg (初めての貢献 #4729)
- @aidenlx (初めての貢献 #4732)
- @mohankumarelec (初めての貢献 #4556)
- @ShaMan123 (初めての貢献 #4636)
- @rokasta12 (初めての貢献 #4649)
- @mgcrea (初めての貢献 #4707)
フルチェンジログ
v4.11.10...v4.12.0 を参照してください。
Contributors: yusukebe, usualoma, and 11 other contributors
Thanks to everyone who contributed and helped review this release!