# Components of the server

> 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](https://github.com/QWERN-9876r/RookLang/blob/HEAD/docs/components.md): components, decorators, context,
> components as values. It describes only what is different on the server.

```go
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 `return`s is simply the result.
- **A component may wait:** `await` is allowed in its body, so it loads what it shows itself. In
  JavaScript every component of the server is an `async function`; it is not marked `async` in
  Rook, since all of them are.
- **The result is any value.** Its type is inferred from the `return`s, as for a function literal,
  and may be written after the properties, as the result of a function:

  ```go
  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: `Html` for `UserCard`, `User[]` for
  `Users`. The tag waits for the component by itself, so `await <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, `?T` that 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:

```go
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 />` is `Html | Response`, and markup takes it: a `Response` is 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 `defer` of each one closes what it opened.
- A component that wants the `Response` of a child as a value, to look at it, takes it outside
  markup: `const page = <Account />` gives `Html | Response`, which
  `if page instanceof Response` narrows.
- **`try` lets it go up:** `const value, err = try await children()` of an error boundary catches
  errors, and a `Response` from 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.

