Server and client code
Every file in app/ runs on the server, in the browser, or in both. Where a file runs decides what it can do, and
whether it ends up in the JavaScript your visitors download.
Where each file runs
| Files | Runs | In the browser's bundle |
|---|---|---|
.ts files of app/routes/, app/server.ts, *.server.ts | on the server only | never |
a page's loader, action, actions (in its .tsx) | on the server only | never |
| pages, layouts, their components | on the server (the first render), then in the browser | yes |
| anything else they import (a schema, a type) | wherever it is imported | if a page imports it |
The browser's bundle is everything the pages and layouts import, directly or not. Each page is a chunk of its own, loaded when it is visited.
A page's route file
A .tsx route file is both: its handlers run on the server, its page on both. The browser gets a copy of the file
without its server code: loader, action, actions, prerender and params are removed, then the imports and
top-level declarations only they used, and so on until none is left.
// app/routes/users/[id].tsx
import { defineHandler, render } from 'fondu' // removed: only the loader uses it
import { getUser } from '#/modules/users/users.server.ts' // removed
import { Avatar } from '#/components/avatar.tsx' // kept: the page uses it
const select = (u: User) => ({ name: u.name }) // removed: only the loader uses it
export const loader = defineHandler(async (c) => render(c, Show, select(await getUser(c))))
export default function Show({ name }: { name: string }) {
return <Avatar name={name} />
}The copy is made from the source, before TypeScript and JSX are compiled, so what the page uses is read as you wrote it:
a JSX name (<Avatar>, <ui.Avatar>) counts, a type does not (typeof loader in a type is fine). The copy does not
follow an enum, a namespace, import x = …, a class with a parameter property or a decorator: a route file that has
one fails the build, and it goes to another module. The tsconfig of a new app has "erasableSyntaxOnly": true, so the
editor flags it as you type; add it to an older app's. A type-only import or export (import type, import { type Row },
export type) is removed too: in a page's route file, either spelling of a type import is fine. Source maps carry the copy, not the file: with build.sourcemap, and in development,
sourcesContent holds the page without its loader. The server runs the whole file.
What is left is what the page uses, so a server module it still imports (a helper the page calls too) stops the
build, as any other: declare what the server and the browser share in a module of its own. A page imports from
fondu/client: fondu is the server's. Server code may import href() from fondu/client too (for a redirect); request(), navigate()
and the other calls for the browser throw there.
Server code
The .ts files of app/routes/, app/server.ts and files named *.server.ts are server code: they read the database,
the secrets, the session. If a page or a component imports one, even through another file, the build stops and names
the import:
fondu: /app/db/index.server.ts is server code (routes/*.ts, server.ts, *.server.ts) and cannot be imported by browser codeName a module *.server.ts when it must never reach the browser, and import it from handlers and other server files.
A type is not code: a page can take one from a server file with import type, which is removed with the rest of
TypeScript before the build looks at the imports.
// app/components/users/card.tsx
import type { UserSummary } from '#/modules/users/users.server.ts'Write the whole import as import type. With verbatimModuleSyntax, which a new app's tsconfig.json has,
import { type UserSummary } from … leaves an empty import of the file behind, and the build stops at it.
Code for both
Pages and their components run twice: on the server for the first render, then in the browser. So they must not touch
window or document while rendering (do it in an effect), and they can't reach
the server's resources: what they need comes as props.
A module with no secrets, such as a validation schema, can be shared: a route validates with it, a page checks the same rules before sending.
Route modules load with the app
Every file of app/routes/ is imported when the app starts (in development, on the first request), so what a route
imports at the top runs for every page, not only for its own. Keep a heavy dependency out of the start: import the
entry you need (one icon's module, not the whole icon library), or import it in the handler:
export const GET = defineHandler(async (c) => {
const { renderPdf } = await import('#/lib/pdf.server.ts') // loaded on the first request to this route
return c.body(await renderPdf(), 200, { 'Content-Type': 'application/pdf' })
})Editing in development
The dev server updates what an edit changes, without reloading the page where it can:
- A page or a component (
.tsx,.mdx): React Fast Refresh, the page's state kept. - A route's handlers, or code they import: the page asks for its props again (
reload()), its state and scroll kept. A page's file is both: its page is refreshed, its props asked for again. app/server.ts, or code only it imports: the page reloads, as the document around it changed.- An app that fails to load (a route that throws as it is imported): Vite's overlay shows the error, as a whole page; the next edit reloads it.
The dev server doesn't watch node_modules: after updating a dependency, Fondu included, restart it, or it keeps
running the version it loaded.
Props are public
Props are the only way data goes from a route to a page, and they are written into the HTML and the JSON of the page:
anyone who can see the page can read them. Send what the page shows, not whole records: { name: user.name }, not a
user row with its password hash.
Environment variables and secrets
- On Cloudflare:
import { env } from 'cloudflare:workers'in server code, for variables, secrets and bindings, typed bycf-typegen. - On Bun and Node:
process.env, in server code. In development, Fondu loads.env,.env.local,.env.developmentand.env.development.localinto it; a variable already set (by the shell, the host) is not replaced. In production the server reads them: a Node app'sstartscript has--env-file-if-exists=.env(.envalone; the flag needs Node 22.9 or later, and a new Node app'senginessays 22.12), and Bun reads.env,.env.productionand.env.localitself. import.meta.env.VITE_*is written into the browser's bundle by Vite: only for values that are public anyway.