Project structure
The files
my-app/
├─ app/
│ ├─ server.ts # the server: export default createApp()
│ ├─ routes/ # the file path is the URL
│ │ ├─ _middleware.ts # runs for its directory and below
│ │ ├─ _layout.tsx # around every page (a directory's _layout.tsx: around its pages)
│ │ ├─ _error.tsx # the error page (404, 403, 500…)
│ │ ├─ index.tsx # /: its loader and actions, and the page
│ │ ├─ users/[id].tsx # /users/:id
│ │ └─ api/health.ts # /api/health: server code only
│ └─ fondu.gen.d.ts # the generated types (commit it)
├─ package.json # fondu, hono, react, react-dom
└─ vite.config.ts # plugins: [fondu({ target: 'node' })]vite.config.ts
fondu() is the Vite plugin. It takes the target, bun, cloudflare or node, and a few options more (progress, viewTransitions, react: API):
import fondu from 'fondu/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [fondu({ target: 'node' })],
})On Cloudflare, the Cloudflare plugin comes first and runs the Worker in workerd, in development too:
plugins: [cloudflare({ viteEnvironment: { name: 'ssr' } }), fondu({ target: 'cloudflare' })]app/server.ts
The server: createApp() returns a Hono app made of app/routes/, with CSRF protection, security headers (a Content
Security Policy included), one URL per page (/users/ redirects to /users: 301 for GET and HEAD, 308 for other methods, with no option for it) and error pages built in.
import { createApp } from 'fondu'
export default createApp()Its options: document (the HTML around pages, as in The document), csrf and secureHeaders
(Hono's options, or false), and bodyLimit (bytes, or false). The API lists them.
Server code
The .ts files of app/routes/, app/server.ts, files named *.server.ts and fondu itself are server code:
browser code must not import them, and the build stops if it does. A page's loader and actions are server code too,
in the page's own file: the browser's copy of it is the page alone. Server and client code explains where each file runs.
package.json maps #/* to ./app/*, so import { db } from '#/db/index.server.ts' works from anywhere in app/.
app/fondu.gen.d.ts
The dev server and the build write the types of your routes here: they make href() typed.
A new app ships it. Commit it, so the types are there before the first build (in CI, say); don't edit it.
Editor snippets
A new app has .vscode/fondu.code-snippets: type /route or /page in a new .tsx route file, /loader,
/action, /actions, /validated, /layout or /form in one, /api, /get … /delete or /middleware in a
.ts file. Edit them freely.
One way to organize an app
Fondu reads routes/ and the files above; the rest of app/ is yours, and Fondu only checks that browser code
doesn't import server code. Here is one way to arrange it, which the demo follows. It is an example, not
a rule.
app/
├─ routes/ # URLs: their handlers and pages, the layouts
├─ components/ # all UI that isn't a page
│ ├─ ui/ # the primitives: buttons, fields, dialogs
│ └─ users/ # the parts of the pages in routes/users/
├─ modules/ # what the app does, a directory per domain, no UI
│ ├─ auth/ # auth.server.ts, guards.server.ts
│ └─ users/ # users.server.ts, schemas.ts
├─ lib/ # helpers that know no domain: dates, class names
└─ db/ # the tables and the client- A route file is short: its handlers call a module, its page puts components together.
- A page's own parts are in
components/, in a directory named like the page's route. - A domain keeps its code together:
modules/users/has the server code, the schemas its routes validate with and its forms use, and the types of the data it gives pages. - A new file goes where the first answer is yes: a URL,
routes/; UI,components/; about one domain,modules/<domain>/; anything else,lib/.