Hono
Void handlers run on Hono. Add hono to your app’s dependencies to import its middleware and helpers.
npm install honoVoid already exports HTML templates from void/html and JSX for route handlers from void/jsx. You only need to install Hono for direct hono/* imports.
Built-in Middleware
Use Hono's built-in middleware in your global middleware files or per-route:
// middleware/01.cors.ts
import { cors } from 'hono/cors';
import { defineMiddleware } from 'void';
const apiCors = cors({ origin: 'https://example.com' });
export default defineMiddleware((c, next) =>
c.req.path.startsWith('/api/') ? apiCors(c, next) : next(),
);With an origin other than *, cors() adds Vary: Origin to every response it handles, and revalidation does not cache responses that vary by request headers, so this middleware applies it only to paths under /api/.
Your middleware handles preflight requests in dev and preview as well as in production. If you enable Vite’s server.cors, Vite answers preflights first. Preview uses that setting unless you override preview.cors.
// routes/api/products.ts (per-route cache)
import { cache } from 'hono/cache';
import { defineHandler } from 'void';
import { listProducts } from '../../lib/products';
export const GET = defineHandler(
cache({ cacheName: 'products', cacheControl: 'max-age=3600' }),
async () => ({ products: await listProducts() }),
);WARNING
cache() stores one response per URL and returns it to every visitor. It skips requests with an Authorization header, but not cookies. Use it only on routes whose response is the same for everyone, never on a route that reads the signed-in user. To cache pages, use revalidation, which skips requests that carry a cookie.
// routes/api/webhooks/deploy.ts (per-route usage)
import { bearerAuth } from 'hono/bearer-auth';
import { timingSafeEqual } from 'hono/utils/buffer';
import { defineHandler } from 'void';
import { env } from 'void/env';
export const POST = defineHandler(
bearerAuth({ verifyToken: (token) => timingSafeEqual(token, env.DEPLOY_HOOK_TOKEN) }),
() => ({ received: true }),
);Declare DEPLOY_HOOK_TOKEN in env.ts and set it with void secret put. Services that sign their webhook requests instead, such as Stripe, need a signature check on the raw body from c.req.text().
Cloudflare compresses responses for clients that accept compression, so compress() from hono/compress has no effect when your app runs on Cloudflare. With the node, bun or deno target, it compresses responses as in any Hono app.
Response Helpers
Hono's context helpers are available on c in every handler:
// routes/api/download.ts
import { stream } from 'hono/streaming';
import { defineHandler } from 'void';
export const GET = defineHandler((c) => {
return stream(c, async (stream) => {
await stream.write('chunk 1');
await stream.write('chunk 2');
});
});// routes/api/data.ts
import { defineHandler } from 'void';
export const GET = defineHandler((c) => {
c.header('X-Custom', 'value');
c.status(201);
return c.json({ created: true });
});Third-party Middleware
Any hono/* or @hono/* package works. Install the package in your app, then use it directly:
// middleware/01.block-ai-bots.ts
import { uaBlocker } from '@hono/ua-blocker';
import { aiBots } from '@hono/ua-blocker/ai-bots';
import { defineMiddleware } from 'void';
export default defineMiddleware(uaBlocker({ blocklist: aiBots }));Workers do not allow timers, random values, or I/O at the top level of a file. A package that uses them when you create its middleware, such as hono-rate-limiter with its default store, works in dev but fails in preview and production. To rate-limit requests, keep the count in Durable State or, on direct Cloudflare deployments, use a Workers Rate Limiting binding.
Utilities
Hono's standalone helpers work anywhere:
import { HTTPException } from 'hono/http-exception';
throw new HTTPException(404, { message: 'User not found' });For more built-in middleware and helpers, see the Hono documentation.
Redirects
Return c.redirect() from a handler to send the visitor to another URL. If a helper needs to stop the handler, throw an HTTPException with the redirect response in res:
// lib/require-page-user.ts
import { HTTPException } from 'hono/http-exception';
import type { CloudContext } from 'void';
import { getUser } from 'void/auth';
export function requirePageUser(c: CloudContext) {
const user = getUser();
if (!user) {
throw new HTTPException(302, { res: c.redirect('/login') });
}
return user;
}This works in loaders, actions, and middleware, including client-side page navigation. For a redirect after a mutation, use new HTTPException(303, { res: c.redirect('/login', 303) }) so the next request is a GET.
For a sign-in check like this one, requireAuth(c, { throwRedirect: '/login' }) provides the same guard and selects the redirect status for you.