Get to know Hono on the Fastly Compute environment with a basic starter written in TypeScript.
This starter demonstrates how to:
- Define typed environment bindings (
Backend,Logger, etc.) - Add routing and middleware with Hono
- Build synthetic responses at the edge
- Forward requests to backends with caching overrides
- Serve static files compiled directly into your Compute app
For more starter kits and patterns, see the Fastly Documentation Hub.
- TypeScript source with
tsconfig.jsonpre-configured for Compute - Usage of the
@fastly/hono-fastly-computehelper library - Restrict allowed HTTP methods
- Route matching with Hono
- Static file embedding via
includeBytes - Logging to a Fastly endpoint
- Backend fetch with
CacheOverride
The entry point src/index.ts includes several common patterns you'll use when building Compute apps with Hono:
-
Defining environment context bindings
const fire = buildFire({ httpme: 'Backend:http-me', // I have a backend named `http-me` myEndpoint: 'Logger:my_endpoint', // I have a logging endpoint named `my_endpoint` }); type Env = { Bindings: typeof fire.Bindings, }; const app = new Hono<Env>(); // set up routes... fire(app);
Defined environment resources are available on
c.envwithin handlers and middleware (env on Hono), for example:c.env.myEndpoint.log('Hello from the edge!');
For more details on context bindings, see @fastly/compute-js-context (typed environment bindings).
-
Static file response
const welcomePage = includeBytes('./src/welcome-to-compute.html'); return c.body(welcomePage, 200, { 'Content-Type': 'text/html' });
-
Streaming log endpoint
c.env.myEndpoint.log('Hello from the edge!');
-
Method filtering
app.use(createMiddleware(async (c, next) => { if (!["HEAD", "GET", "PURGE"].includes(c.req.method)) { return c.text("This method is not allowed", 405); } await next(); }));
-
Backend fetch with cache override
const cacheOverride = new CacheOverride('override', { ttl: 60 }); const beresp = await fetch(bereq, { backend: c.env.httpme, cacheOverride });
Create a new app from this starter kit:
npm create @fastly/compute@latest -- --language=typescript --starter-kit=honoRun locally with the local development environment:
npm run startDeploy to Fastly (you'll be prompted to create a new service if you don't have one yet):
npm run deploy- Try adding a new route in
index.ts, e.g.app.get('/hello', ...) - Configure a real backend in
fastly.tomland call it from your code - Explore
@fastly/compute-js-contextfor richer typed bindings - Add middleware from the Hono ecosystem
Please see our SECURITY.md for guidance on reporting security-related issues.