Components of the server

Components on the server — design

Status: implemented are markup as HTML, components of the server, their results, Response going up, Content<T>, component values with a result, decorators with guards, wrappers that take the body and the request context, routes, and rooklang/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 and rooklang/server remain 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>
}

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>

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.

Markdown source on GitHub