Absolute URL of the handled route; on client navigation, the target page's URL.
params
Named dynamic segments, each a string.
query
URLSearchParams.
headers
Headers.
env
Worker environment and bindings.
cookies
Cookie jar described below.
d1
D1 query facade described below.
postgres
Postgres.js client with Hyperdrive caching disabled, for writes and fresh reads.
postgresCached
Postgres.js client with Hyperdrive caching enabled, for reads that may be about a minute stale.
kv
KV facade described below.
cache
JSON cache described below.
r2
R2 bucket facade described below.
body()
Promise of a plain object: parsed JSON, or the fields of either form encoding. Other content types, including text, give {}; use rawBody() for those.
rawBody()
Promise of Uint8Array bytes, sharing the cached read with body().
waitUntil(promise)
Register background work with the Worker execution context. See background work.
Middleware may attach its own fields to req, such as req.user, for later middleware and the handler; declare them for TypeScript on AppRequestFields. See Request handlers.
JSON bodies: JSON bodies must have an object root.
Form bodies: URL-encoded and multipart forms return field objects. Uploads are File values, repeated names become arrays, and missing fields are absent.
Upload fields: Fields declared in the route's uploads become verified R2Upload values or are absent. See Uploads.
Text bodies: Text returns a string.
Unsupported bodies: Empty or unsupported bodies return an empty object.
Parse failures: Malformed supported bodies produce 400. See Request handlers.
src/routes/name.ts
import type { Handler } from"goribu/server";
exportconst POST: Handler = async (req, res) => {
const { name } = await req.body();
if (typeof name !== "string" || !name.trim()) {
return res.invalid({ name: "Enter your name." });
}
return res.json({ name: name.trim() });
};
src/routes/name.js
exportconst POST = async (req, res) => {
const { name } = await req.body();
if (typeof name !== "string" || !name.trim()) {
return res.invalid({ name: "Enter your name." });
}
return res.json({ name: name.trim() });
};
Normal anchor props, replace = false, prefetch = true.
NavLink
Link props plus end = false and a string or ({ isActive }) => className callback. Goribu owns aria-current.
navigate(target, { replace? }?)
Returns Promise<void>. Requires a browser and an HTTP(S) destination.
Active matching: NavLink matches pathname boundaries, ignoring query and hash. Root is exact-only.
Native links: Link enhancement preserves native behavior for external links, downloads, modifier clicks, and same-document anchors. See Links & navigation.
Static navigation: Production static navigation and prefetch reuse saved build-time data without invoking the Worker.
Current static URL: Clicking the exact current URL scrolls a confirmed static page without fetching or rerendering.
Current dynamic URL: Clicking the exact current URL of a dynamic page fetches fresh data.
Current-URL history: Neither static nor dynamic navigation to the exact current URL adds a history entry.
Use the req.d1 tagged template in handlers or middleware. Values become parameters. A statement is lazy until a terminal runs.
API
Result or purpose
.get()
Promise of first row or null.
.all()
Promise of row array.
.value()
Promise of first column of first row or null.
.run()
Promise of { changes, lastRowId }.
req.d1.atomic(statements)
Atomic batch of unexecuted statements; entries have rows, changes, and lastRowId.
req.d1.unsafe(text, params?)
Statement from application-controlled SQL structure and explicit values.
req.d1.fresh
Query facade that starts each tagged terminal at the primary.
req.d1.public
Query facade without the visitor's bookmark.
.session
Session object for integrations; raw binding when replication is disabled.
.binding
Plain Cloudflare D1Database binding.
Tagged D1 values: Strings, numbers, booleans, null, ArrayBuffer, ArrayBufferView, and non-empty arrays of those scalars are supported.
Unsafe parameters:unsafe() parameters are a flat scalar array.
Rejected D1 values: Undefined, dates, objects, nested/empty arrays, functions, symbols, and bigints are rejected.
Default consistency: Default D1 queries use the visitor session when replication is enabled.
Facade restrictions: Fresh/public cannot be chained. Atomic belongs to the default facade.
Batch session: A batch rebinds passed statements to its own session. See D1.
Typed row results
// Inside a handler; D1 and the posts table must exist.type Post = { id: number; title: string };
const posts = await req.d1<Post>`
SELECT id, title FROM posts ORDERBY id DESC`.all();
Typed row results
const posts = await req.d1`
SELECT id, title FROM posts ORDERBY id DESC`.all();
With database: { type: "postgres" }, handlers and middleware get two Postgres.js clients. Goribu doesn't wrap them, so the Postgres.js API applies as written. Use them as tagged templates; values become parameters and each query resolves to a row array.
API
Result or purpose
req.postgres
Client with Hyperdrive caching off. The default for writes, transactions, and fresh reads.
req.postgresCached
Client with Hyperdrive caching on. Results may be up to about 75 seconds old, and writes don't clear them.
req.postgres.begin(callback)
Transaction. Commits when the callback finishes and rolls back if it throws.
req.postgres(identifier), req.postgres(rows)
Postgres.js helpers for dynamic table or column names and bulk inserts.
req.postgres.array(values)
Array parameter for booleans, Dates, bytea, or bigints. String and number arrays need no wrapper.
req.env.POSTGRES.connectionString
Hyperdrive connection string with caching off, for libraries with their own driver.
req.env.POSTGRES_CACHED.connectionString
Hyperdrive connection string with caching on.
Connection strings:POSTGRES_URL is required in the env files; optional POSTGRES_MIGRATE_URL replaces it for migrations. Deploy reads both from .env.production only and doesn't upload them as secrets. req.env.POSTGRES_URL exists only in development.
Compatibility date: Postgres apps need 2024-09-23 or later. Goribu adds the nodejs_compat flag.
Development:pnpm dev connects both clients directly to the database in .env, so cached reads are always fresh locally.
Cached client restrictions: Never write or open a transaction on req.postgresCached. Queries that call NOW(), CURRENT_DATE, or RANDOM() are never cached.
Connections: Clients are created per request; never call .end(). A request runs up to three req.postgres and two req.postgresCached queries at once; more wait their turn.
No session state: Each query may use a different connection. Use SET only inside a transaction; temporary tables, session locks, and LISTEN/NOTIFY don't carry over.
Array results:int8[] and numeric[] return strings. Arrays of custom enum types return a single string, such as '{a,b}'.
Errors: Errors carry Postgres's code, such as 23505 for a unique violation. Branch on the code, not the message.
Query time: A query through Hyperdrive runs for at most 60 seconds. Migrations connect directly and aren't limited. See Database: Postgres.
Typed row results
// Inside a handler; Postgres and the posts table must exist.type Post = { id: number; title: string };
const posts = await req.postgres<Post[]>`
SELECT id, title FROM posts ORDERBY id DESC`;
Typed row results
const posts = await req.postgres`
SELECT id, title FROM posts ORDERBY id DESC`;
req.r2 is the main bucket declared in r2, and req.r2.private the optional private bucket. The GoribuR2 and GoribuR2Bucket types are exported from goribu/server.
API
Result or purpose
get, put, head, delete, list
Native R2 Workers API methods with the same arguments and results.
createMultipartUpload, resumeMultipartUpload
Native multipart uploads.
url(key)
Permanent public URL string on r2.domain. Main bucket only, and only when it has a domain.
signedUrl(key, options?)
Promise of an expiring link. Options: expiresIn seconds (default 900, up to 604,800), download filename, immutable boolean.
req.r2.private
The private bucket, with the same methods and signedUrl, but no url.
req.env.R2, req.env.R2_PRIVATE
Raw Cloudflare bucket bindings, for libraries that expect them.
Content type:put with a File or typed Blob sets its content type unless httpMetadata already names one.
Missing buckets: Using req.r2 without r2, or req.r2.private without r2.private, throws.
Development URLs: Under pnpm dev, url(key) returns a local /_goribu/r2/public/ path.
Keys:url() and signedUrl() reject empty keys and keys with . or .. path segments.
Signed links: A link works for anyone who has it until it expires; check access before creating one. immutable: true reuses one URL for half the lifetime, so use it only for contents that never change under the same key. Prerendering rejects signed links.
Signing secret: Signed links and upload references use GORIBU_SECRET. Changing it invalidates existing ones. See R2 files & uploads.
Public and private links
// Inside a handler; r2 must declare a domain and a private bucket.const avatarUrl = req.r2.url(user.avatarKey);
const downloadUrl = await req.r2.private.signedUrl(invoice.fileKey, {
expiresIn: 900,
download: "invoice.pdf",
});
Public and private links
// Inside a handler; r2 must declare a domain and a private bucket.const avatarUrl = req.r2.url(user.avatarKey);
const downloadUrl = await req.r2.private.signedUrl(invoice.fileKey, {
expiresIn: 900,
download: "invoice.pdf",
});
A route exports uploads to accept files into R2. The browser uploads each file first, then the form submits a signed reference that the handler receives as an R2Upload.
API
Contract
uploads route export
Object mapping field names to UploadPolicy objects.
FileUpload
{ name, children? }. A file field inside Form that uploads on selection. children is an optional (state) => children render function.
upload(file, { action, name, signal? })
Promise of a signed reference string for the field name declared at action. Send it to that route's handler as the field value.
UploadError
Error with a ready-to-show message and a code: type, size, forbidden, network, expired, or server.
Policy field
Contract
accept
Required non-empty array of MIME types, such as image/png or image/*. Parameters and */* are rejected.
maxBytes
Required positive integer. At most 100,000,000, or 5,000,000,000 with direct: true.
private
Boolean. Stores the field in r2.private, which must be configured.
direct
Boolean. The browser uploads straight to R2 through a short-lived pre-signed URL.
key
({ req, file, ext }) => string, called after middleware. Defaults to uploads/<random id><ext>.
R2Upload:{ key, name, type, size, url? }. url is present only for the public main bucket.
FileUpload render state:file, status (idle, uploading, done, or error), errorMessage, previewUrl, showPicker(), and dropProps for a drop target.
One file per field: Use separate field names for several files. FileUpload outside a Form throws; use upload() there.
New keys only: Uploads never overwrite an existing object, so a fixed key fails the second time.
Time limits: An upload must start within 60 seconds and the form must be submitted within an hour.
Rendering: A route with uploads always runs on the Worker. To keep a page static, put uploads and the handler in a separate action route.
Direct uploads: Production needs the Worker secrets R2_ACCESS_KEY_ID and R2_SECRET_ACCESS_KEY; deploy asks for them in a terminal. Development uploads through the local Worker without keys.
File contents: The browser reports the type and Goribu doesn't inspect contents. Abandoned uploads stay in the bucket. See R2 files & uploads.
defineConfig(config) validates and returns a frozen configuration. The fields are name, compatibilityDate, optional compatibilityFlags, database, kv, r2, domain, workersDev, responseHeaders, and wrangler.
Optional top-level domain connects one application hostname during deployment. workersDev: false turns off the workers.dev address and requires domain.
Postgres declaration:{ type: "postgres" }, or with both hyperdriveId and hyperdriveCachedId for configs you manage. Connection strings go in POSTGRES_URL and the optional POSTGRES_MIGRATE_URL env values, never in the config. See Database: Postgres.
KV declaration:{ name }.
R2 declaration:{ name, domain?, private?: { name }, jurisdiction?: "eu" }. private requires domain on the main bucket. See R2 files & uploads.
Wrangler block: Native Wrangler fields as JSON, such as ai, services or vars, deeply frozen and added to the generated config. Fields and binding names Goribu owns, unknown fields, and non-JSON values fail the build. See Wrangler bindings.
Header callback: Receives { request, env, mode, nonce } and synchronously returns a plain object of strings or string arrays. See Configuration for defaults, allowed names, environment files, and header restrictions.
Delete local R2 objects (every declared bucket) or KV keys, all or only keys starting with the prefix. Works while pnpm dev is running and never touches production.
pnpm exec goribu migrate:new <name>
Create the next numbered SQL migration. Migrations are forward-only; migrate:down was removed in 0.9.4.
pnpm exec goribu migrate
Apply pending local migrations.
pnpm exec goribu migrate:status
Read migration status.
pnpm exec goribu migrate:seed
Run local seed.sql; cannot target production (D1 only).
pnpm exec goribu --help
Print usage. -h also works.
pnpm exec goribu --version
Print the installed version. -v also works.
Production targets: Migrate, status, and d1 accept --remote, or --local for the default local target. Seed accepts only --local.
Postgres migrations:migrate:new, migrate, and migrate:status work with Postgres. With --remote, they connect to the database in .env.production. There is no Postgres version of d1; use psql "$POSTGRES_URL".
Development arguments: Dev accepts only --port and --host.
Argument-free commands: Build, check, deploy, tail, and cloudflare-login take no arguments.
CLI failures: Failures exit non-zero. Recognized Goribu failures print their repair message without a stack trace.
Type checking:pnpm typecheck is the TypeScript starter's separate tsc --noEmit script.
Scaffolding:pnpm create goribu@latest <name> creates an app.
Scaffolder options:--js, --help, and --goribu-tarball <path> for a matching local framework archive.
Scaffolder scope: Scaffolding never installs, authenticates, or deploys for you.
Handler types:Handler<Props, Env, Params> and Middleware<Env>.
UI types:Meta<Props> and Form<Data>.
Data types:req.d1<Row>, req.postgres<Row[]>, and req.kv.get<Value>(key, { type: "json" }). Postgres takes the array type, D1 the row type.
Postgres bindings:req.env isn't typed for POSTGRES and POSTGRES_CACHED. Declare them in the handler's Env type argument to read their connectionString.
Body validation: Request body values still need narrowing and validation.
createApp(options) from goribu/server returns a Worker app with fetch(request, env, ctx): Promise<Response>. Normal applications use goribu() in Vite; the build supplies the route graph and calls this API for them.
Required app options:manifest, modules, and errorPages.
Optional app options:documentId, middlewareByRoute, mode, clientAssetManifest, clientAssetBase, applicationConfig, and postgres, an (env) => { fresh(), cached() } factory for the two Postgres clients.
Compiled inputs: App options describe compiled routes, error pages, assets, and validated config. They are not additional goribu.config.js fields.