Html SSR
On the BEAM target you can build HTML with the same Html and Html.Attributes modules you know from Elm, then send the result as an HTTP response, write a static site, or prerender the same TEA view your SPA will hydrate.
Rendering
Tag helpers become safe markup. Text and attribute values are escaped for you.
| Function | Result |
|---|---|
Html.toString | A fragment as a string |
Html.toDocument | A full document with doctype |
Server.html status headers view | A text/html HTTP response |
Server.pageHtml title module flags | SPA shell with an empty #app |
Server.page { title, module_, flags, body } | SPA shell with SSR markup inside #app for hydrate |
handle req =
case req.method of
"GET" ->
Server.html 200 []
(Html.main_ []
[ Html.h1 [] [ Html.text "hello" ]
]
)
_ ->
Server.empty 404 []Hydrate the same TEA view
Put init / update / view in a shared path package. The server calls prerender (first view after init, discarding Cmds) and Server.page with matching JSON flags. The [client].entry wraps the same functions in Browser.element so VirtualDom attaches to the SSR tree instead of an empty #app.
spa =
let
flags = { count = countOf }
in
Server.string 200 [] "text/html"
(Server.page
{ title = "Hydrate"
, module_ = "Main"
, flags = Page.encodeFlags flags
, body = Page.prerender flags
}
)Html.Events handlers are inert on the BEAM (they do not appear in the markup). Clicks before JS boots do nothing; after hydrate, the client attaches real listeners. Prefer Browser.element into #app for this path. See examples/11_hydrate.
Static export
This documentation site is itself a selm app. You declare pages once, then export them to static files:
cd site
selm export --out _siteselm export builds the project, writes each page in Site.pages with Html.toDocument, writes optional raw Site.files (for example feed.xml), and copies static/ into the output tree.
Link previews and feeds
Use OpenGraph.website (or OpenGraph.tags) in your document head so Slack, Discord, and similar apps can show a title, description, and image. Build an RSS 2.0 string with Rss.feed and expose it from Site.files:
files : List { path : String, body : () -> String }
files =
[ { path = "feed.xml"
, body = \_ ->
Rss.feed
{ title = "Docs"
, link = "https://example.com/"
, description = "Project docs"
, language = Just "en"
, items = []
}
}
]Absolute og:url / og:image values matter for crawlers. This site keeps its public origin in SiteMeta and links feed.xml from every page head.
Shared views
Put view modules in a local package and use them from both the server and the SPA. Markup-only sharing: examples/ui and examples/02_counter. Full TEA hydrate: examples/11_hydrate.