Fondu

API

View as Markdown

fondu

The server's API, for app/server.ts, routes and middleware.

createApp()

createApp(options?) returns the Hono app made of app/routes/.

OptionIs
documentthe HTML around pages, ({ head, body, c, nonce }) to JSX or a string: The document
csrfhono/csrf's options, or false
bodyLimitthe largest request body in bytes (default 10 MB, then 413: hono/body-limit), or false
secureHeadershono/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

ExportIs
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
ValidationErrorthrow new ValidationError({ field: 'message' }): back with the errors

Responses

ExportIs
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

ExportIs
Registerdeclared by the app: shared and flash type share(), flash(), usePage() (Type safety)
RenderOptionsthe options of render(): { status? }
AsJsona type as a page receives it as JSON: AsJson<Row> (a Date is a string) (Type safety)
DocumentTemplatethe type of the document option: (parts) => JSX or string (The document)
DocumentPartswhat a document template gets: { head, body, c, nonce }
CreateAppOptionsthe options of createApp(), as the table above has them

Route exports

ExportIs
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, OPTIONSHTTP handlers (in a .ts file)
prerendertrue, 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

FileIs
name.tsx, name.mdxa page and its handlers (what each exports)
name.tsserver code only: HTTP handlers
_middleware.tsthe middleware of its directory and below
_layout.tsxthe layout of the pages in its directory and below
_error.tsxthe error page
_name.tsx, _name/not a route: a file, or a directory, starting with _
name.test.tsxnot 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.

ExportIs
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)
RequestErrorwhat request() throws for a non-2xx answer: status, data (the parsed body), response

Types

ExportIs
Errorsthe type of a page's errors prop: field name to message
Hrefthe URLs href() takes: each route's pattern, and pattern?action for its actions
Navigationwhat useNavigation() returns: { state, to, form }
Pagewhat 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.

OptionIs
target'bun', 'cloudflare' or 'node'
progressthe navigations' progress bar: { color, delay, forms } (default: the text color, 350 ms, forms included), or false
viewTransitionstrue: 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
reactpassed to @vitejs/plugin-react (its include has .mdx by default)

Types

ExportIs
FonduOptionsthe options of fondu(), as the table above has them
ProgressOptionsthe 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 with X-Fondu: true) differently, and says so: Caching. A response that Accept chooses also has Vary: Accept: Errors.
  • A navigation from a client built from other assets is a 409 with an X-Fondu-Location header: the URL to load as a document. A client of your own that sends X-Fondu: true gets it too, and can load that URL.

The statuses Fondu answers itself, apart from the 404 and 500 of the error page:

StatusWhen
303back(), and a form's ValidationError: back to the page
400a body or query withValidator can't read as one object
405a method the route has no handler for, with Allow (Errors)
409a navigation from an old build: see above. Also a redirect to another site, which a navigation can't follow
413a body over bodyLimit
415a body that is neither JSON nor form data, where a handler reads one
422a ValidationError for an API client ({ errors }), and a GET whose query is invalid
499the 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.

Last updated