Hooks 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.
A server component calls a module-local hook with const task = useTask(id). The
compiler inlines the hook at the call site in the async component function. The body may
await, use server markup and call other hooks. Each invocation gets its own let and
const; parameters are evaluated when the call is reached.
async func loadTitle(id number) string {
return `Task #${id}`
}
hook useTask(id number) {
const title = await loadTitle(id)
defer console.log("finished rendering task", id)
return { title }
}
comp TaskRow(id number) {
const task = useTask(id)
return <li>{task.title}</li>
}
The record in the hook's final return exposes its local names. A hook's defer runs with the
component's other deferred calls when it returns or throws, in reverse registration order.
Use ordinary let and const for server data. Browser declarations such as state,
derived, ref, mount and effect cause the same errors as in a server component.