API
fondu
The server's API, for app/server.ts, routes and middleware.
createApp()
createApp(options?) returns the Hono app made of app/routes/.
| Option | Is |
|---|---|
document | the HTML around pages, ({ head, body, c, nonce }) to JSX or a string: The document |
csrf | hono/csrf's options, or false |
bodyLimit | the largest request body in bytes (default 10 MB, then 413: hono/body-limit), or false |
secureHeaders | hono/secure-headers' options, or false |
The options are checked when createApp() is called: an unknown option (there is no port: the built server
listens on PORT), a wrong type, or a bodyLimit that is not a positive number or false throws
fondu: createApp(): ….
The Content Security Policy by default: default-src 'self'; scripts of the site or with the request's nonce (a JSX
document's scripts have it); inline styles (React's style attributes); data: images and fonts;
object-src 'none'; base-uri 'self'. A directive of yours replaces that one only:
createApp({
secureHeaders: { contentSecurityPolicy: { fontSrc: ["'self'", 'https://fonts.gstatic.com'] } },
})contentSecurityPolicy: false: no policy.
secureHeaders also sets Referrer-Policy: same-origin by default. back() relies on it: it reads the page the request came
from in Referer, and a stricter policy (no-referrer) leaves it empty, so back() goes to its fallback. Change referrerPolicy
only for a reason, and keep a policy that sends Referer to this site.
Handlers
| Export | Is |
|---|---|
defineHandler(...middleware, handler) | a route's handler: Hono's createHandlers |
defineHandler.withValidator({ body, query, params }) | the same, with input checked by Standard Schemas: (c, input) => … |
defineMiddleware(fn) | a middleware: Hono's createMiddleware |
withoutSsr() | a middleware: these pages render in the browser only |
ValidationError | throw new ValidationError({ field: 'message' }): back with the errors |
Responses
| Export | Is |
|---|---|
render(c, Page, props?, options?) | its file's page; options: status |
share(c, props) | props every page of this request gets |
flash(c, data) | data for the next page (usePage().flash) |
back(c, fallback?) | a redirect to the page the request came from |
Types
| Export | Is |
|---|---|
Register | declared by the app: shared and flash type share(), flash(), usePage() (Type safety) |
RenderOptions | the options of render(): { status? } |
AsJson | a type as a page receives it as JSON: AsJson<Row> (a Date is a string) (Type safety) |
DocumentTemplate | the type of the document option: (parts) => JSX or string (The document) |
DocumentParts | what a document template gets: { head, body, c, nonce } |
CreateAppOptions | the options of createApp(), as the table above has them |
Route exports
| Export | Is |
|---|---|
default (in a .tsx file) | the page, rendered by render(), or on GET without a loader |
loader (in a .tsx file) | GET |
action (in a .tsx file) | POST |
actions = { name } (in a .tsx file) | POST ?name (action or actions, not both) |
GET, POST, PUT, PATCH, DELETE, OPTIONS | HTTP handlers (in a .ts file) |
prerender | true, false or a function listing params: a static page; any route exporting it turns prerendering on |
params | { name: ['value', …] }: the values a param takes |
default (in _middleware.ts) | the directory's middleware |
Route files
| File | Is |
|---|---|
name.tsx, name.mdx | a page and its handlers (what each exports) |
name.ts | server code only: HTTP handlers |
_middleware.ts | the middleware of its directory and below |
_layout.tsx | the layout of the pages in its directory and below |
_error.tsx | the error page |
_name.tsx, _name/ | not a route: a file, or a directory, starting with _ |
name.test.tsx | not a route: a test (*.test.*, *.spec.*) |
fondu/client
Hooks and href() run on the server too, in a page's server render (so does RequestError, a class).
request(), reload(), navigate(), closeShallow() and loadDocument() are for the browser, from events and effects: on
the server they throw.
| Export | Is |
|---|---|
href(path, params?) | a URL of app/routes/, typed: href('/users/:id', { id: 7, query?, hash? }) (Type safety) |
usePage() | the page on screen, a Page: component (its id: its route file's path), props, url (path and query), flash |
useNavigation() | the navigation on its way: { state, to, form }, state 'idle', 'loading', 'submitting' or 'failed' (a form not sent) |
useForm(schema?) | a form checked in the browser by its action's schema: { form, field, errors, submitting, failed } (Forms) |
reload() | the page's props again, its state and scroll position kept |
navigate(href, { replace?, keep?, transition? }) | goes to href as a link would: a page in place, anything else as a document; keep: the page keeps its state and scroll, as reload() does; transition: true or the types to add, a view transition (Navigation) |
navigate(href, { shallow: true, replace? }) | changes the page on the screen to a page URL of this app, keeping its component and props, asking for nothing, with no transition; usePage().url follows (Shallow navigation) |
closeShallow(to) | closes what a shallow navigation opened, one step: Back, or to in place of the current entry (A dialog or a drawer) |
loadDocument(href) | a document load, for what a page can't change in place |
request(path, options?) | a typed call to a .ts route from the browser: request('/api/users/:id', { id: 7, method?, query?, body? }); a non-2xx answer throws (Routes) |
RequestError | what request() throws for a non-2xx answer: status, data (the parsed body), response |
Types
| Export | Is |
|---|---|
Errors | the type of a page's errors prop: field name to message |
Href | the URLs href() takes: each route's pattern, and pattern?action for its actions |
Navigation | what useNavigation() returns: { state, to, form } |
Page | what usePage() returns: { component, props, url, flash }; Page<P> types props as P and errors |
fondu/client/internal is the Vite plugin's entry, for the plugin only: an app does not import it, and it may change in
any release.
fondu/vite
The Vite plugin: fondu(options). The options are checked when the plugin is created: target is required, and an unknown option fails. There is
no port option: the built server (Bun and Node) listens on PORT, else 3000.
| Option | Is |
|---|---|
target | 'bun', 'cloudflare' or 'node' |
progress | the navigations' progress bar: { color, delay, forms } (default: the text color, 350 ms, forms included), or false |
viewTransitions | true: a navigation that shows another page runs inside the browser's view transition by default (not keep or reload()), and a link or call opts out; navigation |
react | passed to @vitejs/plugin-react (its include has .mdx by default) |
Types
| Export | Is |
|---|---|
FonduOptions | the options of fondu(), as the table above has them |
ProgressOptions | the progress option's object: { color?, delay?, forms? } |
For proxies, CDNs and tests
What an app in front of a cache, or a test that is not a browser, can rely on. The header names, cookie names and /_fondu/*
paths Fondu uses between its server and its client are internal: they may change in any release, and are not listed here
except where a client has to know them.
Vary: a page answers a document and a navigation (a request withX-Fondu: true) differently, and says so: Caching. A response thatAcceptchooses also hasVary: Accept: Errors.- A navigation from a client built from other assets is a 409 with an
X-Fondu-Locationheader: the URL to load as a document. A client of your own that sendsX-Fondu: truegets it too, and can load that URL.
The statuses Fondu answers itself, apart from the 404 and 500 of the error page:
| Status | When |
|---|---|
| 303 | back(), and a form's ValidationError: back to the page |
| 400 | a body or query withValidator can't read as one object |
| 405 | a method the route has no handler for, with Allow (Errors) |
| 409 | a navigation from an old build: see above. Also a redirect to another site, which a navigation can't follow |
| 413 | a body over bodyLimit |
| 415 | a body that is neither JSON nor form data, where a handler reads one |
| 422 | a ValidationError for an API client ({ errors }), and a GET whose query is invalid |
| 499 | the client left before the answer: an empty response, nothing logged as an error |
Versioning
Before 1.0, a minor release (0.20 to 0.21) may break an app, and a patch release (0.20.0 to 0.20.1) does not. Each breaking change is in the release notes, with what to change. A type that starts to reject code that already misbehaves at runtime is a fix, not a break.