Skip to Content
Senri
Runtimes

Cloudflare Workers

Run on Workers. export default app, c.env and c.executionCtx, and deploying

Export the app as is. Workers calls app.fetch(request, env, ctx).

src/index.ts
import {  } from "@senrijs/senri";

const  = ();
.("/", () => "Hello, Workers!");

export default ;
npx wrangler dev      # http://localhost:8787 (the template's npm run dev)
npx wrangler deploy   # deploy (the template's npm run deploy)

For a new app, the template is the quickest way.

npm create senri@latest my-app -- --template workers

c.env and c.executionCtx

Variables and bindings in wrangler.jsonc are available as c.env. Type them with createApp<{ Bindings: Env }>().

src/index.ts
import {  } from "@senrijs/senri";

type  = { : string };
const  = <{ :  }>();

.("/", (, ) => {
    ..(());   // work that continues after the response
    return ..GREETING;
type GREETING: string
}); export default ;

To also type the variables used with c.set() / c.get(), write createApp<{ Bindings: Env; Variables: { user: User } }>().

Differences on Workers

  • Development mode: Workers cannot read NODE_ENV, so devtools() gives development output only for requests to localhost (wrangler dev). Error pages appear, but without the code. There is no /_senri dashboard
  • Bundle size: wrangler bundles everything into one file, so leaving dev: devtools() in also bundles the development code. Remove it from production builds
  • Static files: use Workers Static Assets instead of @senrijs/senri/static
  • IP: getIp(c) from @senrijs/senri/ip reads CF-Connecting-IP, which Cloudflare adds

On this page