Decorators on 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.
Decorators keep what they are in the browser: state and functions of their own, public members,
context, includes, a wrapper. On the server their body may also wait and may answer instead of
the component.
dec admin() {
if @session.user?.role != "admin" {
return reply("Forbidden", { status: 403 })
}
return (page Html) => <Layout title="Admin">{page}</Layout>
}
@admin
comp Dashboard() {
const stats = await db.stats() // not run without access
return <main>...</main>
}
awaitis allowed in the body of a decorator:public const user = await userOf(request).An early
returnof a value (not the function of a wrapper) is the result of the component: the body of the component and the bodies of the decorators below do not run, and the wrappers are not applied. Its type is added to the type of the component's tag:<Dashboard />isHtml | Response. This is a guard: an access check, a limit of requests, a cache that already has the answer.Arguments can read the component's context. In
@guard(@api.secret, request)the compiler infers the dependency on@api. The value is read before the guard runs.A parameter with
publicis a member,dec http(public request Request, public ip string), read as@http.request, so a context does not copy its parameters into constants.The last
returnis still the wrapper,(page T) => U.Tlimits where the decorator can be applied, as(form HTMLFormElement) => ...does in the browser;Uis the result of the tag. The wrapper may wait, as the body does.A wrapper that takes
Content<T>gets the body instead of what it gave: a function that runs the component's body, with the wrappers of the decorators below, when the wrapper calls it. So a decorator decides whether the body runs and catches what it throws, an error boundary written in Rook:dec fallback(view func(Error) Html) { return (page Content<Html | Response>) => { const value, err = try await page() if err != null { return view(err) } return value } } @fallback((err) => <Oops message={err.message} />) comp Feed() { ... }trylets aResponsefrom below go up, so a redirect under the boundary is still the answer. The bodies of the decorators run before, as always: the boundary catches the component and what it renders. What the component gives is checked againstT.mountdoes not exist on the server: there is nothing to mount. The cleanup of a decorator is adeferin its body, which runs when the component ends, after its children.The order is that of the browser: bodies from top to bottom, wrappers from the bottom up; a guard of the top decorator stops the ones below it.