Components of the server
Components on the server — design
Status: implemented are markup as HTML, components of the server, their results,
Responsegoing up,Content<T>, component values with a result, decorators with guards, wrappers that take the body and the request context, routes, androoklang/server; the server of the site,site/server/main.rk, is written with them, and the guide has a section on them. The decisions are collected at the end of the document, together with what is not decided yet. Server markup, components androoklang/serverremain experimental in 1.x and may change between releases.
This document builds on components.md: components, decorators, context, components as values. It describes only what is different on the server.
comp UserCard(user User) {
return <article class="user">
<h2>{user.name}</h2>
<p>{user.email}</p>
</article>
}
comp UserPage(id string) {
const user, err = await db.users.find(id)
if err != null {
return reply(<NotFound />, { status: 404 })
}
return <Layout title={user.name}><UserCard user={user} /></Layout>
}
The body runs once for each use, from the top, as in the browser. There are no updates, so the choice between
returns is simply the result.A component may wait:
awaitis allowed in its body, so it loads what it shows itself. In JavaScript every component of the server is anasync function; it is not markedasyncin Rook, since all of them are.The result is any value. Its type is inferred from the
returns, as for a function literal, and may be written after the properties, as the result of a function:comp Users() User[] { // JSON for an API return await db.users.all() } comp Profile(user ?User) Html | Response { if user == null { return Response.redirect("/login", 303) } return <h1>{user.name}</h1> }A component that uses itself writes the type: the cycle needs it before the body is checked. Another module takes the type of an exported component from its module.
A tag of a component is its use.
<UserCard user={user} />gives the result of the component, and the tag's type is the type of that result:HtmlforUserCard,User[]forUsers. The tag waits for the component by itself, soawait <UserCard />is not written.Tags side by side run at the same time. In
<main><Header /><Feed /><Footer /></main>the three components start together and the markup is joined in order once all of them end, so the time of the page is that of the slowest part, not the sum.Properties are checked as in the browser: types, default values,
?Tthat may be left out, spreads,...attrs. They are read-only, like the properties of a browser component.
A Response goes up
A component deep in the tree may decide that there is no page at all: a redirect to the login, a
404, a 403. It returns a Response, and when a Response is among the children of markup, the
rendering of the whole request stops and that Response is the answer:
comp Layout(title string, children Content) {
return <html>
<head><title>{title}</title></head>
<body>{children}</body>
</html>
}
comp Account() {
if @session.user == null {
return Response.redirect("/login", 303)
}
return <section>...</section>
}
// The redirect goes out, without the layout around it.
<Layout title="Account"><Account /></Layout>
- The type of
<Account />isHtml | Response, and markup takes it: aResponseis a child that may stop the rendering. Other values are still errors. - The components that already run side by side end, and their results are thrown away: the
response waits for them, so that the
deferof each one closes what it opened. - A component that wants the
Responseof a child as a value, to look at it, takes it outside markup:const page = <Account />givesHtml | Response, whichif page instanceof Responsenarrows. trylets it go up:const value, err = try await children()of an error boundary catches errors, and aResponsefrom below is not one of them, so a redirect or a 401 under the boundary is still the answer.
This is what redirect() and notFound() do in Next.js and Remix, but with a value instead of an
exception: the type of a component says that it may answer instead of the page.