OpenAIHono2026/02/19 11:46

v4.12.0

要点だけを先に読めるように短く再構成したセクションです。

元記事

Quick Digest

要約

要点だけを先に読めるように短く再構成したセクションです。

openaijamodel: gpt-5-mini-2025-08-07

Hono v4.12.0 リリースノート

Key Points

  • TrieRouter が高速化
  • $path() でパス文字列取得
  • SSG に静的リダイレクト生成

Summary

Hono v4.12.0 が公開されました(2026-02-19)。本リリースはクライアント機能の拡充、ミドルウェア/アダプタの改善、そしてルータやコンテキスト周りの大幅な性能改善が中心です。エンジニア向けに導入ポイントと運用上の注意を簡潔にまとめます。

Key Points

  • $path() を追加
    • クライアントから URL オブジェクトではなくパス文字列を取得できます。ルーティング照合やキャッシュキー生成に便利。
    • 例: client.api.posts.$path()/api/posts
  • ApplyGlobalResponse 型ヘルパー(RPC クライアント)
    • app.onError() やグローバルミドルウェアの共通エラー型をクライアント側で型付け可能に。
  • SSG の redirectPlugin
    • 301/302/... のレスポンスを静的リダイレクト HTML(meta refresh、canonical、robots:noindex)として生成します。
  • basic-auth に onAuthSuccess コールバック
    • 認証成功後にコンテキストへユーザ情報をセットしたりログを残す処理を同期/非同期で実行可能。
  • getConnInfo を追加(AWS Lambda, Cloudflare Pages, Netlify)
    • クライアントIPなど接続情報を各アダプタから取得できるようになりました。
  • trimTrailingSlash の alwaysRedirect オプション
    • ワイルドカードルートと組み合わせた場合でもリダイレクトをハンドラ実行前に行えます。
  • 言語ミドルウェアの RFC 4647 Lookup 対応
    • ja-JP のようなリージョン指定をベース言語 ja に段階的にマッチさせる挙動をサポート。
  • ExecutionContext に exports フィールド追加
    • Cloudflare Workers のモジュール型(Wrangler 生成型)での型拡張が可能に。
  • 性能改善
    • TrieRouter が約1.5×〜2.0×高速化。c.json() にも fast path を導入しレイテンシとスループットが改善。

Migration / Notes

  • 破壊的変更は報告されていません。新機能は基本的にオプトインで利用可能です。
  • 型ヘルパーや ExecutionContext の拡張はコード側で明示的に利用する必要があります。
  • 性能改善はルーティング負荷が高いパスに特に効果が出ます。ベンチマーク確認の上でアップデートを検討してください。

Full Translation

翻訳

原文の流れを保ったまま読める翻訳セクションです。

openaijamodel: gpt-5-mini-2025-08-07

v4.12.0 リリースノート

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' )
// Get the path string
const path = client . api . posts . $path ( )
// => '/api/posts'

// With path parameters
const postPath = client . api . posts [ ':id' ] . $path ( { param : { id : '123' } , } )
// => '/api/posts/123'

// With query parameters
const searchPath = client . api . posts . $path ( { query : { filter : 'test' } , } )
// => '/api/posts?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' )
// Now client knows about both success and error responses
const res = await client . api . users . $get ( )
// InferResponseType includes { users: string[] } | { error: string; message: string }

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' ) )

// redirectPlugin must be placed before defaultPlugin
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 チェック、遅延正規表現生成、冗長処理の削除などを行いました。ベンチマーク結果(相対):

RouteNode.jsDenoBun
short static GET /user1.70x1.40x1.34x
dynamic GET /user/lookup/username/hey1.38x1.69x1.51x
wildcard GET /static/index.html1.51x1.72x1.43x
all together1.58x1.60x1.82x

Thanks @EdamAme-x!

c.json() の高速パス

c.json()c.text() と同様の高速パス最適化が追加されました。カスタムステータスやヘッダー、finalized 状態がない場合、Headers オブジェクトを割り当てずに直接 Response を生成します。一般的なパターンが高速化されます。

// This common pattern is now faster
return c . json ( { message : 'Hello' } )

ベンチマーク:

MetricBeforeAfterChange
Reqs/sec92,26895,244+3.2%
Latency5.42ms5.25ms-3.1%
Throughput17.24MB/s19.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!