new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Hono · all subjects

routing

73 notes in this subject, read out of this brain and free to use. This is page 2 of 2.

Supabase Edge Functions basePath configuration

When building a Hono app for Supabase Edge Functions, set the basePath to match the function name format. The basePath should be set to the function name prefixed with a forward slash, for example const app = new Hono().basePath(`/${functionName}`) where functionName is 'hello-world'.

Avoid RoR-like Controllers for type inference

Do not create separate controller functions that take Context as a parameter and return responses. This prevents path parameter type inference. Instead, write handlers directly inline after path definitions where path parameters can be inferred by TypeScript.

Use factory.createHandlers() for RoR-style controllers when necessary

If you must create RoR-like controllers, use factory.createHandlers() from 'hono/factory'. Import createFactory from 'hono/factory', instantiate it as const factory = createFactory(), then call factory.createHandlers() passing middleware and handler functions. This enables correct type inference for path parameters.

factory.createHandlers() example with middleware

Example showing factory.createHandlers() with middleware: import { createFactory } from 'hono/factory'; import { logger } from 'hono/logger'; const factory = createFactory(); const middleware = factory.createMiddleware(async (c, next) => { c.set('foo', 'bar'); await next(); }); const handlers = factory.createHandlers(logger(), middleware, (c) => { return c.json(c.var.foo); }); app.get('/api', ...handlers);

Hono automatically converts HEAD to GET

Hono automatically handles HEAD requests by converting them to GET requests and stripping the response body. This conversion happens at the dispatch layer before route matching, not in individual handlers.

HEAD requests use GET routes with body stripped

GET route handlers automatically handle HEAD requests. The response returns the same status and headers as GET, but with no body. Define handlers using app.get(), not app.head().

app.head() and app.on('HEAD') won't work as expected

Creating dedicated HEAD handlers with app.head() or app.on('HEAD') will not work because HEAD requests are converted to GET before route matching occurs. These handlers will never be called.

Use middleware for HEAD-specific logic

To add HEAD-specific behavior (like skipping expensive body computation), use middleware that checks c.req.method === 'HEAD' after the handler runs. In middleware after next(), detect HEAD and optionally create a new Response(null, c.res) to remove the body.

HEAD request performance optimization

If you expect many HEAD requests and the GET handler performs expensive operations, use middleware to detect HEAD requests and skip body generation. This avoids unnecessary computation for HEAD responses.

HEAD requests respect same caching rules as GET

HEAD response caching headers follow the same caching rules as GET responses. Body-processing middleware like compression automatically skips HEAD requests.

Test HEAD requests separately from GET

When testing, verify both GET and HEAD requests independently. Check that HEAD returns the same status and headers as GET but with null body.

RegExpRouter performance

The RegExpRouter in Hono is the fastest router in the JavaScript world, with 402,820 ops/sec. It matches routes using a single large Regex created before dispatch.

Multiple router types in Hono

Hono provides multiple routers: RegExpRouter (fastest, uses single regex), SmartRouter (supports all route patterns), LinearRouter (registers routes very quickly for environments that initialize applications every time), and PatternRouter (simple, makes small bundles).

Give your agent this brain