Pages and props
A page is a React component: the default export of a route file. app/routes/users/[id].tsx is the page at
/users/:id, and its loader, in the same file, renders it with its props.
// app/routes/users/[id].tsx
import { defineHandler, render } from 'fondu'
// GET /users/7, on the server
export const loader = defineHandler(async (c) => {
const user = await getUser(c.req.param('id'))
return render(c, Show, { user: { name: user.name } })
})
// the page: the part of the file the browser gets
export default function Show({ user }: { user: { name: string } }) {
return (
<>
<title>{`${user.name} · My app`}</title>
<h1>{user.name}</h1>
</>
)
}A page can also be an MDX file. The parts around pages are layouts. How the app moves from one page to the next is in Navigation.
Props
The props are what the loader gives render(). They are the page's only data from the server.
render(c, Show, { user })
render(c, Show, { user }, { status: 201 }) // another statusrender()is type-checked against the component: a missing prop, or one of the wrong type, is an error.- The props travel as JSON. A
Datearrives as a string, so the page types itstring. AMap, aSetor a function can't be sent: typing a prop that way is an error. Type safety has the details, andAsJson<Row>to type a page's props from the data's own type (Props from your data). - The props are written into the page's HTML and JSON: anyone who can see the page can read them. Send what the page
shows (
{ name: user.name }), not whole records. - A route file renders its own page only. To use another route's page, make it this file's default export too:
export { default } from './index.tsx'.
A page without a loader is rendered on GET with no props of its own.
Title and meta tags
A page renders its own <title>, <meta> and <link> tags where it likes; React moves them into <head>, on the
server too (a tag in a Suspense boundary that resolves later is sent in <body>). A title is one string, hence the template literal above.
<>
<title>{`${post.title} · Blog`}</title>
<meta name="description" content={post.summary} />
<link rel="canonical" href={`https://example.com/posts/${post.slug}`} />
</>What every page shares (the charset, icons, fonts) goes in the document.
Shared props
Props every page gets, such as the signed-in user, are set once in a middleware with share():
// app/routes/_middleware.ts
import { defineMiddleware, share } from 'fondu'
export default defineMiddleware(async (c, next) => {
const user = await currentUser(c)
share(c, { auth: { user: user && { name: user.name } } })
await next()
})A shared value can be a function: it runs only when a page renders, not for API requests. Pages and layouts read
shared props with usePage().props. Type safety shows how to type them.
A slow prop
A page is sent with all its props, so a slow one holds it. To leave it out, render the page without it and ask for it
from the browser: a .ts route gives the data, and the page calls it with request(). Put the .ts route next to the
page, in the same directory, so the same _middleware.ts guards both:
app/routes/dashboard/
├─ _middleware.ts # the sign-in check: guards index.tsx and stats.ts
├─ index.tsx # /dashboard
└─ stats.ts # /dashboard/statsA .ts route is guarded only by the _middleware.ts files above it. Nothing in the page protects it: a .ts route
outside the directory is open to anyone who asks for its URL.
// app/routes/dashboard/stats.ts
import { defineHandler } from 'fondu'
export const GET = defineHandler(async (c) => c.json(await loadStats()))// app/routes/dashboard/index.tsx
import { defineHandler, render } from 'fondu'
import { request } from 'fondu/client'
import { useEffect, useState } from 'react'
export const loader = defineHandler((c) => render(c, Dashboard, { user: currentUser(c) }))
export default function Dashboard({ user }: { user: User }) {
const [stats, setStats] = useState<Stats | null>()
useEffect(() => {
const ask = new AbortController()
// an error shows as "not available"; leaving the page aborts the request
request('/dashboard/stats', { signal: ask.signal }).then(setStats, () => setStats(null))
return () => ask.abort()
}, [])
return (
<>
<h1>{user.name}</h1>
{stats === undefined ? <p>Loading…</p> : stats === null ? <p>The stats are not available.</p> : <Chart stats={stats} />}
</>
)
}The document has the loading state (undefined) and so has a page without JavaScript. The error state (null) is for a failed request, and the abort is for a page left before the answer. What decides the status or a redirect is awaited in the
loader, as before. A promise as a prop of render() ({ stats: loadStats() }) throws, as JSON would send it as {}: await it.
A promise inside a prop is not checked.
Reading the page in a component
usePage() from fondu/client returns the page on the screen, in any component:
| Field | Is |
|---|---|
component | the page's id: its route file's path in app/routes/, without the extension (users/[id]) |
props | the page's props, with the shared props and errors |
url | the page's URL (path and query) |
flash | the data an action left for this page with flash(), shown once |
Its type is Page (Page<Props> for the props you give it: usePage<Props>()).
import { usePage } from 'fondu/client'
export function Greeting() {
const { props } = usePage()
return props.auth.user ? <p>Hello, {props.auth.user.name}</p> : null
}